Example usage for com.google.gson JsonParseException JsonParseException

List of usage examples for com.google.gson JsonParseException JsonParseException

Introduction

In this page you can find the example usage for com.google.gson JsonParseException JsonParseException.

Prototype

public JsonParseException(Throwable cause) 

Source Link

Document

Creates exception with the specified cause.

Usage

From source file:org.apache.rya.indexing.pcj.fluo.app.batch.serializer.BatchInformationTypeAdapter.java

License:Apache License

@Override
public BatchInformation deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
        throws JsonParseException {
    try {/*from   w  w w. ja v a 2s. co m*/
        JsonObject json = arg0.getAsJsonObject();
        String type = json.get("class").getAsString();
        JsonDeserializer<? extends BatchInformation> deserializer = factory.getDeserializerFromName(type);
        return deserializer.deserialize(arg0, arg1, arg2);
    } catch (Exception e) {
        log.trace("Unable to deserialize JsonElement: " + arg0);
        log.trace("Returning an empty Batch");
        throw new JsonParseException(e);
    }
}

From source file:org.apache.rya.periodic.notification.serialization.CommandNotificationTypeAdapter.java

License:Apache License

@Override
public CommandNotification deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
        throws JsonParseException {

    JsonObject json = arg0.getAsJsonObject();
    Command command = Command.valueOf(json.get("command").getAsString());
    String type = json.get("type").getAsString();
    Notification notification = null;
    if (type.equals(PeriodicNotification.class.getSimpleName())) {
        notification = (new PeriodicNotificationTypeAdapter()).deserialize(json.get("notification"),
                PeriodicNotification.class, arg2);
    } else if (type.equals(BasicNotification.class.getSimpleName())) {
        notification = (new BasicNotificationTypeAdapter()).deserialize(json.get("notification"),
                BasicNotification.class, arg2);
    } else {/*from   w  ww . j  a v a  2  s.co  m*/
        throw new JsonParseException("Cannot deserialize Json");
    }

    return new CommandNotification(command, notification);
}

From source file:org.apache.tajo.catalog.json.FunctionAdapter.java

License:Apache License

@Override
public Function deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    String className = CommonGsonHelper.getOrDie(jsonObject, "class").getAsJsonPrimitive().getAsString();

    Class clazz;/*w ww .  j a v a2 s . c om*/
    try {
        clazz = Class.forName(className);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        throw new JsonParseException(e);
    }
    return context.deserialize(CommonGsonHelper.getOrDie(jsonObject, "body"), clazz);
}

From source file:org.apache.tajo.catalog.json.TableDescAdapter.java

License:Apache License

@Override
public TableDesc deserialize(JsonElement json, Type type, JsonDeserializationContext ctx)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    String className = jsonObject.get("classname").getAsJsonPrimitive().getAsString();

    Class clazz = null;/*from  ww  w .  j  av a 2 s . c o  m*/
    try {
        clazz = Class.forName(className);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        throw new JsonParseException(e);
    }
    return ctx.deserialize(jsonObject.get("property"), clazz);
}

From source file:org.apache.tajo.datum.json.DatumAdapter.java

License:Apache License

@Override
public Datum deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    String className = jsonObject.get("classname").getAsJsonPrimitive().getAsString();

    Class clazz;// ww w  .j a  v  a 2 s . c  o  m
    try {
        clazz = Class.forName(className);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        throw new JsonParseException(e);
    }
    return context.deserialize(jsonObject.get("property"), clazz);
}

From source file:org.apache.tajo.json.CommonGsonHelper.java

License:Apache License

/**
 * A helper method that gets a JSON object member value after making sure it exists and has a valid value. Useful when
 * a member value should present to proceed.
 * @param object A JSON object to get a member value from
 * @param memberName The name of a member to get value of
 * @return {@link JsonElement} value read from the given member
 * @throws JsonParseException When the specified member does not exist or have a value.
 *///from w w  w.ja  v a 2  s.  c o  m
public static JsonElement getOrDie(JsonObject object, String memberName) throws JsonParseException {
    if (object.has(memberName)) {
        JsonElement element = object.get(memberName);
        if (!JsonNull.INSTANCE.equals(element)) {
            return element;
        }
    }
    throw new JsonParseException("Field '" + memberName + "' not found in JSON object '" + object + "'");
}

From source file:org.apache.tika.eval.tokens.AnalyzerManager.java

License:Apache License

public static AnalyzerManager newInstance() throws IOException {
    InputStream is = AnalyzerManager.class.getClassLoader().getResourceAsStream("lucene-analyzers.json");
    Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeHierarchyAdapter(Map.class, new AnalyzerDeserializer());
    Gson gson = builder.create();/*from w w  w  .  j a va2s .  co  m*/
    Map<String, Analyzer> map = gson.fromJson(reader, Map.class);
    Analyzer general = map.get(GENERAL);
    Analyzer alphaIdeo = map.get(ALPHA_IDEOGRAPH);
    Analyzer common = map.get(COMMON_TOKENS);
    if (general == null) {
        throw new JsonParseException("Must specify " + GENERAL + " analyzer");
    }
    if (common == null) {
        throw new JsonParseException("Must specify " + COMMON_TOKENS + " analyzer");
    }

    return new AnalyzerManager(general, common);
}

From source file:org.apache.zeppelin.display.RuntimeTypeAdapterFactory.java

License:Apache License

public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
    if (type.getRawType() != baseType) {
        return null;
    }//from w w  w . j  ava  2s  . c om

    final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>();
    final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>();
    for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) {
        TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue()));
        labelToDelegate.put(entry.getKey(), delegate);
        subtypeToDelegate.put(entry.getValue(), delegate);
    }

    return new TypeAdapter<R>() {
        @Override
        public R read(JsonReader in) throws IOException {
            JsonElement jsonElement = Streams.parse(in);
            JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName);
            String label = (labelJsonElement == null ? null : labelJsonElement.getAsString());
            @SuppressWarnings("unchecked") // registration requires that subtype extends T
            TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label);
            if (delegate == null) {
                throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label
                        + "; did you forget to register a subtype?");
            }
            return delegate.fromJsonTree(jsonElement);
        }

        @Override
        public void write(JsonWriter out, R value) throws IOException {
            Class<?> srcType = value.getClass();
            String label = subtypeToLabel.get(srcType);
            @SuppressWarnings("unchecked") // registration requires that subtype extends T
            TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType);
            if (delegate == null) {
                throw new JsonParseException(
                        "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?");
            }
            JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject();
            if (jsonObject.has(typeFieldName) && !srcType.getSimpleName().equals("OldInput")) {
                throw new JsonParseException("cannot serialize " + srcType.getName()
                        + " because it already defines a field named " + typeFieldName);
            }
            JsonObject clone = new JsonObject();
            if (!srcType.getSimpleName().equals("OldInput")) {
                clone.add(typeFieldName, new JsonPrimitive(label));
            }
            for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
                clone.add(e.getKey(), e.getValue());
            }
            Streams.write(clone, out);
        }
    }.nullSafe();
}

From source file:org.apache.zeppelin.notebook.NotebookImportDeserializer.java

License:Apache License

@Override
public Date deserialize(JsonElement jsonElement, Type typeOF, JsonDeserializationContext context)
        throws JsonParseException {
    for (String format : DATE_FORMATS) {
        try {//from  w  ww . j  a  v a2  s.c  om
            return new SimpleDateFormat(format, Locale.US).parse(jsonElement.getAsString());
        } catch (ParseException e) {
        }
    }
    throw new JsonParseException("Unparsable date: \"" + jsonElement.getAsString() + "\". Supported formats: "
            + Arrays.toString(DATE_FORMATS));
}

From source file:org.broad.igv.feature.AminoAcidManager.java

License:Open Source License

/**
 * Load codon tables from the specified path. If any exceptions occur
 * while loading, no changes are made to this instance.
 * <p/>//  ww w. ja va 2s  .  c  o  m
 * Note that the new codon tables are ADDED to the existing tables
 * <p/>
 * The currentCodonTable is set to be the codonTable with id = defaultid if present
 * If not, the first one in the array is set as default
 *
 * @param codonTablesPath
 * @return
 */
synchronized void loadCodonTables(String codonTablesPath) throws IOException, JsonParseException {
    LinkedHashMap<CodonTableKey, CodonTable> newCodonTables = new LinkedHashMap<CodonTableKey, CodonTable>(20);
    CodonTable defaultCodonTable = null;

    InputStream is = AminoAcidManager.class.getResourceAsStream(codonTablesPath);
    if (is == null) {
        is = ParsingUtils.openInputStream(codonTablesPath);
    }

    if (codonTablesPath.endsWith(".json")) {
        JsonObject allData = readJSONFromStream(is);
        int defaultId = -1;
        defaultId = allData.get("defaultid").getAsInt();
        JsonArray codonArray = allData.get("Genetic-code-table").getAsJsonArray();
        if (codonArray.size() == 0) {
            throw new JsonParseException("JSON File has empty array for Genetic-code-table");
        }
        for (int ca = 0; ca < codonArray.size(); ca++) {
            CodonTable curTable = CodonTable.createFromJSON(codonTablesPath,
                    codonArray.get(ca).getAsJsonObject());
            newCodonTables.put(curTable.getKey(), curTable);
            if (defaultCodonTable == null || curTable.getId() == defaultId) {
                defaultCodonTable = curTable;
            }
        }
    } else {
        throw new IllegalArgumentException("Unknown file type, must be .json");
    }

    allCodonTables.putAll(newCodonTables);
    currentCodonTable = defaultCodonTable;
}