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

Structure validation: duplicate key #37

Open
GeTOUO opened this issue Jan 8, 2024 · 1 comment
Open

Structure validation: duplicate key #37

GeTOUO opened this issue Jan 8, 2024 · 1 comment

Comments

@GeTOUO
Copy link

GeTOUO commented Jan 8, 2024

If there are duplicate keys in the JSON, the current JSON parser will take the value corresponding to the first occurrence of the key:

inputString:

public class SimdJsonTest {

    static final SimdJsonParser SIMD_PARSER = new SimdJsonParser();
    static final ObjectMapper JACKSON = new ObjectMapper();
    static final Gson GSON = new GsonBuilder().create();

    public static void main(String[] args) throws IOException {

        String json = "{\"num\": 2, \"num\": 123}";
        byte[] bytes = json.getBytes();
        JsonValue value = SIMD_PARSER.parse(bytes, bytes.length);

        System.err.println("[simd].size = " + value.getSize());   // 2
        System.err.println("[simd].num = " + value.get("num"));  // 2

        Map jcsObj = JACKSON.readValue(bytes, Map.class);
        System.err.println("[jackson].size = " + jcsObj.size());   // 1
        System.err.println("[jackson].num = " + jcsObj.get("num"));   // 123

        // will throw exception: com.google.gson.JsonSyntaxException: duplicate key: num
        Map gsonObj = GSON.fromJson(new String(bytes), Map.class);
        System.err.println("[gson].size = " + gsonObj.size());
        System.err.println("[gson].num = " + gsonObj.get("num"));
    }
}

The content of the console output:

[simd].size = 2
[simd].num = 2
[jackson].size = 1
[jackson].num = 123
Exception in thread "main" com.google.gson.JsonSyntaxException: duplicate key: num
...

@piotrrzysko
Copy link
Member

Thanks for reporting this. This is definitely something that needs to be documented at least. I'm not sure, though, how this situation should be handled. The JSON RFC doesn't specify this: https://datatracker.ietf.org/doc/html/rfc7159#section-4.

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

2 participants