1
1
package com .teamresourceful .resourcefullib .common .codecs ;
2
2
3
+ import com .google .gson .JsonArray ;
3
4
import com .google .gson .JsonElement ;
5
+ import com .google .gson .JsonObject ;
4
6
import com .mojang .datafixers .util .Either ;
5
7
import com .mojang .serialization .Codec ;
6
8
import com .mojang .serialization .DataResult ;
@@ -54,7 +56,7 @@ public static <T> Codec<T> passthrough(Function<T, JsonElement> encoder, Functio
54
56
return DataResult .success (decoder .apply (jsonElement ));
55
57
}
56
58
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 ) )));
58
60
}
59
61
60
62
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) {
64
66
public static <S > Codec <S > eitherLeft (Codec <Either <S , S >> eitherCodec ) {
65
67
return eitherCodec .xmap (e -> e .map (p -> p , p -> p ), Either ::left );
66
68
}
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
+ }
67
91
}
0 commit comments