I am attempting to parse HashMap data into POJO using
Object parsedMessage = objectMapper.convertValue(receivedMessage, destinationClass);
where receivedMessage is a HashMap (earlier parsed from JSON) and containing fields of various types - Integer, Boolean, String, LinkedHashMap.
I have defined destinationClass so that it contains some of the fields found in HashMap keys, with exactly the same names as the keys (case-sensitive).
The instruction executes without an exception but all the fields in parsedMessage are null. What could be the reason? The similar instructions work just fine in other place of code.
I have solved it.
Turns out, regardless how your Java fields in destinationClass are named, Jackson (at least, by default) assumes that it is named in lowerCamelCase. So if your hashmap has "YourField" and your POJO has the same "YourField", then it will NOT work if you do not use lowerCamelCase from the start on both ends! (and if you parse serialized .NET JSON, you will end up with UpperCamelCase).
Because Jackson assumes that your POJO's "YourField" is named "yourField" for matching!
To fix this, either:
your HashMap keys must be lowerCamelCase.
OR
Create Mapper with case-insensitive behaviour
ObjectMapper om = new ObjectMapper();
om.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
return om;
OR
for clarity you can use annottaions in POJO (https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations)
@JsonProperty("YourField")
private String YourField;
NOTE: if you want to do some sort of "Tolerant Reader" (http://martinfowler.com/bliki/TolerantReader.html) with partial parsing, you may find class-based annotation @JsonIgnoreProperties(ignoreUnknown = true) so you will not get exceptions if you are trying to parse the field that you did not described.