Skip to content

Commit

Permalink
new decoder struction
Browse files Browse the repository at this point in the history
new decode struction
  • Loading branch information
Ecdcaeb committed Dec 29, 2024
1 parent 918a3e8 commit 138fe49
Show file tree
Hide file tree
Showing 24 changed files with 1,225 additions and 56 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
org.gradle.jvmargs = -Xmx3G

java_version = 21
cleanroom_version=0.2.3-alpha
cleanroom_version=0.2.4-alpha

# Mapping Properties
mapping_channel= stable
Expand Down
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
50 changes: 26 additions & 24 deletions src/main/java/rml/deserializer/DeserializerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.util.ResourceLocation;
import rml.deserializer.struct.std.StructArray;
import rml.deserializer.struct.std.StructElement;
import rml.deserializer.struct.std.StructObject;
import rml.jrx.announces.EarlyClass;
import rml.jrx.announces.PublicAPI;
import rml.jrx.utils.ClassHelper;
Expand Down Expand Up @@ -81,15 +84,15 @@ public <T> AbstractDeserializer<T> addDefaultEntry(AbstractDeserializer<T> abst
*
* @throws JsonDeserializeException the exception. Error format? Unexpected context?
*/
public <T> T decode(Class<T> clazz, JsonElement jsonElement) throws JsonDeserializeException {
public <T> T decode(Class<T> clazz, StructElement jsonElement) throws JsonDeserializeException {
ClassHelper.forceInitAll(clazz);
clazz = DeserializerBuilder.avoidPrimitive(clazz);

try {
if (clazz.isArray()){
Class<?> clazzComponentType = clazz.getComponentType();
if (jsonElement.isJsonArray()){
JsonArray array = jsonElement.getAsJsonArray();
if (jsonElement.isArray()){
StructArray array = jsonElement.getAsArray();
Object arrayToReturn = Array.newInstance(clazzComponentType, array.size());
for(int index = 0 ; index < array.size() ; index++){
Array.set(arrayToReturn, index, decode(clazzComponentType, array.get(index)));
Expand All @@ -101,9 +104,9 @@ public <T> T decode(Class<T> clazz, JsonElement jsonElement) throws JsonDeserial
return clazz.cast(arrayToReturn);
}
} else {
if (jsonElement.isJsonObject()){
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("type")){
if (jsonElement.isObject()){
StructObject jsonObject = jsonElement.getAsObject();
if (jsonObject.containsKey("type")){
ResourceLocation decoderName = parseLocation(jsonObject.get("type").getAsString());
if (!registry.containsKey(clazz)) throw new JsonDeserializeException(jsonObject, "Deserializer for " + clazz +" , is not registered.");
HashMap<ResourceLocation, AbstractDeserializer<?>> typedRegistry = registry.get(clazz);
Expand Down Expand Up @@ -131,7 +134,7 @@ public <T> T decode(Class<T> clazz, JsonElement jsonElement) throws JsonDeserial
}


public <T> T decodeSilently(Class<T> clazz, JsonElement jsonElement){
public <T> T decodeSilently(Class<T> clazz, StructElement jsonElement){
JsonDeserializeException exception = null;
try {
return decode(clazz, jsonElement);
Expand Down Expand Up @@ -160,20 +163,19 @@ public DeserializerManager(String defaultDomain){
public DeserializerManager(String defaultDomain, Consumer<DeserializerManager> consumer){
this.defaultDomain = defaultDomain;
ResourceLocation GSON = new ResourceLocation("google", "primitive");
this.addDefaultEntry(new AbstractDeserializer<>(GSON, Integer.class, AbstractDeserializer.safeRun(JsonElement::getAsInt)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, Float.class, AbstractDeserializer.safeRun(JsonElement::getAsFloat)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, Double.class, AbstractDeserializer.safeRun(JsonElement::getAsDouble)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, Long.class, AbstractDeserializer.safeRun(JsonElement::getAsLong)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, Character.class, AbstractDeserializer.safeRun(JsonElement::getAsCharacter)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, Byte.class, AbstractDeserializer.safeRun(JsonElement::getAsByte)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, String.class, AbstractDeserializer.safeRun(JsonElement::getAsString)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, Boolean.class, AbstractDeserializer.safeRun(JsonElement::getAsBoolean)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, Short.class, AbstractDeserializer.safeRun(JsonElement::getAsShort)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, BigInteger.class, AbstractDeserializer.safeRun(JsonElement::getAsBigInteger)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, BigDecimal.class, AbstractDeserializer.safeRun(JsonElement::getAsBigDecimal)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, Number.class, AbstractDeserializer.safeRun(JsonElement::getAsNumber)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, Integer.class, AbstractDeserializer.safeRun(StructElement::getAsInteger)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, Float.class, AbstractDeserializer.safeRun(StructElement::getAsFloat)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, Double.class, AbstractDeserializer.safeRun(StructElement::getAsDouble)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, Long.class, AbstractDeserializer.safeRun(StructElement::getAsLong)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, Character.class, AbstractDeserializer.safeRun(StructElement::getAsCharacter)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, Byte.class, AbstractDeserializer.safeRun(StructElement::getAsByte)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, String.class, AbstractDeserializer.safeRun(StructElement::getAsString)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, Boolean.class, AbstractDeserializer.safeRun(StructElement::getAsBoolean)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, Short.class, AbstractDeserializer.safeRun(StructElement::getAsShort)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, BigInteger.class, AbstractDeserializer.safeRun(StructElement::getAsBigInteger)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, BigDecimal.class, AbstractDeserializer.safeRun(StructElement::getAsBigDecimal)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, Number.class, AbstractDeserializer.safeRun((number)->number.getAsNumber().castToNumber())));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, Void.class, AbstractDeserializer.safeRun((jsonElement)->null)));
this.addDefaultEntry(new AbstractDeserializer<>(GSON, JsonObject.class, AbstractDeserializer.safeRun(JsonElement::getAsJsonObject)));
consumer.accept(this);
}

Expand Down Expand Up @@ -203,15 +205,15 @@ public <T, F> AbstractDeserializer<T> map(final Class<T> tClass, final Class<F>
return new AbstractDeserializer<T>(resourceLocation, tClass, jsonElement -> function.apply(DeserializerManager.this.decode(fClass, jsonElement)));
}

public static JsonElement getFromPath(JsonObject jsonObject, String path){
public static StructElement getFromPath(StructObject jsonObject, String path){
int i = path.indexOf('.');
if (i == -1){
return jsonObject.get(path);
}else {
String first = path.substring(0, i-1);
JsonElement element1 = jsonObject.get(first);
if (element1 instanceof JsonObject){
return getFromPath((JsonObject)element1, path.substring(i+1));
StructElement element1 = jsonObject.get(first);
if (element1.isObject()){
return getFromPath(element1.getAsObject(), path.substring(i+1));
}else return null;
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/rml/deserializer/JsonDeserializeException.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package rml.deserializer;

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

Expand All @@ -13,17 +13,17 @@
@EarlyClass
@PublicAPI
public class JsonDeserializeException extends Exception{
public JsonDeserializeException(JsonElement element, Throwable throwable){
public JsonDeserializeException(StructElement element, Throwable throwable){
super(String.valueOf(element), throwable);
}

public JsonDeserializeException(JsonElement element, String s){
public JsonDeserializeException(StructElement element, String s){
super(s + ", " + element);
}
public JsonDeserializeException(JsonElement element){
public JsonDeserializeException(StructElement element){
super(String.valueOf(element));
}
public JsonDeserializeException(JsonElement element, String s, Throwable throwable){
public JsonDeserializeException(StructElement element, String s, Throwable throwable){
super(s + ", " + element, throwable);
}
}
Loading

0 comments on commit 138fe49

Please sign in to comment.