Search in sources :

Example 41 with JsonParseException

use of com.google.gson.JsonParseException in project rom-manager by Jakz.

the class JsonPluginAdapter method deserialize.

@SuppressWarnings("unchecked")
@Override
public T deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
    String name = json.getAsJsonObject().get("class").getAsString();
    try {
        Class<?> clazz = Class.forName(name);
        T instance = (T) clazz.newInstance();
        instance.unserialize(json, context);
        return instance;
    } catch (ClassNotFoundException e) {
        throw new JsonParseException(e);
    } catch (IllegalAccessException | InstantiationException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : JsonParseException(com.google.gson.JsonParseException)

Example 42 with JsonParseException

use of com.google.gson.JsonParseException in project tutorials by eugenp.

the class ActorGsonDeserializer method deserialize.

@Override
public ActorGson deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    final JsonObject jsonObject = json.getAsJsonObject();
    final JsonElement jsonImdbId = jsonObject.get("imdbId");
    final JsonElement jsonDateOfBirth = jsonObject.get("dateOfBirth");
    final JsonArray jsonFilmography = jsonObject.getAsJsonArray("filmography");
    final ArrayList<String> filmList = new ArrayList<String>();
    if (jsonFilmography != null) {
        for (int i = 0; i < jsonFilmography.size(); i++) {
            filmList.add(jsonFilmography.get(i).getAsString());
        }
    }
    ActorGson actorGson = null;
    try {
        actorGson = new ActorGson(jsonImdbId.getAsString(), sdf.parse(jsonDateOfBirth.getAsString()), filmList);
    } catch (final ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return actorGson;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) ActorGson(org.baeldung.gson.entities.ActorGson) JsonParseException(com.google.gson.JsonParseException) ParseException(java.text.ParseException)

Example 43 with JsonParseException

use of com.google.gson.JsonParseException in project Binnie by ForestryMC.

the class StyleSheetParser method parseArea.

private static Area parseArea(JsonArray uvArray) {
    int[] ints = new int[uvArray.size()];
    if (ints.length < 1 || ints.length > 4) {
        throw new JsonParseException("Parameter must have between one and four numbers");
    }
    for (int i = 0; i < uvArray.size(); i++) {
        JsonElement object = uvArray.get(i);
        ints[i] = JsonUtils.getInt(object, "uv" + i);
    }
    if (ints.length == 1) {
        return new Area(ints[0]);
    }
    if (ints.length == 2) {
        return new Area(ints[0], ints[1]);
    }
    if (ints.length == 3) {
        return new Area(ints[0], ints[1], ints[2]);
    }
    return new Area(ints[0], ints[1], ints[2], ints[3]);
}
Also used : Area(binnie.core.gui.geometry.Area) JsonElement(com.google.gson.JsonElement) JsonParseException(com.google.gson.JsonParseException)

Example 44 with JsonParseException

use of com.google.gson.JsonParseException in project Binnie by ForestryMC.

the class StyleSheetParser method parseBorder.

private static Border parseBorder(JsonArray array) {
    int[] ints = new int[array.size()];
    if (ints.length < 1 || ints.length > 4) {
        throw new JsonParseException("Parameter must have between one and four numbers");
    }
    for (int i = 0; i < array.size(); i++) {
        JsonElement object = array.get(i);
        ints[i] = JsonUtils.getInt(object, UV_KEY + i);
    }
    if (ints.length == 1) {
        return new Border(ints[0]);
    }
    if (ints.length == 2) {
        return new Border(ints[0], ints[1]);
    }
    if (ints.length == 3) {
        return new Border(ints[0], ints[1], ints[2]);
    }
    return new Border(ints[0], ints[1], ints[2], ints[3]);
}
Also used : JsonElement(com.google.gson.JsonElement) JsonParseException(com.google.gson.JsonParseException) Border(binnie.core.gui.geometry.Border)

Example 45 with JsonParseException

use of com.google.gson.JsonParseException in project motech by motech.

the class ImportManifestReader method readManifest.

public ImportManifest readManifest() throws IOException {
    ImportManifest manifest = new ImportManifest();
    jsonReader.beginArray();
    while (jsonReader.hasNext()) {
        jsonReader.beginObject();
        String entityName = objectReader.readString("entity");
        EntityDefinitionType entityType = objectReader.readEnum("type", EntityDefinitionType.class);
        Entity existingEntity = getExistingEntity(entityName);
        boolean canImport = isImportable(existingEntity, entityType);
        String moduleName = null != existingEntity && existingEntity.isDDE() ? existingEntity.getModule() : "MDS";
        ImportManifest.Record manifestRecord = manifest.addRecord(entityName, moduleName);
        objectReader.expectAndSkip("schema");
        manifestRecord.setCanIncludeSchema(canImport);
        if (jsonReader.hasNext()) {
            objectReader.expectAndSkip("instances");
            manifestRecord.setCanIncludeData(canImport);
        }
        if (jsonReader.hasNext()) {
            throw new JsonParseException("Invalid json format! Unexpected property: " + jsonReader.nextName());
        }
        jsonReader.endObject();
    }
    jsonReader.endArray();
    return manifest;
}
Also used : ImportManifest(org.motechproject.mds.domain.ImportManifest) EntityDefinitionType(org.motechproject.mds.domain.EntityDefinitionType) Entity(org.motechproject.mds.domain.Entity) 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