Search in sources :

Example 91 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException in project BuildCraft by BuildCraft.

the class ElementTypeStatementParam method deserialize0.

@Override
public IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    if (!json.properties.containsKey("size[0]")) {
        json.properties.put("size[0]", "18");
    }
    if (!json.properties.containsKey("size[1]")) {
        json.properties.put("size[1]", "18");
    }
    inheritProperty(json, "pos[0]", "area[0]");
    inheritProperty(json, "pos[1]", "area[1]");
    inheritProperty(json, "size[0]", "area[2]");
    inheritProperty(json, "size[1]", "area[3]");
    IGuiArea area = resolveArea(json, "area", parent, ctx);
    String source;
    if (json.properties.containsKey("source")) {
        source = json.properties.get("source");
    } else {
        source = resolveEquation(json, "source_expression", ctx);
        if (source == null) {
            throw new JsonSyntaxException("Expected either 'source' or 'source_expression' for " + NAME);
        }
    }
    boolean draw = !"false".equals(json.properties.get("draw"));
    int index = resolveEquationInt(json, "index", ctx);
    FullStatement<?> fullStatement = gui.properties.get(source, FullStatement.class);
    IStatementContainer statementContainer = gui.properties.get("statement.container", IStatementContainer.class);
    return new GuiElementStatementParam(gui, area, statementContainer, fullStatement, index, draw);
}
Also used : IGuiArea(buildcraft.lib.gui.pos.IGuiArea) JsonSyntaxException(com.google.gson.JsonSyntaxException) IStatementContainer(buildcraft.api.statements.IStatementContainer) GuiElementStatementParam(buildcraft.lib.gui.statement.GuiElementStatementParam) FunctionContext(buildcraft.lib.expression.FunctionContext)

Example 92 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException in project BuildCraft by BuildCraft.

the class ElementTypeToolTip method deserialize0.

// Args:
// - pos[0], pos[1]: the area for help (where it will be drawn, relative to the root of the gui). Defaults
// to 0,0
// - size[0], size[1]: the size of the help area
// - area[0-3]: mapping for pos[0], pos[1], size[0], size[1]
// - text: The text to display in the tooltip
// - expression: The expression to display in the tooltip
@Override
public IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    List<String> text = new ArrayList<>();
    String key = "text";
    boolean isExpression = false;
    if (json.properties.containsKey("expression") || json.properties.containsKey("expression[0]")) {
        key = "expression";
        isExpression = true;
    }
    if (json.properties.containsKey(key + "[0]")) {
        int i = 0;
        while (true) {
            String prop = json.properties.get(key + "[" + i + "]");
            if (prop == null) {
                break;
            }
            text.add(prop);
            i++;
        }
    } else {
        text.add(json.properties.getOrDefault(key, "ERROR: Text not given!"));
    }
    INodeBoolean visible = getEquationBool(json, "visible", ctx, true);
    ITooltipElement source;
    if (isExpression) {
        List<INodeObject<String>> nodes = new ArrayList<>(text.size());
        try {
            for (String s : text) {
                nodes.add(GenericExpressionCompiler.compileExpressionString(s, ctx));
            }
        } catch (InvalidExpressionException e) {
            throw new JsonSyntaxException(e);
        }
        source = (list) -> {
            if (visible.evaluate()) {
                String[] arr = new String[nodes.size()];
                for (int i = 0; i < arr.length; i++) {
                    arr[i] = nodes.get(i).evaluate();
                }
                list.add(ToolTip.createLocalized(arr));
            }
        };
    } else {
        ToolTip tooltip = ToolTip.createLocalized(text.toArray(new String[0]));
        source = (list) -> {
            if (visible.evaluate()) {
                list.add(tooltip);
            }
        };
    }
    inheritProperty(json, "pos[0]", "area[0]");
    inheritProperty(json, "pos[1]", "area[1]");
    inheritProperty(json, "size[0]", "area[2]");
    inheritProperty(json, "size[1]", "area[3]");
    IGuiArea area = resolveArea(json, "area", parent, ctx);
    return new GuiElementToolTip(gui, area, source);
}
Also used : GuiElementToolTip(buildcraft.lib.gui.GuiElementToolTip) ToolTip(buildcraft.lib.gui.elem.ToolTip) IGuiArea(buildcraft.lib.gui.pos.IGuiArea) ITooltipElement(buildcraft.lib.gui.ITooltipElement) ArrayList(java.util.ArrayList) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) FunctionContext(buildcraft.lib.expression.FunctionContext) JsonSyntaxException(com.google.gson.JsonSyntaxException) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) GuiElementToolTip(buildcraft.lib.gui.GuiElementToolTip) INodeObject(buildcraft.lib.expression.api.IExpressionNode.INodeObject)

Example 93 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException in project BuildCraft by BuildCraft.

the class JsonGuiElement method getChildElement.

public JsonGuiElement getChildElement(String childName, JsonElement elem) {
    JsonElement value = elem;
    if (!value.isJsonObject()) {
        throw new JsonSyntaxException("Expected an object, got " + value);
    }
    JsonObject childObject = value.getAsJsonObject();
    return new JsonGuiElement(childObject, childName, fullName + "." + childName, types, context);
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject)

Example 94 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException in project BuildCraft by BuildCraft.

the class JsonModel method deserializePartArray.

private static JsonModelPart[] deserializePartArray(JsonObject json, String member, boolean translucent, ResourceLoaderContext ctx) throws JsonParseException, IOException {
    if (!json.has(member)) {
        throw new JsonSyntaxException("Did not have '" + member + "' in '" + json + "'");
    }
    JsonElement elem = json.get(member);
    if (!elem.isJsonArray()) {
        throw new JsonSyntaxException("Expected an array, got '" + elem + "'");
    }
    JsonArray array = elem.getAsJsonArray();
    List<JsonModelPart> to = new ArrayList<>(array.size());
    for (int i = 0; i < array.size(); i++) {
        deserializePart(to, translucent, array.get(i), ctx);
    }
    return to.toArray(new JsonModelPart[to.size()]);
}
Also used : JsonArray(com.google.gson.JsonArray) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList)

Example 95 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException in project BuildCraft by BuildCraft.

the class JsonModelRule method deserialize.

public static JsonModelRule deserialize(JsonElement json, FunctionContext fnCtx, ResourceLoaderContext ctx) {
    if (!json.isJsonObject()) {
        throw new JsonSyntaxException("Expected an object, got " + json);
    }
    JsonObject obj = json.getAsJsonObject();
    String when = JsonUtils.getString(obj, "when");
    INodeBoolean nodeWhen = JsonVariableModelPart.convertStringToBooleanNode(when, fnCtx);
    String type = JsonUtils.getString(obj, "type");
    if (type.startsWith("builtin:")) {
        String builtin = type.substring("builtin:".length());
        if ("rotate_facing".equals(builtin)) {
            fnCtx = new FunctionContext(fnCtx, ExpressionCompat.ENUM_FACING);
            String from = JsonUtils.getString(obj, "from");
            INodeObject<EnumFacing> nodeFrom = JsonVariableModelPart.convertStringToObjectNode(from, fnCtx, EnumFacing.class);
            String to = JsonUtils.getString(obj, "to");
            INodeObject<EnumFacing> nodeTo = JsonVariableModelPart.convertStringToObjectNode(to, fnCtx, EnumFacing.class);
            INodeDouble[] origin;
            if (obj.has("origin")) {
                origin = JsonVariableModelPart.readVariablePosition(obj, "origin", fnCtx);
            } else {
                origin = RuleRotateFacing.DEFAULT_ORIGIN;
            }
            return new RuleRotateFacing(nodeWhen, nodeFrom, nodeTo, origin);
        } else if ("rotate".equals(builtin)) {
            INodeDouble[] origin;
            if (obj.has("origin")) {
                origin = JsonVariableModelPart.readVariablePosition(obj, "origin", fnCtx);
            } else {
                origin = RuleRotate.DEFAULT_ORIGIN;
            }
            INodeDouble[] angles = JsonVariableModelPart.readVariablePosition(obj, "angle", fnCtx);
            return new RuleRotate(nodeWhen, origin, angles);
        } else if ("scale".equals(builtin)) {
            INodeDouble[] origin;
            if (obj.has("origin")) {
                origin = JsonVariableModelPart.readVariablePosition(obj, "origin", fnCtx);
            } else {
                origin = RuleRotate.DEFAULT_ORIGIN;
            }
            INodeDouble[] scales = JsonVariableModelPart.readVariablePosition(obj, "scale", fnCtx);
            return new RuleScale(nodeWhen, origin, scales);
        } else {
            throw new JsonSyntaxException("Unknown built in rule type '" + builtin + "'");
        }
    } else {
        throw new JsonSyntaxException("Unknown rule type '" + type + "'");
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) EnumFacing(net.minecraft.util.EnumFacing) JsonObject(com.google.gson.JsonObject) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) FunctionContext(buildcraft.lib.expression.FunctionContext)

Aggregations

JsonSyntaxException (com.google.gson.JsonSyntaxException)379 Gson (com.google.gson.Gson)169 IOException (java.io.IOException)83 HashMap (java.util.HashMap)68 JsonElement (com.google.gson.JsonElement)62 JsonObject (com.google.gson.JsonObject)58 ArrayList (java.util.ArrayList)46 JsonParser (com.google.gson.JsonParser)42 GsonBuilder (com.google.gson.GsonBuilder)38 Cursor (android.database.Cursor)33 InputStreamReader (java.io.InputStreamReader)30 Map (java.util.Map)30 BadRequestException (co.cask.cdap.common.BadRequestException)28 JsonArray (com.google.gson.JsonArray)28 Path (javax.ws.rs.Path)28 Reader (java.io.Reader)26 Type (java.lang.reflect.Type)23 JsonIOException (com.google.gson.JsonIOException)21 FileReader (java.io.FileReader)21 File (java.io.File)19