I have JSON object and I don't want to parse some of nested objects. Instead I want them as String (I have my reasons). Is there an easy way to do it? I've tried to implement custom Deserializer but I found it complicated in different ways (I don't want to concatenate tokens I just need whole object. It also doesn't consider ':' as token so it need special handling) or maybe I'm missing smth. Also putting quotes in json is not an option. I need that json the way it is.
JSON example:
{
"lastName":"Bitman",
"jsonObjectIDontWantToParse":{
"somefield":1234
}
}
Java object I want to parse json to.
public class Jack {
public String lastName;
public String jsonObjectIDontWantToParse;
@Override
public String toString() {
return "lastName=" + lastName + ", jsonObjectIDontWantToParse=" + jsonObjectIDontWantToParse;
}
}
Here's my main class
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
final String jackString = "{\"lastName\":\"Bitman\",\"jsonObjectIDontWantToParse\":{\"somefield\":1234}}";
Jack user = mapper.readValue(jackString, Jack.class);
System.out.println(user);
}
I expect output to be like this
lastName=Bitman, jsonObjectIDontWantToParse={"somefield":1234}
Updated: So basically this is example what I'm looking for (except There are no such method). I want to skip any parsing for that node...
public class LeaveAsStringDeserializer extends JsonDeserializer<String> {
@Override
public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
return jp.getWholeObject().toString();
}
}