Skip to content
Advertisement

FasterXml – JsonSerializer HashMap

I’m using jackson-databind version 2.12.3 to serialize the return of an object that should return like this:

JavaScript

My classes are these:

JavaScript
JavaScript
JavaScript

The return is myObject:

JavaScript

However with the default ObjectMapper the “link.setField” is ignored and the returned json is:

JavaScript

I tried doing some tests with JsonSerializer but couldn’t do something generic for all classes that extend HashMap (these classes are generated from BerlinGroup’s PSD2 YAML, so I wouldn’t want to change the generated class).

Is there a generic way to do it, or should I make a serialize class for each class that extends the HashMap?

Advertisement

Answer

based on this answer I developed this generic method of making for all objects that extend a Map:

JavaScript

which goes through all fields, when I find one that extends from a Map I go through all the fields of this one and add it to the Map ignoring the object’s fields, so the Serializer works perfectly.

EDIT: to Deserializer properly I do this:

JavaScript
Advertisement