How do I clone an org.json.JSONObject in Java?

JavaJsonorg.json

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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

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

Solution 2 - Java

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));

Solution 3 - Java

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.

Solution 4 - Java

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

Solution 5 - Java

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!

Solution 6 - Java

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

Solution 7 - Java

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;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel SchafferView Question on Stackoverflow
Solution 1 - JavaJaytjuhView Answer on Stackoverflow
Solution 2 - JavaBill the LizardView Answer on Stackoverflow
Solution 3 - JavaBogdan StadnykView Answer on Stackoverflow
Solution 4 - JavaGiboltView Answer on Stackoverflow
Solution 5 - JavaUriView Answer on Stackoverflow
Solution 6 - JavaAliView Answer on Stackoverflow
Solution 7 - Javaxer21View Answer on Stackoverflow