7

Let's posit the following theoretical situation: A webapp is describing the odd creatures of the arctic, including an odd long-snouted creature like an anteater that eats arctic frogs, that the Eskimoes call a "nh-oo-l", or more commonly written, a "null".

When writing the data about this creature out to the webapp, we write this line:

net.sf.json.JSONObject creatureJson = new JSONObject();
creatureJson.element("name", "null");

However, this then runs into a problem inside the value parser:

In net.sf.json.AbstractJSON:

protected Object _processValue( Object value, JsonConfig jsonConfig ) {
if( JSONNull.getInstance().equals( value ) ) {
     return JSONNull.getInstance();
...

In net.sf.json.JSONNull:

public boolean equals( Object object ) {
  return object == null || object == this || object == instance
        || (object instanceof JSONObject && ((JSONObject) object).isNullObject())
        || "null".equals( object );
}

So what happens as a result is, even though we actually want to use "null" as our value (nor do we want the return value to have to process an actual null value), it will come back as a JSONNull, and get printed as {"name": null}.

I've been trying to search through net.sf.json's documentation and configuration options for a way around this, but all the behavior seems to be hardcoded in. It's my understanding that this is a very common library. How is it meant to operate in this way?

NOTE: To be clear, this is for a situation when creating JSON, not parsing. There is a similar issue Can't parse JSON property "null", but the issue there was that the user was attempting to use single-quotes for parsing; the root issue had absolutely nothing to do with nulls at all.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Katana314
  • 8,429
  • 2
  • 28
  • 36

0 Answers0