27

I have a model like that:

private String message;
private Integer errorCode;    
private String data;

I get for example the following JSON from remote:

{"data": {"cat": "1.2.3.4", "ner": "80", "name": "pinta" }, "message" : "m", "errorCode" : 12}

When I deserialize this JSON, the message and errorCode variables gets the correct value. However I don't want to have the content of my data variable interpreted. Instead, I want it to be the following string:

{"cat": "1.2.3.4", "ner": "80", "name": "pinta" }

After that, I will interpret it myself. How can I get this value of data?

oberlies
  • 11,503
  • 4
  • 63
  • 110
kamaci
  • 72,915
  • 69
  • 228
  • 366
  • Could you make it more clear? – MByD Nov 15 '11 at 13:31
  • @MByD When I debug my variables after deserialization I want to see that my data variable has that value: `{"cat": "1.2.3.4", "ner": "80", "name": "pinta" }` because I send it like that: `"data":{"cat": "1.2.3.4", "ner": "80", "name": "pinta" }` – kamaci Nov 15 '11 at 13:37
  • 1
    This question is not a duplicate of https://stackoverflow.com/q/4783421/1523648. This question is a *deserialization* question looking for `@JsonRawValue`, whereas the other question already knows about `@JsonRawValue` and just wonders why it doesn't work during *serialization*. – oberlies Sep 05 '19 at 15:42

2 Answers2

15

I would rather suggest that you do let data be bound to an intermediate object; usually this is either java.util.Map or org.codehaus.jackson.JsonNode (JSON Tree). And then you can access data any way you want; including easily converting to a POJO using ObjectMapper.convertValue(inputPojo, outputType)).

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • 1
    thanks for your answer and voting up. Just want to make it clear: I use that `myObject = mapper.readValue(data, clz);` what is the difference between `transferredObject = mapper.convertValue(data, clz);`? – kamaci Nov 16 '11 at 21:25
  • 1
    readValue() parses content from given source, whereas convertValue() is equivalent to writing value, reading it back as given type -- it can convert anything that can be serialized as JSON, into anything that can be bound from JSON. – StaxMan Nov 17 '11 at 03:36
  • 1
    +1 Best answer for this problem. Keeping the type as JsonNode makes it able to just call toString() and get the String json value. Of course it first gets deserialized, and then serialized again, which is a bit wasteful, but so far, this is the only way to do it nice and clean – Rasive Oct 20 '16 at 08:54
  • Faulty when expecting List of objects on success and Error object on fail – Georgian Benetatos May 05 '17 at 14:22
  • @Rasive no need to serialize in-between: `convertValue()` actually avoids that step, only creating token stream to re-process. This can be done with any object model, but `JsonNode` is usually most natural. – StaxMan May 07 '17 at 04:15
11

Jackson issue 596 was created for the desired functionality described in the original question. Vote for it if you want it implemented.

The current solution available is to implement custom deserialization processing.

Also, the How can I include raw JSON in an object using Jackson? thread covers this topic.

Community
  • 1
  • 1
Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97