Search in sources :

Example 26 with JsonParseException

use of com.google.gson.JsonParseException in project Gaspunk by Ladysnake.

the class GasRecipeDeserializer method deserializeRecipe.

private static void deserializeRecipe(JsonObject json, JsonContext context) {
    String resultName = JsonUtils.getString(json, "result");
    IGas result = ModGases.REGISTRY.getValue(new ResourceLocation(resultName));
    if (result == null)
        throw new JsonParseException("Unrecognized gas: " + resultName);
    JsonObject jsInput = JsonUtils.getJsonObject(json, "input");
    ItemStack in;
    if (jsInput.has("gas"))
        in = getBottle(ModGases.REGISTRY.getValue(new ResourceLocation(JsonUtils.getString(jsInput, "gas"))));
    else
        in = CraftingHelper.getItemStack(jsInput, context);
    JsonObject jsIngredient = JsonUtils.getJsonObject(json, "ingredient");
    String type = JsonUtils.getString(jsIngredient, "type", "minecraft:item");
    if ("forge:ore_dict".equals(type)) {
        String ingredient = JsonUtils.getString(jsIngredient, "ore");
        BrewingRecipeRegistry.addRecipe(new BrewingOreRecipe(in, ingredient, ((ItemGasTube) ModItems.GAS_TUBE).getItemStackFor(result)));
    } else if ("minecraft:item".equals(type)) {
        ItemStack ingredient = CraftingHelper.getItemStack(jsIngredient, context);
        BrewingRecipeRegistry.addRecipe(new BrewingRecipe(in, ingredient, ((ItemGasTube) ModItems.GAS_TUBE).getItemStackFor(result)));
    }
}
Also used : ItemGasTube(ladysnake.gaspunk.item.ItemGasTube) IGas(ladysnake.gaspunk.api.IGas) ResourceLocation(net.minecraft.util.ResourceLocation) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException) ItemStack(net.minecraft.item.ItemStack) BrewingOreRecipe(net.minecraftforge.common.brewing.BrewingOreRecipe) BrewingRecipe(net.minecraftforge.common.brewing.BrewingRecipe)

Example 27 with JsonParseException

use of com.google.gson.JsonParseException in project Gaspunk by Ladysnake.

the class GasRecipeDeserializer method loadRecipes.

private static boolean loadRecipes(Path root, Path file, JsonContext context) {
    String relative = root.relativize(file).toString();
    if (!"json".equals(FilenameUtils.getExtension(file.toString())) || relative.startsWith("_"))
        return true;
    String name = FilenameUtils.removeExtension(relative).replaceAll("\\\\", "/");
    ResourceLocation key = new ResourceLocation(context.getModId(), name);
    try (BufferedReader reader = Files.newBufferedReader(file)) {
        JsonObject json = JsonUtils.fromJson(GSON, reader, JsonObject.class);
        deserializeRecipe(json, context);
    } catch (JsonParseException e) {
        GasPunk.LOGGER.error("Parsing error loading recipe {}", key, e);
        return false;
    } catch (IOException e) {
        GasPunk.LOGGER.error("Couldn't read recipe {} from {}", key, file, e);
        return false;
    }
    return true;
}
Also used : ResourceLocation(net.minecraft.util.ResourceLocation) BufferedReader(java.io.BufferedReader) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException)

Example 28 with JsonParseException

use of com.google.gson.JsonParseException in project java by kubernetes-client.

the class JSON method deserialize.

/**
 * Deserialize the given JSON string to Java object.
 *
 * @param <T>        Type
 * @param body       The JSON string
 * @param returnType The type to deserialize into
 * @return The deserialized Java object
 */
@SuppressWarnings("unchecked")
public <T> T deserialize(String body, Type returnType) {
    try {
        if (isLenientOnJson) {
            JsonReader jsonReader = new JsonReader(new StringReader(body));
            // see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean)
            jsonReader.setLenient(true);
            return gson.fromJson(jsonReader, returnType);
        } else {
            return gson.fromJson(body, returnType);
        }
    } catch (JsonParseException e) {
        // return the response body string directly for the String return type;
        if (returnType.equals(String.class))
            return (T) body;
        else
            throw (e);
    }
}
Also used : StringReader(java.io.StringReader) JsonReader(com.google.gson.stream.JsonReader) ByteString(okio.ByteString) JsonParseException(com.google.gson.JsonParseException)

Example 29 with JsonParseException

use of com.google.gson.JsonParseException in project SpongeCommon by SpongePowered.

the class MixinWorldSettings method onSetGeneratorOptions.

@Inject(method = "setGeneratorOptions", at = @At(value = "RETURN"))
public void onSetGeneratorOptions(String generatorOptions, CallbackInfoReturnable<WorldSettings> cir) {
    // Minecraft uses a String, we want to return a fancy DataContainer
    // Parse the world generator settings as JSON
    DataContainer settings = null;
    try {
        settings = DataFormats.JSON.read(generatorOptions);
    } catch (JsonParseException | IOException ignored) {
    }
    // If null, assume custom
    if (settings == null) {
        settings = DataContainer.createNew().set(DataQueries.WORLD_CUSTOM_SETTINGS, generatorOptions);
    }
    this.generatorSettings = settings;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 30 with JsonParseException

use of com.google.gson.JsonParseException in project kie-wb-common by kiegroup.

the class FormModelSerializer method deserialize.

@Override
public FormModel deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonField = json.getAsJsonObject();
    JsonElement jsonClassName = jsonField.get("formModelType");
    if (jsonClassName != null && !StringUtils.isEmpty(jsonClassName.getAsString())) {
        try {
            return context.deserialize(json, Class.forName(jsonClassName.getAsString()));
        } catch (Exception ex) {
            log.error("Error deserializing formModel", ex);
        }
    }
    return null;
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException)

Aggregations

JsonParseException (com.google.gson.JsonParseException)210 JsonObject (com.google.gson.JsonObject)81 JsonElement (com.google.gson.JsonElement)55 IOException (java.io.IOException)54 Gson (com.google.gson.Gson)34 InputStreamReader (java.io.InputStreamReader)24 JsonArray (com.google.gson.JsonArray)21 InputStream (java.io.InputStream)20 GsonBuilder (com.google.gson.GsonBuilder)18 JsonParser (com.google.gson.JsonParser)18 JsonPrimitive (com.google.gson.JsonPrimitive)18 Type (java.lang.reflect.Type)17 Map (java.util.Map)17 JsonReader (com.google.gson.stream.JsonReader)16 ArrayList (java.util.ArrayList)14 HttpUrl (okhttp3.HttpUrl)13 Request (okhttp3.Request)13 Response (okhttp3.Response)13 JsonSyntaxException (com.google.gson.JsonSyntaxException)10 SocketTimeoutException (java.net.SocketTimeoutException)8