Gson JsonObject copy value affected other JsonObject instances

I have a strange problem with gson (my version of gson is 2.3.1) I have an instance of JsonObject called jsonObject (JsonObject jsonObject) jsonObject is value, not empty And I create another one, JsonObject tempOject = jsonObject; Therefore, when I try to delete an element inside tempObject, let's say tempObject.remove ("children");

This code then affected the jsonObject instance.

Here is the code snippet:

jsonObject              = element.getAsJsonObject();
        JsonElement tempElement = element;
        JsonObject tempObject   = jsonObject;
        String tempJson;

        if(tempObject.has("children")){
            tempObject.remove("children");
            tempJson = tempObject.toString();
            tempElement = new JsonParser().parse(tempJson);
        }

        if(nodes.isEmpty()){
            elements = new ArrayList<>();
            nodes.put(iterator, elements);
        }
        if(!nodes.containsKey(iterator)){
            elements = new ArrayList<>();
            nodes.put(iterator, elements);
        }
        nodes.get(iterator).add(tempElement);

        if (jsonObject.has("children")){
            tempNextJson        = jsonObject.get("children").toString();
            tempCurrJson        = jsonObject.toString();

            tempIterator++;
            metaDataProcessor(tempNextJson, tempCurrJson, tempNextJson, tempIterator, maxLevel);
        }

I read the gson JsonObject class, it uses a deep copy method. This should not affect the link, because JsonObject uses a deep copy of the value, so the returned JsonObject is new.

But why did this happen?

Anyway ... there is a deepCopy method inside the JsonObject class

JsonObject deepCopy() {
    JsonObject result = new JsonObject();
    Iterator i$ = this.members.entrySet().iterator();

    while(i$.hasNext()) {
        Entry entry = (Entry)i$.next();
        result.add((String)entry.getKey(), ((JsonElement)entry.getValue()).deepCopy());
    }

    return result;
}

JsonElement, JsonObject, , . , , .

?

+4
5

! Gson.

public <T> T deepCopy(T object, Class<T> type) {
    try {
        Gson gson = new Gson();
        return gson.fromJson(gson.toJson(object, type), type);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

:

JsonObject jsonObject = deepCopy(oldJsonObject, JsonObject.class);
+12

2.8.2, deepCopy() Gson JsonElement , JSON.

+2

, Gson deepCopy(). JsonElement , , JsonObject. :

@Nonnull
public static JsonObject deepCopy(@Nonnull JsonObject jsonObject) {
    JsonObject result = new JsonObject();
    for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
        result.add(entry.getKey(), deepCopy(entry.getValue()));
    }
    return result;
}

@Nonnull
public static JsonArray deepCopy(@Nonnull JsonArray jsonArray) {
    JsonArray result = new JsonArray();
    for (JsonElement e : jsonArray) {
        result.add(deepCopy(e));
    }
    return result;
}

@Nonnull
public static JsonElement deepCopy(@Nonnull JsonElement jsonElement) {
    if (jsonElement.isJsonPrimitive() || jsonElement.isJsonNull()) {
        return jsonElement;       // these are immutables anyway
    } else if (jsonElement.isJsonObject()) {
        return deepCopy(jsonElement.getAsJsonObject());
    } else if (jsonElement.isJsonArray()) {
        return deepCopy(jsonElement.getAsJsonArray());
    } else {
        throw new UnsupportedOperationException("Unsupported element: " + jsonElement);
    }
}
+1

tempObject = jsonObject . , , jsonObject.

, - :

JSONObject tempObject = new JSONObject(jsonObject.toString());
tempObject.remove("children");

JsonObject, json, .


GSON, JsonObject.deepyCopy(). r855: https://code.google.com/p/google-gson/source/detail?r=855

deepCopy(),

JsonObject tempObject = jsonObject.deepCopy();
tempObject.remove("children");
0

. deepCopy() JsonObject, , , JsonObject JsonElement JsonParser(). JsonObject JsonObject. , , deepCopy. , , JsonObject. JsonObject - hashmap (LinkedTreeMap), - JsonElement, JsonElement.

0

Source: https://habr.com/ru/post/1583901/


All Articles