fastjson icon indicating copy to clipboard operation
fastjson copied to clipboard

Version 1.2.83 failed to deserialize certain value when its ref path contains dash

Open hwding opened this issue 2 years ago • 0 comments

Version: 1.2.83.

Steps to reproduce, notice that after serialize and deserialize, $.response is missing.

JSONObject responseJson = new JSONObject();
responseJson.put("something", "here");

JSONObject dashKeyJson = new JSONObject();
dashKeyJson.put("response", responseJson);

// tree map to make sure 'response' refers the object inside '70e4141c-b1dd-4956-a905-1c326a8b8e45'
Map<String, Object> parent = new TreeMap<>();
parent.put("70e4141c-b1dd-4956-a905-1c326a8b8e45", dashKeyJson);
parent.put("response", responseJson);

String jsonStr = JSONObject.toJSONString(parent);

// {"70e4141c-b1dd-4956-a905-1c326a8b8e45":{"response":{"something":"here"}},"response":{"$ref":"$.70e4141c\\-b1dd\\-4956\\-a905\\-1c326a8b8e45.response"}}
System.out.println(jsonStr);

JSONObject deserializedJson = JSONObject.parseObject(jsonStr);

// {"70e4141c-b1dd-4956-a905-1c326a8b8e45":{"response":{"something":"here"}}}
System.out.println(deserializedJson);

Since fastjson does not support dash in json path, it should be deserialized with ref as:

{"$ref":"$['70e4141c-b1dd-4956-a905-1c326a8b8e45'].response"}

This should fix the issue:

String a = "{\"70e4141c-b1dd-4956-a905-1c326a8b8e45\":{\"response\":{\"something\":\"here\"}},\"response\":{\"$ref\":\"$['70e4141c-b1dd-4956-a905-1c326a8b8e45'].response\"}}\n";
        
// {"response":{"something":"here"},"70e4141c-b1dd-4956-a905-1c326a8b8e45":{"response":{"something":"here"}}}
System.out.println(JSONObject.parseObject(a).toString(SerializerFeature.DisableCircularReferenceDetect));

hwding avatar Mar 05 '24 09:03 hwding