i have something like JSON-RPC client, and i´m having trouble deserializing incoming json string into my java object. The incoming json format is:
{"value":"xxxx","type":"xxxx"}
The object i want to deserialize to:
@JsonAutoDetect
@JsonDeserialize()
public class ReturnValue {
private Object value;
private String type;
@JsonCreator
public ReturnValue(@JsonProperty("value") String val, @JsonProperty("type") String type) {
value = val;
this.type = type;
}
...getters, setters...
This seems to work ok, if the value is String, but if it´s of array type, it throws JsonMapping Exception - Can not deserialize instance of java.lang.String out of START_ARRAY token for the json like this:
{\"value\":[8, 10], \"type\":\"[int]\"}
The code is:
int[] arr = (int[])getReturnValue(jsonString).getValue();
Where getReturnValue is nothing special:
ObjectMapper om = new ObjectMapper();
ReturnValue rv = null;
rv = om.readValue(json, ReturnValue.class);
return rv;
The another problem is that i would want the type property to be of Class type, but this would throw another mapping exception. Is there any way in Jackson to do it, or do i have to convert from string to appropriate class myself. Thank you for any advice.
Change your constructor to be:
@JsonCreator
public ReturnValue(@JsonProperty("value") Object val, @JsonProperty("type") String type) {
since, like error points out, it does not know how to make a String out of array. But both String and JSON Array can be converted to Object; if so, it'll be Java String, or Java List (for JSON arrays), or Java Map (for JSON objects).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With