Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serialize Map<String, List<Object>> with FlexJSON

I have a object which I want to serialize to JSON. This object is a map with list of a specific objects. This looks similar to it:

Map<String, List<Object>> map = new Map<String, List<Object>>();

I am using FlexJSON. I can work only with flexjson.JSONSerializer. There are my tries:

JSONSerializer jsonSerializer = new JSONSerializer();
//        jsonSerializer.include("map");
//        jsonSerializer.include("map.myList.object");
jsonSerializer.transform(new MapTransformer(), "map");
jsonSerializer.exclude("*.class");

As you can see I am trying now with Transformer classes but I haven't succeeded. The commented lines will work, as I suppose, if my List<Object> has own name for example myList. But it doesn't haven own name, because it is values of my map.

How can I serialize such object to JSON with FlexJSON?

like image 882
woyaru Avatar asked Sep 21 '26 10:09

woyaru


1 Answers

Quoi is correct, and most likely the answer you want to use. However, just so you can learn more about how it works you could do this:

new JSONSerializer().include("values.values").serialize( myMap );

Maps and Lists are containers and have special expressions for the elements contained within them. When you want to talk about the path of the values of a container you use "values" expression in the path. Map's two special expressions "keys" and "values". Other Collections have "values".

Another option is to use wildcards like so:

   new JSONSerializer().include("values.*").serialize( myMap );

You can see wildcards used in Quoi's answer as well to exclude the class property.

like image 120
chubbsondubs Avatar answered Sep 23 '26 01:09

chubbsondubs



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!