I tried to serialize and deseriliaze LinkedHashMap data as follow:
LinkedHashMap<String, Object> o = new LinkedHashMap<String, Object>();
o.put("1", "a");
o.put("2", "b");
o.put("3", "c");
o.put("4", new BigDecimal("9999999999999999999999.00999999999999999999999"));
String serialize = new JSONSerializer().deepSerialize(o);
System.out.println("serialize" + serialize);
LinkedHashMap deserialize = new JSONDeserializer<LinkedHashMap>().deserialize(serialize, LinkedHashMap.class);
System.out.println("deserialize:" + deserialize);
and I get ClassCastException:
serialize{"1":"a","2":"b","3":"c","4":9999999999999999999999.00999999999999999999999}
Exception in thread "main" java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.LinkedHashMap
at com.JSONUtil.main(JSONUtil.java:161)
So I tried slightly different approach when deserializing as follow:
HashMap deserialize = new JSONDeserializer<HashMap>().deserialize(serialize, HashMap.class);
System.out.println("deserialize:" + deserialize);
This time I get result but the Map is unordered (I think because it is now using HashMap). In addition, the BigDecimal value is not displayed like the original format.
deserialize:{3=c, 2=b, 1=a, 4=1.0E22}
It appears that FlexJSON has issue with sorted Map. I guess this is because it has no way to put 'class' entry like the example below, eg.
"class":"ch.qos.logback.classic.Logger","debugEnabled":true,"errorEnabled":true,"infoEnabled":true}
or perhaps there is another way to do this. Can anyone please help? Thank you
After introducing ObjectFactory as suggested by chubbsondubs:
LinkedHashMap deserialize = new JSONDeserializer<LinkedHashMap>()
.use(LinkedHashMap.class, new ObjectFactory() {
public Object instantiate(ObjectBinder context, Object value, Type targetType, Class targetClass) {
System.out.println("mymap:"value);
}
}).deserialize(serialize, LinkedHashMap.class);
The value object has HashMap type and it prints:
mymap:{3=c, 2=b, 1=a, 4=flexjson.JsonNumber@104fc23}