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:com.sias.util.CustomDateJsonSerializer.java

@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    String asString = json.getAsString();
    try {//from   www  . j av  a 2s  .  c  o  m
        if (DATE_PATTERN.matcher(asString).matches()) {
            return getDateFormat().parse(asString);
        } else if (DATE_PATTERN_EN.matcher(asString).matches()) {
            return getDateFormatEN().parse(asString);
        } else if (DATE_TIME_PATTERN.matcher(asString).matches()) {
            return getDateTimeFormat().parse(asString);
        } else if (DATE_TIME_PATTERN_WITH_SECONDS.matcher(asString).matches()) {
            return getDateTimeFormatWithSeconds().parse(asString);
        } else {
            throw new JsonParseException("Could not parse to date: " + json);
        }
    } catch (java.text.ParseException ex) {
        Logger.getLogger(CustomDateJsonSerializer.class.getName()).log(Level.SEVERE, null, ex);
        throw new JsonParseException("Could not parse to date: " + json);
    }
}

From source file:com.singhinderjeet.gracenoteapi.json.UtcDateTypeAdapter.java

License:Apache License

@SuppressWarnings("deprecation")
@Override/* www  . j av  a2s . co m*/
public Date read(JsonReader in) throws IOException {
    switch (in.peek()) {
    case NULL:
        in.nextNull();
        return null;
    default:
        String date = in.nextString();
        // Instead of using iso8601Format.parse(value), we use Jackson's date parsing
        // This is because Android doesn't support XXX because it is JDK 1.6
        try {
            return parse(date, new ParsePosition(0));
        } catch (ParseException e) {
            try {
                return new Date(date);
            } catch (IllegalArgumentException iae) {
                try {
                    return dateFormat.get().parse(date);
                } catch (ParseException pe) {
                    throw new JsonParseException(pe);
                }
            }
        }
    }
}

From source file:com.sk89q.worldedit.util.gson.BlockVectorAdapter.java

License:Open Source License

@Override
public BlockVector3 deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonArray jsonArray = json.getAsJsonArray();
    if (jsonArray.size() != 3) {
        throw new JsonParseException("Expected array of 3 length for BlockVector3");
    }//  w w  w  .j av  a  2s.c o  m

    double x = jsonArray.get(0).getAsInt();
    double y = jsonArray.get(1).getAsInt();
    double z = jsonArray.get(2).getAsInt();

    return BlockVector3.at(x, y, z);
}

From source file:com.sk89q.worldedit.util.gson.VectorAdapter.java

License:Open Source License

@Override
public Vector deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonArray jsonArray = json.getAsJsonArray();
    if (jsonArray.size() != 3) {
        throw new JsonParseException("Expected array of 3 length for Vector");
    }//from ww  w.j ava2  s.co  m

    double x = jsonArray.get(0).getAsDouble();
    double y = jsonArray.get(1).getAsDouble();
    double z = jsonArray.get(2).getAsDouble();

    return new Vector(x, y, z);
}

From source file:com.smartling.cms.gateway.client.internal.CommandTypeAdapter.java

License:Apache License

@Override
public BaseCommand deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    JsonPrimitive prim = jsonObject.getAsJsonPrimitive("cmd");
    String commandName = prim.getAsString();

    if (commandName.equalsIgnoreCase("getResource")) {
        String requestId = jsonObject.getAsJsonPrimitive("rid").getAsString();
        String fileUri = jsonObject.getAsJsonPrimitive("uri").getAsString();
        return new GetResourceCommand(requestId, fileUri);
    }/*w  w  w  .ja  v a2  s .c om*/

    if (commandName.equalsIgnoreCase("getHtml")) {
        String requestId = jsonObject.getAsJsonPrimitive("rid").getAsString();
        String fileUri = jsonObject.getAsJsonPrimitive("uri").getAsString();
        return new GetHtmlCommand(requestId, fileUri);
    }

    if (commandName.equalsIgnoreCase("authenticationSuccess")) {
        return new AuthenticationSuccessCommand();
    }

    if (commandName.equalsIgnoreCase("authenticationError")) {
        return new AuthenticationErrorCommand();
    }

    if (commandName.equalsIgnoreCase("disconnect")) {
        JsonPrimitive reason = jsonObject.getAsJsonPrimitive("message");
        String message = reason == null ? null : reason.getAsString();
        return new DisconnectCommand(message);
    }

    throw new JsonParseException("Unknown command " + commandName);
}

From source file:com.softwaremagico.tm.json.InterfaceAdapter.java

License:Open Source License

public Class<?> getObjectClass(String className) {
    try {/* w w  w .  j ava2s . co  m*/
        return Class.forName(className);
    } catch (ClassNotFoundException e) {
        throw new JsonParseException(e.getMessage());
    }
}

From source file:com.softwarementors.extjs.djn.test.config.GsonBuilderConfiguratorForTesting.java

License:Open Source License

private static int getIntValue(JsonObject parent, String elementName) {
    assert parent != null;
    assert !StringUtils.isEmpty(elementName);

    JsonElement element = parent.get(elementName);
    if (!element.isJsonPrimitive()) {
        throw new JsonParseException("Element + '" + elementName + "' must be a valid integer");
    }/* www.ja  v  a  2  s  . co  m*/
    JsonPrimitive primitiveElement = (JsonPrimitive) element;
    if (!primitiveElement.isNumber()) {
        throw new JsonParseException("Element + '" + elementName + "' must be a valid integer");
    }
    return primitiveElement.getAsInt();
}

From source file:com.srotya.tau.wraith.actions.ActionSerializer.java

License:Apache License

public Action deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    if (jsonObject.entrySet().isEmpty()) {
        throw new JsonParseException("Empty action are not allowed");
    }/*from w  w w  .  j  ava  2s .  c o  m*/
    String type = jsonObject.get(TYPE).getAsString();
    if (Utils.CLASSNAME_REVERSE_MAP.containsKey(type)) {
        type = Utils.CLASSNAME_REVERSE_MAP.get(type);
    }
    JsonElement element = jsonObject.get(PROPS);
    try {
        Action pojo = context.deserialize(element, Class.forName(type));
        Field[] fields = pojo.getClass().getDeclaredFields();
        for (Field f : fields) {
            if (f.getAnnotation(Required.class) != null) {
                try {
                    f.setAccessible(true);
                    if (f.get(pojo) == null) {
                        throw new JsonParseException("Missing required field in condition: " + f.getName());
                    }
                } catch (IllegalArgumentException | IllegalAccessException ex) {
                }
            }
        }
        return pojo;
    } catch (ClassNotFoundException cnfe) {
        throw new JsonParseException("Unknown action type: " + type, cnfe);
    } catch (NumberFormatException e) {
        throw new JsonParseException("Type must be a number:" + e.getLocalizedMessage());
    }
}

From source file:com.srotya.tau.wraith.conditions.ConditionSerializer.java

License:Apache License

public Condition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    if (jsonObject.entrySet().isEmpty()) {
        throw new JsonParseException("Empty conditions are not allowed");
    }//from ww  w  .j a va 2  s  .  c  o  m
    String type = jsonObject.get(TYPE).getAsString();
    if (Utils.CLASSNAME_REVERSE_MAP.containsKey(type)) {
        type = Utils.CLASSNAME_REVERSE_MAP.get(type);
    }
    JsonElement element = jsonObject.get(PROPS);
    try {
        Condition pojo = context.deserialize(element, Class.forName(type));
        if (pojo instanceof JavaRegexCondition) {
            JavaRegexCondition regex = ((JavaRegexCondition) pojo);
            if (regex.getValue() == null) {
                throw new JsonParseException("Regex can't be empty");
            } else {
                try {
                    regex.setValue(regex.getValue());
                } catch (PatternSyntaxException e) {
                    throw new JsonParseException("Regex " + regex.getValue() + " is not a valid Java regex");
                }
            }
        }
        List<Field> fields = new ArrayList<>();
        Utils.addDeclaredAndInheritedFields(Class.forName(type), fields);
        for (Field f : fields) {
            if (f.getAnnotation(Required.class) != null) {
                try {
                    f.setAccessible(true);
                    if (f.get(pojo) == null) {
                        throw new JsonParseException("Missing required field in condition: " + f.getName());
                    }
                } catch (IllegalArgumentException | IllegalAccessException ex) {
                }
            }
        }
        return pojo;
    } catch (ClassNotFoundException cnfe) {
        throw new JsonParseException("Unknown condition type: " + type, cnfe);
    }
}

From source file:com.strategicgains.restexpress.serialization.json.GsonDateSerializer.java

License:Apache License

@Override
public Date deserialize(JsonElement json, Type typeOf, JsonDeserializationContext context)
        throws JsonParseException {
    try {//from   w  ww. j  ava  2 s.co m
        return adapter.parse(json.getAsJsonPrimitive().getAsString());
    } catch (ParseException e) {
        throw new JsonParseException(e);
    }
}