I use FastJSON to deserialize a json into an object. During the deserialization I would like to automatically replace all the null values in the json by an empty string. Is there a JSON parameter to do that? Otherwise, what would you suggest?
You can use JSON.parseObject(String json, Class<T> clazz, Feature... features) method and pass through Feature.InitStringFieldAsEmpty as last argument.
Related
If I do this:
import { MyType } from 'somewhere';
class MyClass {
myObj: MyType = new MyType();
updateObject(newVal: string): void {
myObj.thing = newVal;
this.saveStuff(JSON.stringify(myObj));
}
saveStuff(json: JSON): void {
// http request...
}
}
I get an error that I'm passing a string, not JSON. (I understand that I am in fact passing a string) How can I make it take the string as JSON?
I tried casting the string as JSON, ie: JSON.stringify(foo) as JSON or <JSON> JSON.stringify(foo). But I get a "Type 'string' cannot be converted to type 'JSON'." error both ways.
What you are doing with the TypeScript type annotation is to announce the type you expect a value to have (you don't declare the type). More often than never it happens that you set a type annotation and when you run the code you discover that the actual type is something else.
In this case though the TS compiler can evaluate the types beforehand. It knows that JSON.stringify returns a string. Since you have annotated the saveStuff method to accept a JSON object, it will give you a compiler error for the type mismatch.
Regardless of its content, a string remains a string. It may contain JSON, XML or a poem. It will still be nothing else than a string. The JSON class is just a utility class that provides you with a way to serialize and deserialize a JavaScript object into and from a string (with JSON content).
JSON is not a type. When you parse a string by calling JSON.parse(str), what you get is an object literal.
In your code, as you call JSON.stringify(foo), you are converting the object literal foo to a string.
Thus, your saveStuff() receives a string.
JSON stands for JavaScript Object Notation. It is just a specification on how to represent an object.
I'm trying to create a custom deserializer for generic lists. Lets say I get a json representation of class B:
public class B{
List<A> listObject;
}
where A is some other class which I see only at runtime. I'd like to create a deserializer that will be able to infer the type of listObject as list with inner type A and deserialize it as such instead of using the default hashmap deserializer.
I tried using contextual deserializer, similar to what was suggested here
and then adding it as a custom deserializer for List
addDeserializer(List.class, new CustomListDeserializer())
But I'm not sure how am I supposed to read the json and create the list in deserialize function (in the Wrapper example above it's pretty simple, you read the value and set it as a value field, but if my 'wrapper' is List, how do I read the values and add them?)
I tried using readValue with CollectionType constructed with constructCollectionType(List.class, valueType) but then I go into an infinite loop, since readValue uses the deserializer from which it was called.
Any ideas?
Thanks for the suggestion. I solved it by parsing the json as an array of inner generic type and then converting to list, as follows:
Class<?> classOfArray = Array.newInstance(valueType.getRawClass(), 0).getClass();
Object[] parsedArray = (Object[]) parser.getCodec().readValue(parser, classOfArray);
return Arrays.asList(parsedArray);
I have a model class that has a property of type 'Object'.
class Model{
private Object data;
......
}
When I return the response :
return Response.ok(modelObject).build();
if 'data' is of string type, I want to bypass it as raw string to avoid quotes escaping as it is JSON format. I found that #JsonRawValue does the work. However, if 'data' is some object other than string, it produces the entire response in plain text format. (return type is MediaType.APPLICATION_JSON)
My requirement is to apply the annotation only when data is of String type else don't apply.
Is there a way to achieve this or if there is any other solution?
A plain string is not a JSON, so your response should be text/plain in this case and not application/json
Add TEXT_PLAIN to your #Produces
#Produces({MediaType.APPLICATION_JSON,MediaType.TEXT_PLAIN})
public Response myService()
And set the mediatype according to response content
return Response.ok(modelObject).type(MediaType.APPLICATION_JSON).build();
return Response.ok(plainString).type(MediaType.TEXT_PLAIN).build();
I am marshalling an object into JSON that has JSON in a String property (String actionsJSON). This was causing that JSON to be escaped, so I am using Jackson annotation #JsonRawValue to get the JSON in that property not be escaped. The problem is that when I unmarshal it back into a String property (the reverse process) Jackson processes the JSON (which has a JSON array) and throws an error as the java class property is just a String:
Can not deserialize instance of java.lang.String out of START_ARRAY token
How can I make Jackson just copy the content of that property without trying to process it so I have JSON code inside the String property as I had in the original object?
(I have tried #JsonRawValue in the target class, ... #JsonSerialize(using = ToStringSerializer.class, as = StringSerializer.class) but error is still there.
You should look at JsonDeserlalize and not JsonSerialize.
A nice example at Deserialize JSON to string in raw format using Jackson
And more detailed information at http://www.baeldung.com/jackson-annotations
I am using FlexJson within my play framework application but at the point I am trying to deseralize the json string it throws a java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean:
User user = new JSONDeserializer<User>()
.use(null, User.class).deserialize(body);
Body is the json string passed into the controller using standard jquery/ajax and
where User has the following boolean value declared:
public Boolean isCurrentUser;
Any ideas as to what I am doing wrong?
Thanks
In Json, Boolean is a type. Your JSon is:
{"user_id":"18","isCurrentUser":"true","title":"mr","description":"description"}
when it should be:
{"user_id":"18","isCurrentUser":true,"title":"mr","description":"description"}
Note that true is not a String, but a boolean. The parser fails because it finds a String instead of the expected boolean type. Fix the JSon generation to add a boolean, not a String.