Skip to content

Commit 721ee17

Browse files
committed
decoder
1 parent a5c8370 commit 721ee17

3 files changed

Lines changed: 30 additions & 26 deletions

File tree

src/main/java/rml/deserializer/AbstractDeserializer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package rml.deserializer;
22

3-
import com.google.gson.JsonElement;
43
import net.minecraft.util.ResourceLocation;
4+
import rml.deserializer.struct.std.StructElement;
55
import rml.jrx.announces.EarlyClass;
66
import rml.jrx.announces.PublicAPI;
77

@@ -24,7 +24,7 @@ public AbstractDeserializer(ResourceLocation registerName, Class<T> target, IDes
2424
public Class<T> getResultTarget(){
2525
return this.target;
2626
}
27-
public T deserialize(JsonElement json) throws JsonDeserializeException {
27+
public T deserialize(StructElement json) throws JsonDeserializeException {
2828
return this.function.apply(json);
2929
}
3030

@@ -34,7 +34,7 @@ public ResourceLocation getRegisterName(){
3434

3535
@FunctionalInterface
3636
public interface IDeserializer<V>{
37-
V apply(JsonElement jsonElement) throws JsonDeserializeException;
37+
V apply(StructElement jsonElement) throws JsonDeserializeException;
3838
}
3939

4040
public static <T> IDeserializer<T> safeRun(IDeserializer<T> deserializer){

src/main/java/rml/deserializer/Argument.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.google.common.collect.Lists;
44
import com.google.gson.JsonElement;
55
import com.google.gson.JsonObject;
6+
import rml.deserializer.struct.std.StructElement;
7+
import rml.deserializer.struct.std.StructObject;
68
import rml.jrx.announces.EarlyClass;
79
import rml.jrx.announces.PublicAPI;
810

@@ -35,8 +37,8 @@ public T cast(DeserializerBuilder.Context context) {
3537
public static <E> Argument<E> type(String name, Class<E> clazz) {
3638
return new Argument<E>(name) {
3739
@Override
38-
public void execute(DeserializerManager manager, JsonObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
39-
JsonElement element = DeserializerManager.getFromPath(jsonObject, name);
40+
public void execute(DeserializerManager manager, StructObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
41+
StructElement element = DeserializerManager.getFromPath(jsonObject, name);
4042
if (element != null){
4143
try {
4244
final Class<?> clazz2 = DeserializerBuilder.avoidPrimitive(clazz);
@@ -56,7 +58,7 @@ public static Argument<Integer> integer(String name, Function<Integer, Throwable
5658
return new Argument<Integer>(name) {
5759
@Override
5860
public void execute(DeserializerManager manager, JsonObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
59-
JsonElement element = DeserializerManager.getFromPath(jsonObject, name);
61+
StructElement element = DeserializerManager.getFromPath(jsonObject, name);
6062
if (element != null){
6163
Integer obj = null;
6264
try {
@@ -92,8 +94,8 @@ public static Argument<Integer> positiveInteger(String name) {
9294
public static Argument<Float> floatArgument(String name, Function<Float, Throwable> predicate){
9395
return new Argument<Float>(name) {
9496
@Override
95-
public void execute(DeserializerManager manager, JsonObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
96-
JsonElement element = DeserializerManager.getFromPath(jsonObject, name);
97+
public void execute(DeserializerManager manager, StructObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
98+
StructElement element = DeserializerManager.getFromPath(jsonObject, name);
9799
if (element != null){
98100
Float obj = null;
99101
try{
@@ -129,8 +131,8 @@ public static Argument<Float> positiveFloat(String name){
129131
public static Argument<Boolean> bool(String name, Function<Boolean, Throwable> predicate){
130132
return new Argument<Boolean>(name) {
131133
@Override
132-
public void execute(DeserializerManager manager, JsonObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
133-
JsonElement element = DeserializerManager.getFromPath(jsonObject, name);
134+
public void execute(DeserializerManager manager, StructObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
135+
StructElement element = DeserializerManager.getFromPath(jsonObject, name);
134136
if (element != null){
135137
Boolean obj = null;
136138
try{
@@ -154,12 +156,12 @@ public void execute(DeserializerManager manager, JsonObject jsonObject, Deserial
154156
public static<T> Argument<Map<String, T>> map(String name, final Class<T> clazz){
155157
return new Argument<Map<String, T>>(name) {
156158
@Override
157-
public void execute(DeserializerManager manager, JsonObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
158-
JsonElement element = DeserializerManager.getFromPath(jsonObject, name);
159+
public void execute(DeserializerManager manager, StructObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
160+
StructElement element = DeserializerManager.getFromPath(jsonObject, name);
159161
if (element != null){
160-
if (!element.isJsonObject()) throw new JsonDeserializeException(jsonObject, "field" + name + "is required be json object.");
162+
if (!element.isObject()) throw new JsonDeserializeException(jsonObject, "field" + name + "is required be json object.");
161163
try {
162-
Map<String, T> map = element.getAsJsonObject().entrySet().stream().collect(Collectors.toMap((entry) -> entry.getKey(), (entry) -> manager.decodeSilently(clazz, entry.getValue())));
164+
Map<String, T> map = element.getAsObject().entrySet().stream().collect(Collectors.toMap((entry) -> entry.getKey(), (entry) -> manager.decodeSilently(clazz, entry.getValue())));
163165
context.put(name, map);
164166
}catch (Exception e){
165167
throw new JsonDeserializeException(element, e);
@@ -174,8 +176,8 @@ public void execute(DeserializerManager manager, JsonObject jsonObject, Deserial
174176
public static<T> Argument<List<T>> list(String name, final Class<T> clazz){
175177
return new Argument<List<T>>(name) {
176178
@Override
177-
public void execute(DeserializerManager manager, JsonObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
178-
JsonElement element = DeserializerManager.getFromPath(jsonObject, name);
179+
public void execute(DeserializerManager manager, StructObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
180+
StructElement element = DeserializerManager.getFromPath(jsonObject, name);
179181
if (element != null){
180182
try {
181183
Class<?> arrayCls = Array.newInstance(clazz, 0).getClass();

src/main/java/rml/deserializer/DeserializerBuilder.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.google.gson.JsonElement;
55
import com.google.gson.JsonObject;
66
import net.minecraft.util.ResourceLocation;
7+
import rml.deserializer.struct.std.StructElement;
8+
import rml.deserializer.struct.std.StructObject;
79
import rml.internal.net.minecraftforge.common.util.LazyOptional;
810
import rml.jrx.announces.EarlyClass;
911
import rml.jrx.announces.PublicAPI;
@@ -23,10 +25,10 @@
2325
@EarlyClass
2426
@PublicAPI
2527
public class DeserializerBuilder<T> {
26-
private DeserializerManager manager;
27-
private Class<T> clazz;
28-
private ResourceLocation resourceLocation;
29-
private HashSet<IAction> actions = new HashSet<>();
28+
private final DeserializerManager manager;
29+
private final Class<T> clazz;
30+
private final ResourceLocation resourceLocation;
31+
private final HashSet<IAction> actions = new HashSet<>();
3032
private IJsonObjectFunction<T> function = (context) -> null;
3133
private boolean isDefault = false;
3234
public DeserializerBuilder(DeserializerManager manager, Class<T> clazz, ResourceLocation resourceLocation){
@@ -58,7 +60,7 @@ public DeserializerBuilder<T> check(final Function<Context, JsonDeserializeExcep
5860
public DeserializerBuilder<T> optionalWhen(final Class<?> clazz,final String name,final Context.ToBooleanFunction isNotRequired){
5961
final Class<?> type = DeserializerBuilder.avoidPrimitive(clazz);
6062
return this.action(((manager, jsonObject, context) -> {
61-
JsonElement element = DeserializerManager.getFromPath(jsonObject, name);
63+
StructElement element = DeserializerManager.getFromPath(jsonObject, name);
6264
if (element != null){
6365
try{
6466
Object obj = manager.decode(type, element);
@@ -93,7 +95,7 @@ public DeserializerBuilder<T> optional(Class<?> clazz, String name){
9395
public <V> DeserializerBuilder<T> optionalDefaultWhen(final Class<V> clazz, final String name, final Context.ToBooleanFunction isNotRequired, final V defaultValue){
9496
final Class<?> type = DeserializerBuilder.avoidPrimitive(clazz);
9597
return this.action(((manager, jsonObject, context) -> {
96-
JsonElement element = DeserializerManager.getFromPath(jsonObject, name);
98+
StructElement element = DeserializerManager.getFromPath(jsonObject, name);
9799
if (element != null){
98100
try{
99101
Object obj = manager.decode(type, element);
@@ -116,7 +118,7 @@ public <V> DeserializerBuilder<T> optionalDefault(Class<V> clazz, String name, V
116118
public <V> DeserializerBuilder<T> optionalDefaultLazyWhen(final Class<V> clazz, final String name, final Context.ToBooleanFunction isNotRequired, final LazyOptional<V> defaultValue){
117119
final Class<?> type = DeserializerBuilder.avoidPrimitive(clazz);
118120
return this.action(((manager, jsonObject, context) -> {
119-
JsonElement element = DeserializerManager.getFromPath(jsonObject, name);
121+
StructElement element = DeserializerManager.getFromPath(jsonObject, name);
120122
if (element != null){
121123
try{
122124
Object obj = manager.decode(type, element);
@@ -239,8 +241,8 @@ public void put(String s, Object obj){
239241

240242
@FunctionalInterface
241243
public interface ToBooleanFunction{
242-
ToBooleanFunction TRUE = (o)->true;
243-
ToBooleanFunction FALSE = (o)->false;
244+
ToBooleanFunction TRUE = (o) -> true;
245+
ToBooleanFunction FALSE = (o) -> false;
244246
boolean apply(Context context);
245247
}
246248

@@ -249,7 +251,7 @@ public interface ToBooleanFunction{
249251

250252
@FunctionalInterface
251253
public interface IAction{
252-
void execute(DeserializerManager manager, JsonObject jsonObject, Context context) throws JsonDeserializeException;
254+
void execute(DeserializerManager manager, StructObject jsonObject, Context context) throws JsonDeserializeException;
253255
}
254256

255257
@FunctionalInterface

0 commit comments

Comments
 (0)