Use prefix to merge result from codec with result json
This commit is contained in:
parent
d5bbd717b6
commit
f71bed5dae
@ -36,12 +36,12 @@ fun <V : Any> Codec<V>.fromJsonStrict(value: JsonElement): V {
|
||||
return decode(JsonOps.INSTANCE, value).get().map({ left -> left.first }, { throw JsonSyntaxException("Error decoding element: ${it.message()}") })
|
||||
}
|
||||
|
||||
fun <V : Any> Codec<V>.toJson(value: V): JsonElement? {
|
||||
return encode(value, JsonOps.INSTANCE, JsonOps.INSTANCE.empty()).get().map({ it }, { null })
|
||||
fun <V : Any> Codec<V>.toJson(value: V, prefix: JsonElement = JsonOps.INSTANCE.empty()): JsonElement? {
|
||||
return encode(value, JsonOps.INSTANCE, prefix).get().map({ it }, { null })
|
||||
}
|
||||
|
||||
fun <V : Any> Codec<V>.toJsonStrict(value: V): JsonElement {
|
||||
return encode(value, JsonOps.INSTANCE, JsonOps.INSTANCE.empty()).get().map({ it }, { throw RuntimeException("Error encoding element: ${it.message()}") })
|
||||
fun <V : Any> Codec<V>.toJsonStrict(value: V, prefix: JsonElement = JsonOps.INSTANCE.empty()): JsonElement {
|
||||
return encode(value, JsonOps.INSTANCE, prefix).get().map({ it }, { throw RuntimeException("Error encoding element: ${it.message()}") })
|
||||
}
|
||||
|
||||
fun <V : Any> Codec<V>.fromNbt(value: Tag): V? {
|
||||
@ -52,12 +52,12 @@ fun <V : Any> Codec<V>.fromNbtStrict(value: Tag): V {
|
||||
return decode(NbtOps.INSTANCE, value).get().map({ left -> left.first }, { throw RuntimeException("Error decoding element: ${it.message()}") })
|
||||
}
|
||||
|
||||
fun <V : Any> Codec<V>.toNbt(value: V): Tag? {
|
||||
return encode(value, NbtOps.INSTANCE, NbtOps.INSTANCE.empty()).get().map({ it }, { null })
|
||||
fun <V : Any> Codec<V>.toNbt(value: V, prefix: Tag = NbtOps.INSTANCE.empty()): Tag? {
|
||||
return encode(value, NbtOps.INSTANCE, prefix).get().map({ it }, { null })
|
||||
}
|
||||
|
||||
fun <V : Any> Codec<V>.toNbtStrict(value: V): Tag {
|
||||
return encode(value, NbtOps.INSTANCE, NbtOps.INSTANCE.empty()).get().map({ it }, { throw RuntimeException("Error encoding element: ${it.message()}") })
|
||||
fun <V : Any> Codec<V>.toNbtStrict(value: V, prefix: Tag = NbtOps.INSTANCE.empty()): Tag {
|
||||
return encode(value, NbtOps.INSTANCE, prefix).get().map({ it }, { throw RuntimeException("Error encoding element: ${it.message()}") })
|
||||
}
|
||||
|
||||
// 1.19 being 1.19
|
||||
|
@ -13,19 +13,15 @@ import ru.dbotthepony.mc.otm.core.toJsonStrict
|
||||
class Codec2Serializer<T : Any>(val codec: Codec<T>, val embed: Boolean = true) : Serializer<T> {
|
||||
override fun serialize(p_79325_: JsonObject, p_79326_: T, p_79327_: JsonSerializationContext) {
|
||||
if (embed) {
|
||||
val json = codec.toJsonStrict(p_79326_)
|
||||
val result = codec.toJsonStrict(p_79326_, p_79325_)
|
||||
|
||||
if (json !is JsonObject) {
|
||||
throw RuntimeException("It was specified to embed result from codec into target json, but codec returned ${json::class.qualifiedName}, while was expecting JsonObject")
|
||||
if (result !is JsonObject) {
|
||||
throw RuntimeException("Expected JsonObject from codec, got ${result::class.qualifiedName}")
|
||||
}
|
||||
|
||||
for ((k, v) in json.entrySet()) {
|
||||
if (p_79325_.has(k)) {
|
||||
throw RuntimeException("Codec returned object with '$k' as member, which is not allowed (it was written by upstream code into provided JsonObject)")
|
||||
}
|
||||
|
||||
p_79325_[k] = v
|
||||
}
|
||||
val keys = ArrayList(p_79325_.keySet())
|
||||
for (k in keys) p_79325_.remove(k)
|
||||
for ((k, v) in result.entrySet()) p_79325_[k] = v
|
||||
} else {
|
||||
p_79325_["value"] = codec.toJsonStrict(p_79326_)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user