Can the type field be serialized to yield a consistent output when using net.sf.json.JSONObject
for data processing, as compared to when using a Map?
@Test
public void testJsonObject() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
List<String> type = Lists.newArrayList("A", "B");
JSONObject jsonObject = new JSONObject();
jsonObject.put("type", objectMapper.writeValueAsString(type));
System.out.println(objectMapper.writeValueAsString(jsonObject));
Map<String, Object> map = new HashMap<>();
map.put("type", objectMapper.writeValueAsString(type));
System.out.println(objectMapper.writeValueAsString(map));
}
Output:
{"type":["A","B"]}
{"type":"["A","B"]"}
Serialize the type twice:
jsonObject.put("type", objectMapper.writeValueAsString(objectMapper.writeValueAsString(type)));
Output:
{"type":"[\"A\",\"B\"]"}
{"type":"["A","B"]"}
The output differs between utilizing a Map and performing the serialization twice. With a Map, the type can be directly deserialized into an array of strings. However, the output from a double serialization process cannot be directly deserialized into an array of strings.
1