Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hwding opened this issue Mar 5, 2024 · 0 comments

Comments

@hwding
Copy link

hwding commented Mar 5, 2024

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));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant