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

JSONObject(Map) handling of null keys #39

Closed
jwagenleitner opened this issue Jan 5, 2012 · 2 comments
Closed

JSONObject(Map) handling of null keys #39

jwagenleitner opened this issue Jan 5, 2012 · 2 comments

Comments

@jwagenleitner
Copy link

The JSONObject's map based constructor does not throw an exception when the Map contains null keys.

The following will fail the NotNull check.

        HashMap m = new HashMap();
        m.put("foo", "bar");
        m.put(null, "oops");

        JSONObject json = new JSONObject(m);

        assertNotNull(json.toString());
        assertEquals("{\"foo\":\"bar\"}", json.toString());

And the following will fail the Equals check:

        HashMap mFoo = new HashMap();
        mFoo.put("one", "two");
        mFoo.put(null, "three");

        HashMap m = new HashMap();
        m.put("foo", mFoo);

        JSONObject json = new JSONObject(m);

        assertNotNull(json.toString());
        assertEquals("{\"foo\":{\"one\":\"two\"}}", json.toString());  // actual is {"foo":null}

I am interested to know if the current behavior is by design or if an exception is meant to be thrown since the javadocs for the constructor says it throws a JSONException but it doesn't declare that it throws it.

@douglascrockford
Copy link
Contributor

The HashMap constructor is provided as a convenience. It assumes that you know what you are doing.

@jwagenleitner
Copy link
Author

That was my suspicion, thanks for the clarification.

BGehrels pushed a commit to BGehrels/JSON-java that referenced this issue Apr 29, 2020
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