Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally deserialize an object based on JSON format

I am using Jackson for JSON parsing and I have the following JSON structure:

["foo", {"baz": []}, {"myList": ["a", "b", "c"]}]

I would like to ONLY serialize objects with property "myList" into

public class MyClass {
  List<String> myList;
}

where ["a", "b", "c"] is used as the value of myList. All other objects should just get turned into the default Map<String, Object> and everything else (arrays, strings, etc) should turn into whatever the defaults are.

Is there a way to do this?

like image 661
Sridatta Thatipamala Avatar asked Jun 16 '26 00:06

Sridatta Thatipamala


1 Answers

You can search the content tree for the "myList" element and append its values to "MyClass" if it's to be found; otherwise, simply redirect the content to the standard deserializer.

like image 97
noamt Avatar answered Jun 18 '26 13:06

noamt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!