Skip to content

Commit 07acf58

Browse files
committed
fix passthrough because DFU encodes null values to json but doesn't decode properly
see: Mojang/DataFixerUpper#62
1 parent 5789b43 commit 07acf58

File tree

1 file changed

+25
-1
lines changed
  • common/src/main/java/com/teamresourceful/resourcefullib/common/codecs

1 file changed

+25
-1
lines changed

Diff for: common/src/main/java/com/teamresourceful/resourcefullib/common/codecs/CodecExtras.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.teamresourceful.resourcefullib.common.codecs;
22

3+
import com.google.gson.JsonArray;
34
import com.google.gson.JsonElement;
5+
import com.google.gson.JsonObject;
46
import com.mojang.datafixers.util.Either;
57
import com.mojang.serialization.Codec;
68
import com.mojang.serialization.DataResult;
@@ -54,7 +56,7 @@ public static <T> Codec<T> passthrough(Function<T, JsonElement> encoder, Functio
5456
return DataResult.success(decoder.apply(jsonElement));
5557
}
5658
return DataResult.error("Value was not an instance of JsonElement");
57-
}, input -> new Dynamic<>(JsonOps.INSTANCE, encoder.apply(input)));
59+
}, input -> new Dynamic<>(JsonOps.INSTANCE, clearNulls(encoder.apply(input))));
5860
}
5961

6062
public static <S> Codec<S> eitherRight(Codec<Either<S, S>> eitherCodec) {
@@ -64,4 +66,26 @@ public static <S> Codec<S> eitherRight(Codec<Either<S, S>> eitherCodec) {
6466
public static <S> Codec<S> eitherLeft(Codec<Either<S, S>> eitherCodec) {
6567
return eitherCodec.xmap(e -> e.map(p -> p, p -> p), Either::left);
6668
}
69+
70+
private static JsonElement clearNulls(JsonElement json) {
71+
if (json instanceof JsonObject object) {
72+
JsonObject newObject = new JsonObject();
73+
for (String key : object.keySet()) {
74+
JsonElement element = clearNulls(object.get(key));
75+
if (element != null) {
76+
newObject.add(key, element);
77+
}
78+
}
79+
} else if (json instanceof JsonArray array) {
80+
JsonArray newArray = new JsonArray();
81+
for (JsonElement item : array) {
82+
JsonElement element = clearNulls(item);
83+
if (element != null) {
84+
newArray.add(element);
85+
}
86+
}
87+
return newArray;
88+
}
89+
return json.isJsonNull() ? null : json;
90+
}
6791
}

0 commit comments

Comments
 (0)