use of com.google.gson.JsonParseException in project ImmersiveEngineering by BluSunrize.
the class RecipeFactoryShapelessIngredient method parse.
@Override
public IRecipe parse(JsonContext context, JsonObject json) {
String group = JsonUtils.getString(json, "group", "");
NonNullList<Ingredient> ings = NonNullList.create();
for (JsonElement ele : JsonUtils.getJsonArray(json, "ingredients")) ings.add(CraftingHelper.getIngredient(ele, context));
if (ings.isEmpty())
throw new JsonParseException("No ingredients for shapeless recipe");
ItemStack result = CraftingHelper.getItemStack(JsonUtils.getJsonObject(json, "result"), context);
RecipeShapelessIngredient recipe = new RecipeShapelessIngredient(group.isEmpty() ? null : new ResourceLocation(group), result, ings);
if (JsonUtils.hasField(json, "damage_tool"))
recipe.setToolDamageRecipe(JsonUtils.getInt(json, "damage_tool"));
if (JsonUtils.hasField(json, "copy_nbt"))
recipe.setNBTCopyTargetRecipe(JsonUtils.getInt(json, "copy_nbt"));
return recipe;
}
use of com.google.gson.JsonParseException in project ion by koush.
the class GsonTests method testJunkPayload.
public void testJunkPayload() throws Exception {
AsyncHttpServer httpServer = new AsyncHttpServer();
try {
httpServer.get("/", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
response.send("not json!");
}
});
httpServer.listen(5555);
Future<JsonObject> ret = Ion.with(getContext()).load("PUT", "http://localhost:5555/").asJsonObject();
ret.get();
fail();
} catch (ExecutionException e) {
assertTrue(e.getCause() instanceof JsonParseException);
} finally {
httpServer.stop();
AsyncServer.getDefault().stop();
}
}
use of com.google.gson.JsonParseException in project gerrit by GerritCodeReview.
the class SqlTimestampDeserializer method deserialize.
@Override
public Timestamp deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json.isJsonNull()) {
return null;
}
if (!json.isJsonPrimitive()) {
throw new JsonParseException("Expected string for timestamp type");
}
JsonPrimitive p = (JsonPrimitive) json;
if (!p.isString()) {
throw new JsonParseException("Expected string for timestamp type");
}
String input = p.getAsString();
if (input.trim().isEmpty()) {
// introduce an undesired dependency.
return new Timestamp(0);
}
return JavaSqlTimestampHelper.parseTimestamp(input);
}
use of com.google.gson.JsonParseException in project MusicDNA by harjot-oberai.
the class MetalArchives method fromMetaData.
@Reflection
public static Lyrics fromMetaData(String artist, String title) {
String baseURL = "http://www.metal-archives.com/search/ajax-advanced/searching/songs/?bandName=%s&songTitle=%s&releaseType[]=1&exactSongMatch=1&exactBandMatch=1";
String urlArtist = artist.replaceAll("\\s", "+");
String urlTitle = title.replaceAll("\\s", "+");
String url;
String text;
try {
String response = Net.getUrlAsString(String.format(baseURL, urlArtist, urlTitle));
JsonObject jsonResponse = new JsonParser().parse(response).getAsJsonObject();
JsonArray track = jsonResponse.getAsJsonArray("aaData").get(0).getAsJsonArray();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < track.size(); i++) builder.append(track.get(i).getAsString());
Document trackDocument = Jsoup.parse(builder.toString());
url = trackDocument.getElementsByTag("a").get(1).attr("href");
String id = trackDocument.getElementsByClass("viewLyrics").get(0).id().substring(11);
text = Jsoup.connect("http://www.metal-archives.com/release/ajax-view-lyrics/id/" + id).get().body().html();
} catch (JsonParseException | IndexOutOfBoundsException e) {
return new Lyrics(NO_RESULT);
} catch (Exception e) {
return new Lyrics(ERROR);
}
Lyrics lyrics = new Lyrics(POSITIVE_RESULT);
lyrics.setArtist(artist);
lyrics.setTitle(title);
lyrics.setText(text);
lyrics.setSource(domain);
lyrics.setURL(url);
return lyrics;
}
use of com.google.gson.JsonParseException in project immutables by immutables.
the class HolderJsonSerializer method deserialize.
@Override
public Holder deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonObject root = (JsonObject) json;
ImmutableHolder.Builder builder = ImmutableHolder.builder();
if (root.has("id")) {
builder.id(root.get("id").getAsString());
}
JsonElement value = root.get(VALUE_PROPERTY);
if (value == null) {
throw new JsonParseException(String.format("%s not found for %s in JSON", VALUE_PROPERTY, type));
}
if (value.isJsonObject()) {
final String valueTypeName = value.getAsJsonObject().get(Holder.TYPE_PROPERTY).getAsString();
try {
Class<?> valueType = Class.forName(valueTypeName);
builder.value(context.deserialize(value, valueType));
} catch (ClassNotFoundException e) {
throw new JsonParseException(String.format("Couldn't construct value class %s for %s", valueTypeName, type), e);
}
} else if (value.isJsonPrimitive()) {
final JsonPrimitive primitive = value.getAsJsonPrimitive();
if (primitive.isString()) {
builder.value(primitive.getAsString());
} else if (primitive.isNumber()) {
builder.value(primitive.getAsInt());
} else if (primitive.isBoolean()) {
builder.value(primitive.getAsBoolean());
}
} else {
throw new JsonParseException(String.format("Couldn't deserialize %s : %s. Not a primitive or object", VALUE_PROPERTY, value));
}
return builder.build();
}
Aggregations