Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson Deserialize Variable as Json String [duplicate]

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?

like image 393
kamaci Avatar asked Nov 15 '11 13:11

kamaci


2 Answers

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

like image 120
StaxMan Avatar answered Nov 20 '22 01:11

StaxMan


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.

like image 31
Programmer Bruce Avatar answered Nov 20 '22 01:11

Programmer Bruce