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
Related
I'm declaring FeignClient with Spring Cloud OpenFeign version 4.0.1. The API I'm calling expects in request body primitive JSON string value (e.g. "something") as a string in quotes. But I'm struggling with implementing it using Feign.
Code sample:
#FeignClient(value = "client", url = "https://${example.host}:${example.port}/api/2/", dismiss404 = true)
public interface ExampleClient {
#GetMapping(value = "/path/to/resource", produces = MediaType.APPLICATION_JSON_VALUE)
String getResourceValue();
#PutMapping(value = "/path/to/resource", consumes = MediaType.APPLICATION_JSON_VALUE)
void setResourceValue(#RequestBody String hostname);
}
The code just serializes the string to body as plain text without adding quotes (it is even stripping them). In deserialization it returns the string including quotes and it is not removing them. I know this is intended behavior but how could I achieve that client will serialize and deserialize simple String value as valid JSON?
The only workaround I was able to find was to replace String type by Jackson TextNode class. But it is not very elegant solution. It was also possible by setting JacksonEncoder instead of Spring implementation but then I loose other features.
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 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.
I've got the following JSON string which I have retrieved from a webapi:
[{"WorkOrderID":2,"WorkOrderNumber":1},{"WorkOrderID":3,"WorkOrderNumber":3}]
Now I want to know if there is a way I can deserialize this into a list of type dynamic in VB.NET?
The JSON string could be coming in any format (therefore I cannot deserialize it to a standard class type) and I just need to deserialize it into a list so it can be used a datasource for a grid.
Is this achievable.
For VB.Net:
Dim x As Object = JsonConvert.DeserializeObject(of Object)(json)
The Client side receives a formal JSON content "{\"Id\":[1,2,3],\"Size\":56}", but get an error in deserialization the byte array.
1 Error occurs in the statement below
IRestResponse<key> response = client.Execute<key>(request);
2 Error message is "No parameterless constructor defined for this object."
3 The object class in client size is the same as it's in server side:
public class key
{
public byte[] id { get; set; }
public int Size { set; get; }
}
4 I've tried passing object that contains string and integer by JSON format and that's all fine but byte array.
JsonDeserializer from RestSharp can not deserialize array. Instead of byte[] use List<byte>. For more information see https://github.com/restsharp/RestSharp/wiki/Deserialization
I have run into this issue, too. My solution was to use RestSharp to perform a raw execute and use Json.NET to deserialize the result:
var response = client.Execute(request);
var keyResponse = JsonConvert.DeserializeObject<key>(response.Content);
keyResponse should now be an instance of your key class deserialized from the JSON content.
In addition to Chris Hogan's reply, I'd like to point out that I got this error when RestSharp incorrectly used the default serializer instead of the custom JSON.NET serializer I had assigned.
The reason for this was that I added a handler with content type application/json whereas the API I was getting the response from returned the content as text/json.
So by changing the AddHandler call to AddHandler("text/json", jsonDeserializer), I resolved the issue.