Java Map Deserialization with Jackson - java

I have a simple POJO like
public class Employee
{
int level;
int salary;
Map<String, String> details; // HashMap
}
A serialized object of this class looks like
{"level":1,"salary":30000, "details":{"address":"ADDRESS", "phone":"12345678"}}
Assuming the above JSON string is stored in a Java String variable called json,
when deserializing it via the following Jackson statement
Employee employee = new ObjectMapper().readValue(json, Employee.class);
the object is properly created, no exception occurs, the fields "level" and "salary" are correctly populated, but the "details" field (originally a HashMap) is always null.
How can I correctly deserialize it?

Jackson correctly deserialized the details attribute with the version of Jackson that I have in my machine-1.8.1. Can you confirm that you have accessors created for Employee class, if not that could be the reason.

Related

Using the instantiated collection type for Jackson deserialization instead of initializing a new one?

If I have a class
class DTO {
final MySet<Types> values = MySetWrapper(EnumSet.of(Types.class));
public MySet getValues() {
return values;
}
}
where MySet extends Set. Jackson complains that
Cannot find a deserializer for non-concrete Collection type MySet
which I understand, but I already instantiated the collection. What I want is for jackson to just call add for each value after it created an instance, something like:
DTO o = new DTO();
MySet<Types> values = o.getValues();
for (Types type : jsonArray) {
values.add(type );
}
I don't want it to try to create a new collection itself.
That error message means that the DTO class is configured (by default or explicitly) to deserialize the values part of the JSON input into the DTO values field of DTO :
Cannot find a deserializer for non-concrete Collection type MySet
If you consider that Jackson should not perform the deserialization directly on this field, you could define a constructor to set values and also make sure that Jackson will not perform automatically the deserialization work : to achieve it, remove setter for that field (or add #JsonIgnore on it) and any jackson module that will use reflection to deserialize to fields.
It would give :
final MySet<Types> values = MySetWrapper(EnumSet.of(Types.class));
#JsonCreator
public MyFoo(Set<Types> values) {
this.values.addAll(values);
}
Note that I specified in the constructor Set and not MySet (should not be an issue as interface doesn't declare fields), otherwise you would get the same issue since you didn't define a deserializer for MySet.
But if you implement a deserializer for that you could of course do :
public MyFoo(MySet<Types> values) {
this.values.addAll(values);
}
Found an answer using #JsonProperty:
#JsonProperty
private void setValues(Set<Types> types) {
values.addAll(types);
}
Pretty short and simple thankfully.
Edit: seems like you don't even need the annotation.

Wrapping Json fields into instance variable of a pojo

i am trying to map certain json fields to a class instance variable.
My sample Person class looks like:
public class Person {
private String name;
private Address address;
//many more fields
//getters and setters
}
The sample Address class is:
public class Address {
private String street;
private String city;
//many more fields
// getters and setters
}
The json object to be deserialized to my Person class doesn't contain "address" field. It looks like:
{
"name":"Alexander",
"street":"abc 12",
"city":"London"
}
Is there a way to deserialize the json to the Person pojo where the Address fields are also mapped properly?
I have used a custom Address deserializer as mentioned in so many posts here. However, it's not being called as the Json object doesn't contain "address" field.
I had resolved this problem by mapping each field manually using JsonNode, however in my real project, it's not a nice solution.
Is there any work around for such problem using jackson?
Plus if this question has been asked before then apologies on my behalf as as i have intensively searched for the solution and might have not seen it yet. .
#JsonUnwrapped annotation was introduced for this problem. Model:
class Person {
private String name;
#JsonUnwrapped
private Address address;
// getters, setters, toString
}
class Address {
private String street;
private String city;
// getters, setters, toString
}
Usage:
ObjectMapper mapper = new ObjectMapper();
String json = "{\"name\":\"Alexander\",\"street\":\"abc 12\",\"city\":\"London\"}";
System.out.println(mapper.readValue(json, Person.class));
Prints:
Person{name='Alexander', address=Address{street='abc 12', city='London'}}
For more info read:
Jackson Annotation Examples
Annotation Type JsonUnwrapped
Jackson JSON - Using #JsonUnwrapped to serialize/deserialize properties as flattening data structure
I don't think you really have a deserialization problem here but rather a general Java problem: how to make sure the address field always contains a value. All you need to do is either assign address to a default value in the Person constructor, or generate and assign a default value for address in the Person.getAddress method.
I understood your problem so that it is about flat Json that has all Address fields at the same level as Person. Even if it is not exactly so this might help you. JsonDeserializer will do fine but you need to apply it to Person because it is the level where all the fields are.
So like this:
public class CustomDeserializer extends JsonDeserializer<Person> {
// need to use separate ObjectMapper to prevent recursion
// this om will not be registered with this custom deserializer
private final ObjectMapper om;
{
om = new ObjectMapper();
// this is needed because flat json contains unknown fields
// for both types.
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
#Override
public Person deserialize(JsonParser parser, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
// make a string of json tree so not any particular object
String json = om.readTree(parser).toString();
// deserialize it as person (ignoring unknown fields)
Person person = om.readValue(json, Person.class);
// set address deserializing it from teh same string, same manner
person.setAddress(om.readValue(json, Address.class));
return person;
}
}
Of course this is not the only way and might not have the best performance but it is only about how you do the deserialization in your custom deserializer. If your Person & Address objects are havin like 10 fields each using this should not be a problem.
Update
I think that in your case - based on your example data - MichaƂ Ziober's
answer might be the best but if you need any more complex handling than plain unwrapping for your data you just need to deserialize Person class somehow like I presented.

deserialization with Jackson: get list of fields set by Json object

I would like to know after the deserialization with Jackson what fields where set by the Json input (even null), so I can than distinguish the null fields than where set on null from the one that where not specified in Json.
This question comes after my previous one about BeanDeserializerModifier.
public class Dto {
public Collection<String> deserializedFields;
// or even better a collection of reflection fields of the object.
}
public MyFooDto extends Dto {
public Integer myField1;
#PossiblySomeJacksonAnnotation (include, exclude, map on other name, special deserializer, etc...)
public SomeDatatype myField2;
}
Example: by deserializing {"myField1": null} I would like to have deserializedFields = ["myField1"], and by deserializing {} I would like to have deserializedFields = [].
I already tried within a custom deserializer and a BeanDeserializerModifier, but still I cant intercept the list of fields inside the Json object (or if I do so it already consumates the JsonParser and it can't be deserialized then).
In the best case I would also get the reflection list of the MyFooDto Fields that have been set...
Do you see how I could proceed?
Thank you Community!
The most straightforward way is to add code in each setter to add the currently set variable name to a List. E.g. :
public class Dto {
public List<String> deserializedFields = new ArrayList<>();
}
and inside MyFooDto setters like:
public void setMyField1(Integer myField1) {
deserializedFields.add("myField1");
this.myField1 = myField1;
}
That's a lot of work if there are hundreds of such setters. An alternative for such a case is to parse JSON into a tree first, traverse it to get JSON property names to add in a collection and then convert the tree to MyFooDto. E.g. (assuming you have a ObjectMapper mapper and json below is a String with your example JSON):
ObjectNode tree = (ObjectNode) mapper.readTree(json);
ArrayNode deserializedFields = mapper.createArrayNode();
tree.fields().forEachRemaining(e -> deserializedFields.add(e.getKey()));
tree.put("deserializedFields", deserializedFields);
MyFooDto dto = mapper.treeToValue(tree, MyFooDto.class);

Jackson mapping: Deserialization of JSON with different property names

I have a server that returns a json string:
{"pId": "ChIJ2Vn0h5wOlR4RsOSteUYYM6g"}
Now, I can use jackson to deserialize it into an object with the variable called pId, but I don't want the variable to be called pId, I would rather deserialize it to placeId.
Current object in android java:
public class Place {
private String pId;
}
What I want the object to look like:
public class Place {
private String placeId;
}
If I change the object's variable to placeId, jackson will not be able to deserialize the JSON as the property names no longer matches.
Is there a jackson annotation I can used to map the "placeId" variable in the java object to the JSON string variable "pId" returned back from the server?
Use #JsonProperty annotation:
public class Place {
#JsonProperty("pId")
private String placeId;
}
For more information you can see the related javadoc.

Jackson Data-Binding with Heterogeneous Json Object

I'm calling a rest service that returns a json object. I'm trying to deserialize the responses to my Java Beans using Jackson and data-binding.
The example Json is something like this:
{
detail1: { property1:value1, property2:value2},
detail2: { property1:value1, property2:value2},
otherObject: {prop3:value1, prop4:[val1, val2, val3]}
}
Essentially, detail1 and detail2 are of the same structure, and thus can be represented by a single class type, whereas OtherObject is of another type.
Currently, I've set up my classes as follows (this is the structure I would prefer):
class ServiceResponse {
private Map<String, Detail> detailMap;
private OtherObject otherObject;
// getters and setters
}
class Detail {
private String property1;
private String property2;
// getters and setters
}
class OtherObject {
private String prop3;
private List<String> prop4;
// getters and setters
}
Then, just do:
String response = <call service and get json response>
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(response, ServiceResponse.class)
The problem is I'm getting lost reading through the documentation about how to configure the mappings and annotations correctly to get the structure that I want. I'd like detail1, detail2 to create Detail classes, and otherObject to create an OtherObject class.
However, I also want the detail classes to be stored in a map, so that they can be easily distinguished and retrieved, and also the fact that the service in in the future will return detail3, detail4, etc. (i.e., the Map in ServiceResponse would look like
"{detail1:Detail object, detail2:Detail object, ...}).
How should these classes be annotated? Or, perhaps there's a better way to structure my classes to fit this JSON model? Appreciate any help.
Simply use #JsonAnySetter on a 2-args method in ServiceResponse, like so:
#JsonAnySetter
public void anySet(String key, Detail value) {
detailMap.put(key, value);
}
Mind you that you can only have one "property" with #JsonAnySetter as it's a fallback for unknown properties. Note that the javadocs of JsonAnySetter is incorrect, as it states that it should be applied to 1-arg methods; you can always open a minor bug in Jackson ;)

Categories