From a1e6aeeeeb240cea8919c2b40c9a4b419911928a Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Sun, 14 Apr 2024 22:22:51 +0200 Subject: Fix self reference occuring when performing an internalizing config move (#1447) --- .../java/at/hannibal2/skyhanni/utils/JsonUtils.kt | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/main/java/at/hannibal2/skyhanni/utils') diff --git a/src/main/java/at/hannibal2/skyhanni/utils/JsonUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/JsonUtils.kt index f9a279c46..cc3dda044 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/JsonUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/JsonUtils.kt @@ -1,7 +1,9 @@ package at.hannibal2.skyhanni.utils import com.google.gson.Gson +import com.google.gson.JsonArray import com.google.gson.JsonElement +import com.google.gson.JsonObject import java.io.Reader import kotlin.reflect.jvm.javaType import kotlin.reflect.typeOf @@ -12,3 +14,24 @@ inline fun Gson.fromJson(jsonElement: JsonElement): T = this.fromJson(jsonElement, typeOf().javaType) inline fun Gson.fromJson(reader: Reader): T = this.fromJson(reader, typeOf().javaType) + +/** + * Straight forward deep copy. This is included in gson as well, but different versions have it exposed privately instead of publicly, + * so this reimplementation is here as an always public alternative. + */ +fun JsonElement.shDeepCopy(): JsonElement { + return when (this) { + is JsonObject -> JsonObject().also { + for (entry in this.entrySet()) + it.add(entry.key, entry.value.shDeepCopy()) + } + + is JsonArray -> JsonArray().also { + for (entry in this) { + it.add(entry.shDeepCopy()) + } + } + + else -> this + } +} -- cgit