Skip to content

Commit

Permalink
decodeJson/encodeJson -> decodeQuick/encodeQuick
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Nov 4, 2023
1 parent f266d39 commit e24f8aa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ loader_version=0.14.21
fabric_kotlin_version=1.10.0+kotlin.1.9.0

# Mod Properties
mod_version=1.3.1
mod_version=1.4.0
maven_group=dev.andante
17 changes: 8 additions & 9 deletions src/main/kotlin/dev/andante/codex/Codex.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package dev.andante.codex

import com.google.gson.JsonElement
import com.mojang.datafixers.util.Pair
import com.mojang.serialization.Codec
import com.mojang.serialization.Decoder
import com.mojang.serialization.DynamicOps
import com.mojang.serialization.Encoder
import com.mojang.serialization.JsonOps
import com.mojang.serialization.MapCodec
import java.util.Optional

Expand All @@ -27,20 +26,20 @@ fun <A : Any?> MapCodec<A>.functionally(): MapCodec<() -> A> {
}

/**
* Encodes to a JSON format.
* Encodes to a dynamic ops format.
*/
fun <A> Encoder<A>.encodeJson(input: A): JsonElement? {
return encodeStart(JsonOps.INSTANCE, input)
fun <A, T> Encoder<A>.encodeQuick(ops: DynamicOps<T>, input: A): T? {
return encodeStart(ops, input)
.result()
.orElse(null)
}

/**
* Decodes from a JSON format.
* Decodes from a dynamic ops format.
*/
fun <A> Decoder<A>.decodeJson(input: JsonElement): A? {
return decode(JsonOps.INSTANCE, input)
fun <A, T> Decoder<A>.decodeQuick(ops: DynamicOps<T>, input: T): A? {
return decode(ops, input)
.result()
.map(Pair<A, JsonElement>::getFirst)
.map(Pair<A, T>::getFirst)
.orElse(null)
}

0 comments on commit e24f8aa

Please sign in to comment.