31

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?

3
  • Could you make it more clear? Commented Nov 15, 2011 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" } Commented Nov 15, 2011 at 13:37
  • 1
    This question is not a duplicate of 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. Commented Sep 5, 2019 at 15:42

2 Answers 2

16

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)).

Sign up to request clarification or add additional context in comments.

5 Comments

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);?
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.
+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
Faulty when expecting List of objects on success and Error object on fail
@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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.