douglascrockford/JSON-java — Issue #39: JSONObject(Map) handling of null keys
Author: jwagenleitner
Created: 2012-01-05 19:24:34+00:00
State: open

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

The following will fail the NotNull check.

``` java
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:

``` java
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.