42 lines
1.7 KiB
Java
42 lines
1.7 KiB
Java
|
package io.gitlab.jfronny.respackopts.gson;
|
||
|
|
||
|
import io.gitlab.jfronny.gson.*;
|
||
|
import io.gitlab.jfronny.muscript.*;
|
||
|
import io.gitlab.jfronny.muscript.compiler.*;
|
||
|
|
||
|
import java.lang.reflect.Type;
|
||
|
import java.util.*;
|
||
|
|
||
|
public class ExprDeserializer implements JsonDeserializer<Expr<?>> {
|
||
|
private static final Map<String, Expr<?>> compiledScripts = new HashMap<>();
|
||
|
|
||
|
@Override
|
||
|
public Expr<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||
|
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isString()) {
|
||
|
String s = json.getAsJsonPrimitive().getAsString();
|
||
|
if (compiledScripts.containsKey(s))
|
||
|
return compiledScripts.get(s);
|
||
|
try {
|
||
|
Expr<?> expr = Parser.parse(AttachmentHolder.getAttachedVersion() <= 7
|
||
|
? StarScriptIngester.starScriptToMu(s)
|
||
|
: s);
|
||
|
compiledScripts.put(s, expr);
|
||
|
return expr;
|
||
|
} catch (Parser.ParseException e) {
|
||
|
throw new JsonParseException("Could not create script", e);
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
throw new JsonParseException("Could not parse script: Expected string");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static class StringX implements JsonDeserializer<Expr.StringExpr> {
|
||
|
@Override
|
||
|
public Expr.StringExpr deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||
|
Expr<?> expr = jsonDeserializationContext.deserialize(jsonElement, Expr.class);
|
||
|
return expr.asStringExpr();
|
||
|
}
|
||
|
}
|
||
|
}
|