Why JSONObject.put(...) copies the value?

Help
user17
2009-06-19
2013-04-29
  • user17
    user17
    2009-06-19

    Hi,

    My project uses the original org.json code developed by Douglas Crockford. We are looking at json-lib as a possible replacement.

    We have hundreds places in the code where do something like this:

        JSONObject result= new JSONObject();
        JSONObject part1 = new JSONObject();
        JSONObject part2 = new JSONObject();
        ...

        // add empty parts to the parent
        parent.put( "part1", part1 );
        parent.put( "part2", part2 );

        // fill in the parts
        ...

        return result;

    This code broke after we switched from org.json to json-lib. The reason: json-lib version of JSONObject.put(...) adds a *copy* of the value, not the reference to the value, like org.json did.

    A couple of questions:

    1. Is there an easy way to change the behavior of put(...) call, so it stores a reference rather than a copy?

    2. json-lib was derived from org.json.  What was the reason to change the put(...) behavior?

    Thanks,
    user17

     
    • user17
      user17
      2009-06-19

      Sorry, the code snippet in my original post isn't quite right.  Here's the correct version.

      ---------------------------------------------------------
      JSONObject result= new JSONObject();

      JSONObject part1 = new JSONObject();
      JSONObject part2 = new JSONObject();

      // add empty parts to the result
      result.put( "part1", part1 );
      result.put( "part2", part2 );

      // fill in the parts
      ...

      return result;
      ---------------------------------------------------------