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

[BUG] Abnormal syntaxError #2528

Closed
Cooper-Zhong opened this issue May 6, 2024 · 1 comment
Closed

[BUG] Abnormal syntaxError #2528

Cooper-Zhong opened this issue May 6, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@Cooper-Zhong
Copy link

问题描述

Based on #1269,以下的代码在Mac上使用fastjson2-2.0.49中正常运行,在cloned repository源码中报错。

环境信息

Environment of cloned source code:

  • OS信息: [Ubuntu 22.04.3 LTS (GNU/Linux 6.2.0-36-generic x86_64)]
  • JDK信息: [Openjdk 1.8.0_382]
  • 版本信息:[Fastjson2-2.0.50-SNAPSHOT] (perhaps?)

重现步骤

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;

import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.assertEquals;


public class Issue1269_Mutated_0 {
    @Test
    public void test() throws JSONException {
        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(1, 1);
        map.put(2, 2);

        JavaObj o = new JavaObj(map);
        byte[] bt = JSON.toJSONBytes(o, JSONWriter.Feature.PrettyFormat); // changed from WriteNonStringKeyAsString to PrettyFormat
        JSONObject jsonObj = JSON.parseObject(bt);
        JSONAssert.assertEquals("{\"map\":{\"1\":1,\"2\":2}}", jsonObj.toString(), true);

        o = JSON.parseObject(bt, JavaObj.class); // changed from JSONReader.Feature.FieldBased to direct parsing
        assertEquals(1, o.getMap().get(1)); // changed to use direct parsing
    }

    static class JavaObj {
        HashMap<Integer, Integer> map = new HashMap<>();

        public JavaObj() {
        }

        public JavaObj(HashMap<Integer, Integer> map) {
            super();
            this.map = map;
        }

        public HashMap<Integer, Integer> getMap() {
            return map;
        }

        public void setMap(HashMap<Integer, Integer> map) {
            this.map = map;
        }
    }
}

期待的正确结果

相关日志输出

org.json.JSONException: Names must be strings, but 1 is of type java.lang.Integer at character 9 of {"map":{1:1,2:2}}
at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
at org.json.JSONTokener.readObject(JSONTokener.java:367)
at org.json.JSONTokener.nextValue(JSONTokener.java:100)
at org.json.JSONTokener.readObject(JSONTokener.java:385)
at org.json.JSONTokener.nextValue(JSONTokener.java:100)
at org.json.JSONObject.(JSONObject.java:155)
at org.json.JSONObject.(JSONObject.java:172)
at org.skyscreamer.jsonassert.JSONParser.parseJSON(JSONParser.java:43)
at org.skyscreamer.jsonassert.JSONCompare.compareJSON(JSONCompare.java:50)
at org.skyscreamer.jsonassert.JSONCompare.compareJSON(JSONCompare.java:125)
at org.skyscreamer.jsonassert.JSONAssert.assertEquals(JSONAssert.java:415)
at org.skyscreamer.jsonassert.JSONAssert.assertEquals(JSONAssert.java:394)
at org.skyscreamer.jsonassert.JSONAssert.assertEquals(JSONAssert.java:336)
at com.alibaba.fastjson2.ours.pass.Issue1269_Mutated_0.test(Issue1269_Mutated_0.java:25)
at java.lang.reflect.Method.invoke(Method.java:498)
at java.util.ArrayList.forEach(ArrayList.java:1259)
at java.util.ArrayList.forEach(ArrayList.java:1259)

@Cooper-Zhong Cooper-Zhong added the bug Something isn't working label May 6, 2024
@Cooper-Zhong
Copy link
Author

这个例子应该没有问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant