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?
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.
Try
String jsonString = new JSONSerializer().exclude("*.class").deepSerialize(map);
deepSerialize method performs a deep serialization of the target instance. It will include all collections, maps, and arrays by default so includes are ignored except if you want to include something being excluded by an annotation.