Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Replace Gson.deepClone workaround #353

Merged
merged 2 commits into from
May 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions jest-common/src/main/java/com/google/gson/GsonUtils.java

This file was deleted.

5 changes: 3 additions & 2 deletions jest-common/src/main/java/io/searchbox/client/JestResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.*;
import io.searchbox.annotations.JestId;
import io.searchbox.cloning.CloneUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -200,7 +201,7 @@ protected List<JsonElement> extractSource(boolean addEsMetadataIdField) {
JsonObject currentObj = element.getAsJsonObject();
JsonObject source = currentObj.getAsJsonObject(sourceKey);
if (source != null) {
JsonObject copy = GsonUtils.deepCopy(source);
JsonObject copy = (JsonObject)CloneUtils.deepClone(source);
if (addEsMetadataIdField) {
copy.add(ES_METADATA_ID, currentObj.get("_id"));
}
Expand All @@ -210,7 +211,7 @@ protected List<JsonElement> extractSource(boolean addEsMetadataIdField) {
}
}
} else if (obj != null) {
JsonElement copy = GsonUtils.deepCopy(obj);
JsonElement copy = (JsonElement)CloneUtils.deepClone(obj);
JsonElement objId = jsonObject.get("_id");
if ((objId != null) && copy.isJsonObject() && addEsMetadataIdField) {
copy.getAsJsonObject().add(ES_METADATA_ID, objId);
Expand Down
44 changes: 44 additions & 0 deletions jest-common/src/main/java/io/searchbox/cloning/CloneUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.searchbox.cloning;

import com.google.gson.*;

import java.util.Map;

/**
* This class is just a workaround for the non-public deepCopy methods in Gson.
*/
public class CloneUtils {
public static JsonElement deepClone(JsonElement jsonElement) {
if (jsonElement instanceof JsonObject) {
return deepCloneObject(jsonElement);
} else if (jsonElement instanceof JsonArray) {
return deepCloneArray(jsonElement);
} else if (jsonElement instanceof JsonPrimitive) {
return jsonElement;
}

return JsonNull.INSTANCE;
}

private static JsonElement deepCloneObject(JsonElement jsonElement) {
JsonObject jsonObject = (JsonObject) jsonElement;
JsonObject result = new JsonObject();

for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
result.add(entry.getKey(), deepClone(entry.getValue()));
}

return result;
}

private static JsonElement deepCloneArray(JsonElement jsonElement) {
JsonArray jsonArray = (JsonArray) jsonElement;
JsonArray result = new JsonArray();

for (JsonElement element : jsonArray) {
result.add(deepClone(element));
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.searchbox.client.JestResult;
import io.searchbox.core.search.aggregation.MetricAggregation;
import io.searchbox.core.search.aggregation.RootAggregation;
import io.searchbox.cloning.CloneUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

Expand Down Expand Up @@ -111,7 +112,7 @@ protected <T, K> Hit<T, K> extractHit(Class<T> sourceType, Class<K> explanationT
List<String> sort = extractSort(hitObject.getAsJsonArray(SORT_KEY));

if (id != null) {
source = GsonUtils.deepCopy(source);
source = (JsonObject)CloneUtils.deepClone(source);
source.add(ES_METADATA_ID, id);
}
hit = new Hit<T, K>(
Expand Down