diff options
author | Linnea Gräf <nea@nea.moe> | 2024-04-14 22:22:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-14 22:22:51 +0200 |
commit | a1e6aeeeeb240cea8919c2b40c9a4b419911928a (patch) | |
tree | d247497fb515bd64e54bd45c0a957760f3fc2151 /src/main/java/at/hannibal2/skyhanni/utils | |
parent | 3c09b16b7f6a53c3d429c7060c7a851e3e122159 (diff) | |
download | skyhanni-a1e6aeeeeb240cea8919c2b40c9a4b419911928a.tar.gz skyhanni-a1e6aeeeeb240cea8919c2b40c9a4b419911928a.tar.bz2 skyhanni-a1e6aeeeeb240cea8919c2b40c9a4b419911928a.zip |
Fix self reference occuring when performing an internalizing config move (#1447)
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/JsonUtils.kt | 23 |
1 files changed, 23 insertions, 0 deletions
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 <reified T : Any> Gson.fromJson(jsonElement: JsonElement): T = this.fromJson(jsonElement, typeOf<T>().javaType) inline fun <reified T : Any> Gson.fromJson(reader: Reader): T = this.fromJson(reader, typeOf<T>().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 + } +} |