0

I use Jackon to convert simple data classes to Map with

new ObjectMapper().convert(map, MyData.class); 
// or vice versa
new ObjectMapper().convert(myData, new TypeReference<Map<String, String>>() {});

Now I have the requirement that the Map should contain the String "--" as value whenever the data object has a NULL value and I already have two methods that iterate over the Map entries and swap the values.

Is there a Jackson annotation that would call those convertes automatically?

I already tried @JsonSerialize(converter=…) and @JsonDeserialize(converter=…) but it did not seem to work. Would that have been the right approach or is there a better one?

lathspell
  • 3,040
  • 1
  • 30
  • 49
  • you mean `--` as value? – Ryuzaki L Jul 09 '19 at 15:34
  • See [How do you override the null serializer in Jackson 2.0?](https://stackoverflow.com/questions/18101506/how-do-you-override-the-null-serializer-in-jackson-2-0) and [JSON Jackson custom deserializer to return empty String instead of null](http://apieceofmycode.blogspot.com/2015/05/json-jackson-custom-deserializer-to.html). Do you want to handle any type or only `String` properties? – Michał Ziober Jul 09 '19 at 19:39
  • @Deadpool: sorry my above sentence was broken: all NULL values from the Object have to be converted into "--" Map values. – lathspell Jul 09 '19 at 20:00
  • @MichałZiober : I also have to convert the other way around, from "--" in the Map to NULL in the Object. – lathspell Jul 09 '19 at 20:01
  • You try to convert `MyData` to `Map`. Does it mean that all properties in `MyData` class are `String`? – Michał Ziober Jul 09 '19 at 21:00
  • MyObject has String, Integer, Double properties. But no nested structures. – lathspell Jul 10 '19 at 02:19

0 Answers0