Search in sources :

Example 76 with JsonParseException

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;
}
Also used : Ingredient(net.minecraft.item.crafting.Ingredient) JsonElement(com.google.gson.JsonElement) ResourceLocation(net.minecraft.util.ResourceLocation) JsonParseException(com.google.gson.JsonParseException) ItemStack(net.minecraft.item.ItemStack)

Example 77 with JsonParseException

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();
    }
}
Also used : HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) JsonObject(com.google.gson.JsonObject) ExecutionException(java.util.concurrent.ExecutionException) JsonParseException(com.google.gson.JsonParseException)

Example 78 with JsonParseException

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);
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonParseException(com.google.gson.JsonParseException) Timestamp(java.sql.Timestamp)

Example 79 with JsonParseException

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;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) Document(org.jsoup.nodes.Document) JsonParseException(com.google.gson.JsonParseException) JsonParseException(com.google.gson.JsonParseException) JsonParser(com.google.gson.JsonParser) Reflection(com.sdsmdg.harjot.MusicDNA.annotations.Reflection)

Example 80 with JsonParseException

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();
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException)

Aggregations

JsonParseException (com.google.gson.JsonParseException)267 JsonObject (com.google.gson.JsonObject)105 IOException (java.io.IOException)78 JsonElement (com.google.gson.JsonElement)71 Gson (com.google.gson.Gson)39 JsonArray (com.google.gson.JsonArray)30 JsonReader (com.google.gson.stream.JsonReader)28 InputStreamReader (java.io.InputStreamReader)28 JsonParser (com.google.gson.JsonParser)25 JsonPrimitive (com.google.gson.JsonPrimitive)25 Map (java.util.Map)25 InputStream (java.io.InputStream)23 GsonBuilder (com.google.gson.GsonBuilder)20 ArrayList (java.util.ArrayList)20 Type (java.lang.reflect.Type)17 JsonWriter (com.google.gson.stream.JsonWriter)15 StringReader (java.io.StringReader)13 HashMap (java.util.HashMap)13 LinkedHashMap (java.util.LinkedHashMap)13 HttpUrl (okhttp3.HttpUrl)13