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

Commit

Permalink
Reference to uk.com.robust-it has been removed. Instead CloneUtils cl…
Browse files Browse the repository at this point in the history
…ass now simply mimics behaviour of native deepCopy methods defined in JsonObject, JsonArray, JsonPrimitive and JsonNull.
  • Loading branch information
Phil Filonov committed May 4, 2016
1 parent 250dbe6 commit 45b3bb1
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 37 deletions.
6 changes: 0 additions & 6 deletions jest-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@
<artifactId>gson</artifactId>
</dependency>

<dependency>
<groupId>uk.com.robust-it</groupId>
<artifactId>cloning</artifactId>
<version>1.9.2</version>
</dependency>

<!-- Testing Dependencies -->
<dependency>
<groupId>junit</groupId>
Expand Down
6 changes: 3 additions & 3 deletions jest-common/src/main/java/io/searchbox/client/JestResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.google.gson.*;
import io.searchbox.annotations.JestId;
import io.searchbox.gson.GsonUtils;
import io.searchbox.cloning.CloneUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -201,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 @@ -211,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;
}
}
4 changes: 2 additions & 2 deletions jest-common/src/main/java/io/searchbox/core/SearchResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.searchbox.client.JestResult;
import io.searchbox.core.search.aggregation.MetricAggregation;
import io.searchbox.core.search.aggregation.RootAggregation;
import io.searchbox.gson.GsonUtils;
import io.searchbox.cloning.CloneUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

Expand Down Expand Up @@ -112,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
20 changes: 0 additions & 20 deletions jest-common/src/main/java/io/searchbox/gson/GsonUtils.java

This file was deleted.

6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,6 @@
<version>2.0.3-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>uk.com.robust-it</groupId>
<artifactId>cloning</artifactId>
<version>1.9.2</version>
</dependency>

<!-- Testing Dependencies -->
<dependency>
<groupId>junit</groupId>
Expand Down

0 comments on commit 45b3bb1

Please sign in to comment.