1
It is possible to serialize a Custom Object for JSON without having to insert field-by-field, for example:
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", meuObjeto.id);
jsonObject.put("dt_sync", meuObjeto.dtSync);
jsonObject.put("dt_modif", meuObjeto.dtModif);
I tried to pass the object by parameter in the Jsonobject constructor
JSONObject jsonPedido = new JSONObject(pedido);
But some values are lost.
In Java, Android etc.. it is possible to use lib JACKSON for example to generate an Object Map and convert it to a JSON... Or via Annotations.. Anyway..
There is a more agile way to implement this Custom Object serialization for a Jsonobject in Totalcross?
This constructor expects there to be
getters
in the object being serialized. It has how to share a complete, verifiable and minimal example?– Jefferson Quesado
This comment already explains the failure of the attempt by using the Jsonobject constructor. For our false-POJOS have no getters and setters methods..
– Gustavo Bitencourt
Just in case.. isn’t there a class that does the opposite of Jsonfactory? Convert a Java Object to Json Object? In this case it would be through the constructor, as it would use getters methods through Reflection to popular a Json Object.. that?
– Gustavo Bitencourt
So it doesn’t work for serialization via the
JSONObject
.– Jefferson Quesado