This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 729
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #353 from yuriyfilonov/master
Replace Gson.deepClone workaround - fixes #351
- Loading branch information
Showing
4 changed files
with
49 additions
and
17 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
jest-common/src/main/java/io/searchbox/cloning/CloneUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters