2

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();
    }
}
Bitman
  • 1,996
  • 1
  • 15
  • 18

2 Answers2

1

I think, that you should use JsonNode class as a type of jsonObjectIDontWantToParseproperty:

class Jack {

    private String lastName;
    private JsonNode jsonObjectIDontWantToParse;

    // getters, setters, toString, other

    public String getJsonObjectIDontWantToParseAsString() {
        return jsonObjectIDontWantToParse.toString();
    }
}

You can add one additional method getJsonObjectIDontWantToParseAsStringif you want to get Stringrepresentation.

Example usage:

ObjectMapper mapper = new ObjectMapper();
Jack jack = mapper.readValue(json, Jack.class);
System.out.println(jack);
System.out.println(jack.getJsonObjectIDontWantToParseAsString());

Above program prints:

lastName=Bitman, jsonObjectIDontWantToParse={"somefield":1234}
{"somefield":1234}
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • Works fine if you use java class in one project. But if you have more projects and your class in jar you'll need to include jackson jar everywhere. Also this is kind of the same that using Object. JsonNode has LinkedHashMap inside and string is being parsed to fit that map. I wonder whether there is the way to skip parsing and just have String field. Thanks anyway. Well done. – Bitman Apr 15 '14 at 18:59
  • I think, that this is the easiest way.You can use `Map` as you mentioned. If you really want to skip serializing you have to write custom deserializer and concatenate tokens. – Michał Ziober Apr 15 '14 at 19:15
  • Actually JsonNode is using map. I'll use Object for now. But I'd like to have the ability skip parsing for performance and memory matters – Bitman Apr 15 '14 at 19:21
-1

To get to this:

lastName=Bitman, jsonObjectIDontWantToParse={"somefield":1234}

You just need to escape the String representation of jsonObjectIDontWantToParse. That means change it read:

jsonObjectIDontWantToParse: "{ \"somefield\": 1234 }"
Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83