52

Is there a way to clone an instance of org.json.JSONObject without stringifying it and reparsing the result?

A shallow copy would be acceptable.

Gaël J
  • 11,274
  • 4
  • 17
  • 32
Daniel Schaffer
  • 56,753
  • 31
  • 116
  • 165

7 Answers7

92

Easiest (and incredibly slow and inefficient) way to do it

JSONObject clone = new JSONObject(original.toString());
Miha_x64
  • 5,973
  • 1
  • 41
  • 63
Jaytjuh
  • 1,083
  • 8
  • 11
64

Use the public JSONObject(JSONObject jo, java.lang.String[] names) constructor and the public static java.lang.String[] getNames(JSONObject jo) method.

JSONObject copy = new JSONObject(original, JSONObject.getNames(original));
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
4

the fastest + minimal way I found is this. it does deep copy.

JSONObject clone= new JSONObject(original.toMap());

I know the asker said

A shallow copy would be acceptable. but I think that does not rule out if the solution will do deep copy.

Update: the toMap() function is not available in Android. but the org.json library available on maven under groupId org.json has it: https://search.maven.org/artifact/org.json/json/20210307/bundle

Ali
  • 115
  • 1
  • 3
  • 9
3

Cause $JSONObject.getNames(original) not accessible in android, you can do it with:

public JSONObject shallowCopy(JSONObject original) {
    JSONObject copy = new JSONObject();

    for ( Iterator<String> iterator = original.keys(); iterator.hasNext(); ) {
        String      key     = iterator.next();
        JSONObject  value   = original.optJSONObject(key);

        try {
            copy.put(key, value);
        } catch ( JSONException e ) {
            //TODO process exception
        }
    }

    return copy;
}

But remember it is not deep copy.

3

For Android developers, the simplest solution without using .getNames is:

JSONObject copy = new JSONObject();
for (Object key : original.keySet()) {
  Object value = original.get(key);
  copy.put(key, value);
}

Note: This is a only a shallow copy

Gibolt
  • 42,564
  • 15
  • 187
  • 127
2

Couldn't find an existing deep clone method for com.google.gwt.json.client.JSONObject but the implementation should be few lines of code, something like:

public static JSONValue deepClone(JSONValue jsonValue){
    JSONString string = jsonValue.isString();
    if (string != null){return new JSONString(string.stringValue());}

    JSONBoolean aBoolean = jsonValue.isBoolean();
    if (aBoolean != null){return JSONBoolean.getInstance(aBoolean.booleanValue());}

    JSONNull aNull = jsonValue.isNull();
    if (aNull != null){return JSONNull.getInstance();}

    JSONNumber number = jsonValue.isNumber();
    if (number!=null){return new JSONNumber(number.doubleValue());}

    JSONObject jsonObject = jsonValue.isObject();
    if (jsonObject!=null){
        JSONObject clonedObject = new JSONObject();
        for (String key : jsonObject.keySet()){
            clonedObject.put(key, deepClone(jsonObject.get(key)));
        }
        return clonedObject;
    }

    JSONArray array = jsonValue.isArray();
    if (array != null){
        JSONArray clonedArray = new JSONArray();
        for (int i=0 ; i < array.size() ; ++i){
            clonedArray.set(i, deepClone(array.get(i)));
        }
        return clonedArray;
    }

    throw new IllegalStateException();
}

*Note:*I haven't tested it yet!

Uri
  • 29
  • 3
-2

In case anyone comes here looking for a deep clone for org.google.gson, since they don't expose their deepClone() method this is what I came up with...

public static JsonElement deepClone(JsonElement el){
    if (el.isJsonPrimitive() || el.isJsonNull())
        return el;
    if (el.isJsonArray()) {
        JsonArray array = new JsonArray();
        for(JsonElement arrayEl: el.getAsJsonArray())
            array.add(deepClone(arrayEl));
        return array;
    }
    if(el.isJsonObject()) {
        JsonObject obj = new JsonObject();
        for (Map.Entry<String, JsonElement> entry : el.getAsJsonObject().entrySet()) {
            obj.add(entry.getKey(), deepClone(entry.getValue()));
        }
        return obj;
    }
    throw new IllegalArgumentException("JsonElement type " + el.getClass().getName());
}

And here are a few methods to merge two JsonObject's

public static JsonObject merge(String overrideJson, JsonObject defaultObj) {
    return mergeInto((JsonObject)new JsonParser().parse(overrideJson), defaultObj);
}
public static JsonObject merge(JsonObject overrideObj, JsonObject defaultObj) {
    return mergeOverride((JsonObject)deepClone(defaultObj), overrideObj);
}
public static JsonObject mergeOverride(JsonObject targetObj, JsonObject overrideObj) {
    for (Map.Entry<String, JsonElement> entry : overrideObj.entrySet())
            targetObj.add(entry.getKey(), deepClone(entry.getValue()));
    return targetObj;
}
public static JsonObject mergeInto(JsonObject targetObj, JsonObject defaultObj) {
    for (Map.Entry<String, JsonElement> entry : defaultObj.entrySet()) {
        if (targetObj.has(entry.getKey()) == false)
            targetObj.add(entry.getKey(), deepClone(entry.getValue()));
    }
    return targetObj;
}
xer21
  • 304
  • 1
  • 11