Skip to content

Commit

Permalink
decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Ecdcaeb committed Dec 29, 2024
1 parent a5c8370 commit 721ee17
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/main/java/rml/deserializer/AbstractDeserializer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package rml.deserializer;

import com.google.gson.JsonElement;
import net.minecraft.util.ResourceLocation;
import rml.deserializer.struct.std.StructElement;
import rml.jrx.announces.EarlyClass;
import rml.jrx.announces.PublicAPI;

Expand All @@ -24,7 +24,7 @@ public AbstractDeserializer(ResourceLocation registerName, Class<T> target, IDes
public Class<T> getResultTarget(){
return this.target;
}
public T deserialize(JsonElement json) throws JsonDeserializeException {
public T deserialize(StructElement json) throws JsonDeserializeException {
return this.function.apply(json);
}

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

@FunctionalInterface
public interface IDeserializer<V>{
V apply(JsonElement jsonElement) throws JsonDeserializeException;
V apply(StructElement jsonElement) throws JsonDeserializeException;
}

public static <T> IDeserializer<T> safeRun(IDeserializer<T> deserializer){
Expand Down
28 changes: 15 additions & 13 deletions src/main/java/rml/deserializer/Argument.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.common.collect.Lists;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import rml.deserializer.struct.std.StructElement;
import rml.deserializer.struct.std.StructObject;
import rml.jrx.announces.EarlyClass;
import rml.jrx.announces.PublicAPI;

Expand Down Expand Up @@ -35,8 +37,8 @@ public T cast(DeserializerBuilder.Context context) {
public static <E> Argument<E> type(String name, Class<E> clazz) {
return new Argument<E>(name) {
@Override
public void execute(DeserializerManager manager, JsonObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
JsonElement element = DeserializerManager.getFromPath(jsonObject, name);
public void execute(DeserializerManager manager, StructObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
StructElement element = DeserializerManager.getFromPath(jsonObject, name);
if (element != null){
try {
final Class<?> clazz2 = DeserializerBuilder.avoidPrimitive(clazz);
Expand All @@ -56,7 +58,7 @@ public static Argument<Integer> integer(String name, Function<Integer, Throwable
return new Argument<Integer>(name) {
@Override
public void execute(DeserializerManager manager, JsonObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
JsonElement element = DeserializerManager.getFromPath(jsonObject, name);
StructElement element = DeserializerManager.getFromPath(jsonObject, name);
if (element != null){
Integer obj = null;
try {
Expand Down Expand Up @@ -92,8 +94,8 @@ public static Argument<Integer> positiveInteger(String name) {
public static Argument<Float> floatArgument(String name, Function<Float, Throwable> predicate){
return new Argument<Float>(name) {
@Override
public void execute(DeserializerManager manager, JsonObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
JsonElement element = DeserializerManager.getFromPath(jsonObject, name);
public void execute(DeserializerManager manager, StructObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
StructElement element = DeserializerManager.getFromPath(jsonObject, name);
if (element != null){
Float obj = null;
try{
Expand Down Expand Up @@ -129,8 +131,8 @@ public static Argument<Float> positiveFloat(String name){
public static Argument<Boolean> bool(String name, Function<Boolean, Throwable> predicate){
return new Argument<Boolean>(name) {
@Override
public void execute(DeserializerManager manager, JsonObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
JsonElement element = DeserializerManager.getFromPath(jsonObject, name);
public void execute(DeserializerManager manager, StructObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
StructElement element = DeserializerManager.getFromPath(jsonObject, name);
if (element != null){
Boolean obj = null;
try{
Expand All @@ -154,12 +156,12 @@ public void execute(DeserializerManager manager, JsonObject jsonObject, Deserial
public static<T> Argument<Map<String, T>> map(String name, final Class<T> clazz){
return new Argument<Map<String, T>>(name) {
@Override
public void execute(DeserializerManager manager, JsonObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
JsonElement element = DeserializerManager.getFromPath(jsonObject, name);
public void execute(DeserializerManager manager, StructObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
StructElement element = DeserializerManager.getFromPath(jsonObject, name);
if (element != null){
if (!element.isJsonObject()) throw new JsonDeserializeException(jsonObject, "field" + name + "is required be json object.");
if (!element.isObject()) throw new JsonDeserializeException(jsonObject, "field" + name + "is required be json object.");
try {
Map<String, T> map = element.getAsJsonObject().entrySet().stream().collect(Collectors.toMap((entry) -> entry.getKey(), (entry) -> manager.decodeSilently(clazz, entry.getValue())));
Map<String, T> map = element.getAsObject().entrySet().stream().collect(Collectors.toMap((entry) -> entry.getKey(), (entry) -> manager.decodeSilently(clazz, entry.getValue())));
context.put(name, map);
}catch (Exception e){
throw new JsonDeserializeException(element, e);
Expand All @@ -174,8 +176,8 @@ public void execute(DeserializerManager manager, JsonObject jsonObject, Deserial
public static<T> Argument<List<T>> list(String name, final Class<T> clazz){
return new Argument<List<T>>(name) {
@Override
public void execute(DeserializerManager manager, JsonObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
JsonElement element = DeserializerManager.getFromPath(jsonObject, name);
public void execute(DeserializerManager manager, StructObject jsonObject, DeserializerBuilder.Context context) throws JsonDeserializeException {
StructElement element = DeserializerManager.getFromPath(jsonObject, name);
if (element != null){
try {
Class<?> arrayCls = Array.newInstance(clazz, 0).getClass();
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/rml/deserializer/DeserializerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.util.ResourceLocation;
import rml.deserializer.struct.std.StructElement;
import rml.deserializer.struct.std.StructObject;
import rml.internal.net.minecraftforge.common.util.LazyOptional;
import rml.jrx.announces.EarlyClass;
import rml.jrx.announces.PublicAPI;
Expand All @@ -23,10 +25,10 @@
@EarlyClass
@PublicAPI
public class DeserializerBuilder<T> {
private DeserializerManager manager;
private Class<T> clazz;
private ResourceLocation resourceLocation;
private HashSet<IAction> actions = new HashSet<>();
private final DeserializerManager manager;
private final Class<T> clazz;
private final ResourceLocation resourceLocation;
private final HashSet<IAction> actions = new HashSet<>();
private IJsonObjectFunction<T> function = (context) -> null;
private boolean isDefault = false;
public DeserializerBuilder(DeserializerManager manager, Class<T> clazz, ResourceLocation resourceLocation){
Expand Down Expand Up @@ -58,7 +60,7 @@ public DeserializerBuilder<T> check(final Function<Context, JsonDeserializeExcep
public DeserializerBuilder<T> optionalWhen(final Class<?> clazz,final String name,final Context.ToBooleanFunction isNotRequired){
final Class<?> type = DeserializerBuilder.avoidPrimitive(clazz);
return this.action(((manager, jsonObject, context) -> {
JsonElement element = DeserializerManager.getFromPath(jsonObject, name);
StructElement element = DeserializerManager.getFromPath(jsonObject, name);
if (element != null){
try{
Object obj = manager.decode(type, element);
Expand Down Expand Up @@ -93,7 +95,7 @@ public DeserializerBuilder<T> optional(Class<?> clazz, String name){
public <V> DeserializerBuilder<T> optionalDefaultWhen(final Class<V> clazz, final String name, final Context.ToBooleanFunction isNotRequired, final V defaultValue){
final Class<?> type = DeserializerBuilder.avoidPrimitive(clazz);
return this.action(((manager, jsonObject, context) -> {
JsonElement element = DeserializerManager.getFromPath(jsonObject, name);
StructElement element = DeserializerManager.getFromPath(jsonObject, name);
if (element != null){
try{
Object obj = manager.decode(type, element);
Expand All @@ -116,7 +118,7 @@ public <V> DeserializerBuilder<T> optionalDefault(Class<V> clazz, String name, V
public <V> DeserializerBuilder<T> optionalDefaultLazyWhen(final Class<V> clazz, final String name, final Context.ToBooleanFunction isNotRequired, final LazyOptional<V> defaultValue){
final Class<?> type = DeserializerBuilder.avoidPrimitive(clazz);
return this.action(((manager, jsonObject, context) -> {
JsonElement element = DeserializerManager.getFromPath(jsonObject, name);
StructElement element = DeserializerManager.getFromPath(jsonObject, name);
if (element != null){
try{
Object obj = manager.decode(type, element);
Expand Down Expand Up @@ -239,8 +241,8 @@ public void put(String s, Object obj){

@FunctionalInterface
public interface ToBooleanFunction{
ToBooleanFunction TRUE = (o)->true;
ToBooleanFunction FALSE = (o)->false;
ToBooleanFunction TRUE = (o) -> true;
ToBooleanFunction FALSE = (o) -> false;
boolean apply(Context context);
}

Expand All @@ -249,7 +251,7 @@ public interface ToBooleanFunction{

@FunctionalInterface
public interface IAction{
void execute(DeserializerManager manager, JsonObject jsonObject, Context context) throws JsonDeserializeException;
void execute(DeserializerManager manager, StructObject jsonObject, Context context) throws JsonDeserializeException;
}

@FunctionalInterface
Expand Down

0 comments on commit 721ee17

Please sign in to comment.