Why serialization is not working as expected for the JSONObject inside an Object ?
class Random {
String name;
JSONObject json;
}
JSONObject json= new JSONObject();
json.put("key", "value");
Random random = new Random("name", json);
new ObjectMapper().writeValueAsString(); ->
Result produces : {"name":"name", {"empty":false,"mapType":"java.util.HashMap"}}
Expected result : {"name":"name", "json": {"key":"value"}}
How to resolve this behaviour ?
If you are planning to have only a Map with key values in the "json" field you can try this approach:
Instead of JSONObject keep a Map.
The code to serialize will use json.toMap()