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:elaborate.tag_analysis.oosm.impl.gson.BaseInterfaceDeserializer.java

@Override
public T deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException {
    try {/*from  ww w  .  jav a  2 s. co  m*/
        T object = (T) this.createInstance();
        JsonObject jsonObject = (JsonObject) je;
        Iterator<Entry<String, JsonElement>> it = jsonObject.entrySet().iterator();
        //iterate through class fields
        while (it.hasNext()) {
            try {
                Entry<String, JsonElement> entry = it.next();
                JsonElement jsonElement = entry.getValue();
                Field field = getField(entry.getKey());
                if (jsonElement.isJsonArray()) {
                    Object arrayValue = this.processArrayElement(entry.getKey(), jsonElement.getAsJsonArray(),
                            field, jdc);
                    PropertyUtils.setProperty(object, entry.getKey(), arrayValue);
                } else {
                    Object normalValue = this.processNormalElement(entry.getKey(), jsonElement, field, jdc);
                    PropertyUtils.setProperty(object, entry.getKey(), normalValue);
                    //                        if(field.getName().equals("type")){
                    //                            System.out.println(( (OOSMElement)object ).getType());
                    //                        }
                }
            } catch (Exception ex) {
                //System.out.println(object.getClass());
                Logger.getLogger(BaseInterfaceDeserializer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return object;
    } catch (Exception ex) {
        Logger.getLogger(BaseInterfaceDeserializer.class.getName()).log(Level.SEVERE, null, ex);
        throw new JsonParseException(ex);
    }
}

From source file:eu.crushedpixel.littlstar.api.gson.RubyDateDeserializer.java

License:Apache License

@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    try {/* w  w  w . j a v  a2s  .com*/
        return RUBY_DATE_FORMAT.parse(json.getAsString());
    } catch (ParseException e) {
        throw new JsonParseException(e);
    }
}

From source file:eu.focusnet.app.model.gson.DateTypeAdapter.java

License:Open Source License

/**
 * Deserialization/*  w ww  .ja v  a2 s .  c o m*/
 *
 * @param jsonElement                The GSON {@link JsonElement} to deserialize into at {@link Date}
 * @param type                       inherited
 * @param jsonDeserializationContext inherited
 * @return A {@link Date} object
 */
@Override
public synchronized Date deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) {
    try {
        return dateFormat.parse(jsonElement.getAsString());
    } catch (ParseException e) {
        throw new JsonParseException(e);
    }
}

From source file:ezbake.services.search.utils.DateSerializer.java

License:Apache License

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

From source file:fr.dudie.keolis.client.KeoUtils.java

License:Open Source License

/**
 * Checks the response code of the keolis API and return the JSON Object named "data".
 * /* w w w  .  j  a  va 2  s .co m*/
 * @param apiResponse
 *            the response of the keolis API
 * @param <T>
 *            type of data returned by the API
 * @throws JSONException
 *             the response does not contains valid data
 */
public static <T> void checkResponse(final ApiResponse<T> apiResponse) throws JsonParseException {

    final StatusAttributes attributes = apiResponse.getOpendata().getAnswer().getStatus().getAttributes();

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("checking keolis response - code = {}, message = {}", attributes.getCode(),
                attributes.getMessage());
    }

    if (attributes.getCode() != 0) {
        final String message = String.format("Keolis API error : code=%S, %s ", attributes.getCode(),
                attributes.getMessage());
        throw new JsonParseException(message);
    }
}

From source file:fr.inria.atlanmod.discoverer.JsonSource.java

License:Open Source License

/**
 * Adds a new JSON definition from a file with the JSON as well as the input to get such a
 * definition. The input must be provided as a valid JSON object.
 * /*from   www . ja v  a  2  s.c  o m*/
 * Warning: If the JSON source already includes JSON Data, the provision of input must match
 * 
 * @param file
 * @param input
 * @throws FileNotFoundException
 */
public void addJsonDef(File file, String input) throws FileNotFoundException {
    if (file == null || !file.exists())
        throw new IllegalArgumentException("File cannot be null and must exist");
    if (input == null || input.equals(""))
        throw new IllegalArgumentException("Argument cannot be null or empty");
    if (this.jsonData.size() > 0 && this.withInput == false)
        throw new IllegalStateException("This JSON source was created to hold JSON data *without* input");

    JsonElement inputElement = (new JsonParser()).parse(new JsonReader(new StringReader(input)));
    if (!inputElement.isJsonObject())
        throw new JsonParseException("The input value must be a valid JSON object. Received " + input);

    JsonElement rootElement = (new JsonParser()).parse(new JsonReader(new FileReader(file)));
    JsonData data = new JsonData(inputElement.getAsJsonObject(), rootElement);
    getJsonData().add(data);
    this.withInput = true;
}

From source file:fr.inria.atlanmod.discoverer.JsonSource.java

License:Open Source License

/**
 * Adds a new JSON definition from a string with the JSON and a provided input
 * //from  w ww . ja v a 2 s.  c o m
 * @param file
 * @param input
 * @throws FileNotFoundException 
 */
public void addJsonDef(String string, String input) {
    if (string == null || string.equals(""))
        throw new IllegalArgumentException("Argument cannot be null or empty");
    if (this.jsonData.size() > 0 && this.withInput == true)
        throw new IllegalStateException("This JSON source was created to hold JSON data *with* input");
    if (input == null || input.equals(""))
        throw new IllegalArgumentException("Argument cannot be null or empty");

    JsonElement inputElement = (new JsonParser()).parse(new JsonReader(new StringReader(input)));
    if (!inputElement.isJsonObject())
        throw new JsonParseException("The input value must be a valid JSON object. Received " + input);

    JsonElement rootElement = (new JsonParser()).parse(new JsonReader(new StringReader(string)));
    JsonData data = new JsonData(inputElement.getAsJsonObject(), rootElement);
    getJsonData().add(data);
    this.withInput = true;
}

From source file:gov.nasa.jpf.jdart.summaries.json.ExpressionHandler.java

License:Open Source License

@Override
public Expression<?> deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
        throws JsonParseException {
    try {//from  w  ww. j a  v a2s  .  c om
        String s = je.getAsJsonPrimitive().getAsString();
        if (s.startsWith("[V]")) {
            return ParserUtil.parseVariable(s.substring(3));
        } else if (s.startsWith("[A]")) {
            return ParserUtil.parseArithmetic(s.substring(3));
        }
        return ParserUtil.parseLogical(s.substring(3));
    } catch (RecognitionException ex) {
        throw new JsonParseException(ex);
    }
}

From source file:gov.nasa.jpf.jdart.summaries.json.SubClassHandler.java

License:Open Source License

@Override
public T deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException {
    JsonObject jo = je.getAsJsonObject();
    String className = jo.get(SUB_CLASS).getAsString();
    try {/*from  www .ja  v  a 2  s . c o m*/
        Class<?> clazz = Class.forName(className);
        return jdc.deserialize(je, clazz);
    } catch (ClassNotFoundException e) {
        throw new JsonParseException(e);
    }
}

From source file:gov.nasa.jpf.jdart.summaries.json.VariableHandler.java

License:Open Source License

@Override
public Variable<?> deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
        throws JsonParseException {
    try {/*w w w  . ja  va 2 s  .  c  o  m*/
        Expression<Boolean> expr = ParserUtil.parseVariable(je.getAsJsonPrimitive().getAsString());
        return (Variable<?>) expr;
    } catch (RecognitionException ex) {
        throw new JsonParseException(ex);
    }
}