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)));
}
}
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;
}
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);
}
}
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;
}
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;
}
Aggregations