Reputation: 2890
I have the following JSON response that I am trying to convert to a Java POJO:
[
1393591550.6117706,
"TYPE",
"TAG",
"Message",
null
]
(The null at the end is supposed to be there.)
I am not sure how to deserialize this with Jackson (2.0). I am not too familiar with Json so I am not sure what to search. I have looked into keyless deserialization but there are lots of similar search terms and I can't find what I want.
The only idea I have is to use the getElements() of a JsonObject and manually map it to a log object (which is what this json is), but I'm wondering if there's a better way to do this, like @JsonElement(0) or something. The way I have my project setup, having to manually iterate over keyless entries is going to require some major reworking (or duplicate code).
Upvotes: 2
Views: 2151
Reputation: 116472
It is actually doable without custom deserializers, using something like this:
@JsonFormat(shape=JsonFormat.Shape.ARRAY)
@JsonPropertyOrder({ "value", "type", "tag", "mesage", "other" })
public class Data {
public double value;
public String type;
public String tag;
public String message;
// not sure what the null is to represent; needs to match to this property
public Object other;
}
and then you can read instances like so:
Data data = mapper.readValue(src, Data.class);
Upvotes: 4
Reputation: 121702
You can write a custom deserializer, and then add this annotation to your class:
@JsonDeserialize(using = MyCustomDeserializer.class)
In one of my projects, I do exactly that. A JSON Merge Patch is either an array or an object, so standard Jackson deserialization fails for me. The code for the deserializer is here.
As to serialization, make your class implement JsonSerializable
.
This has the advantage that you can {de,}serialize using a "vanilla" ObjectMapper
, without having to configure it with custom modules etc
Upvotes: 1
Reputation: 32748
What about writing a custom serializer / deserialize - with logic to handle NULLs? Check out this page from the docs:
http://wiki.fasterxml.com/JacksonHowToCustomSerializers
Upvotes: 1