Example usage for com.google.gson JsonSyntaxException JsonSyntaxException

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

Introduction

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

Prototype

public JsonSyntaxException(String msg, Throwable cause) 

Source Link

Usage

From source file:net.feed_the_beast.launcher.json.DateAdapter.java

License:Apache License

@Override
public Date deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    if (!(json instanceof JsonPrimitive)) {
        throw new JsonParseException("Date was not string: " + json);
    }// ww  w. j  av  a  2  s.com
    if (type != Date.class) {
        throw new IllegalArgumentException(getClass() + " cannot deserialize to " + type);
    }
    String value = json.getAsString();
    synchronized (enUsFormat) {
        try {
            return enUsFormat.parse(value);
        } catch (ParseException e) {
            try {
                return iso8601Format.parse(value);
            } catch (ParseException e2) {
                try {
                    String tmp = value.replace("Z", "+00:00");
                    return iso8601Format.parse(tmp.substring(0, 22) + tmp.substring(23));
                } catch (ParseException e3) {
                    throw new JsonSyntaxException("Invalid date: " + value, e3);
                }
            }
        }
    }
}

From source file:net.minecraftforge.gradle.util.json.DateAdapter.java

License:Open Source License

@Override
public Date deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    if (!(json instanceof JsonPrimitive)) {
        throw new JsonParseException("Date was not string: " + json);
    }//from w  w w  . j  a v  a2 s .  co  m
    if (type != Date.class) {
        throw new IllegalArgumentException(getClass() + " cannot deserialize to " + type);
    }
    String value = json.getAsString();
    synchronized (enUsFormat) {
        try {
            return enUsFormat.parse(value);
        } catch (ParseException e) {
            try {
                return iso8601Format.parse(value);
            } catch (ParseException e2) {
                try {
                    String tmp = value.replace("Z", "+00:00");
                    if (tmp.length() < 22)
                        return new Date();
                    else
                        return iso8601Format.parse(tmp.substring(0, 22) + tmp.substring(23));
                } catch (ParseException e3) {
                    throw new JsonSyntaxException("Invalid date: " + value, e3);
                }
            }
        }
    }
}

From source file:net.technicpack.launchercore.util.DateTypeAdapter.java

License:Open Source License

private Date deserializeToDate(JsonElement json) {
    synchronized (this.enUsFormat) {
        try {/*  w  w w  . ja va2 s  .  c  om*/
            return this.enUsFormat.parse(json.getAsString());
        } catch (ParseException localParseException) {
            try {
                return this.iso8601Format.parse(json.getAsString());
            } catch (ParseException localParseException1) {
                try {
                    String cleaned = json.getAsString().replace("Z", "+00:00");
                    cleaned = cleaned.substring(0, 22) + cleaned.substring(23);
                    return this.iso8601Format.parse(cleaned);
                } catch (Exception e) {
                    throw new JsonSyntaxException("Invalid date: " + json.getAsString(), e);
                }
            }
        }
    }
}

From source file:org.aku.sm.smclient.service.DateTypeAdapter.java

License:Apache License

private synchronized Date deserializeToDate(String json) {
    try {/*from   w ww .j a  va  2 s . com*/
        return iso8601Format.parse(json);
    } catch (ParseException e) {
        throw new JsonSyntaxException(json, e);
    }
}

From source file:org.projectforge.rest.converter.UTCDateTimeTypeAdapter.java

License:Open Source License

@Override
public synchronized Date deserialize(final JsonElement jsonElement, final Type type,
        final JsonDeserializationContext jsonDeserializationContext) {
    try {/*from w ww .  ja  v  a 2s .co m*/
        synchronized (dateTimeFormat) {
            final String element = jsonElement.getAsString();
            if (element == null) {
                return null;
            }
            if (StringUtils.isNumeric(element) == true) {
                final Date date = new Date(Long.parseLong(element));
                return date;
            }
            return dateTimeFormatter.parse(element);
        }
    } catch (final ParseException e) {
        throw new JsonSyntaxException(jsonElement.getAsString(), e);
    }
}

From source file:org.projectforge.rest.converter.UTCDateTypeAdapter.java

License:Open Source License

@Override
public synchronized Date deserialize(final JsonElement jsonElement, final Type type,
        final JsonDeserializationContext jsonDeserializationContext) {
    try {/* w w  w.ja va 2 s  . c  om*/
        synchronized (dateFormatter) {
            final String element = jsonElement.getAsString();
            if (element == null) {
                return null;
            }
            if (StringUtils.isNumeric(element) == true) {
                final Date date = new Date(Long.parseLong(element));
                return date;
            }
            final java.util.Date date = dateFormatter.parse(element);
            return new Date(date.getTime());
        }
    } catch (final ParseException e) {
        throw new JsonSyntaxException(jsonElement.getAsString(), e);
    }
}

From source file:org.rapla.rest.gwtjsonrpc.server.JsonServlet.java

License:Apache License

public void mapRequestToCall(final ActiveCall call, final HttpServletRequest req, String body) {
    final Gson gs = createGsonBuilder().create();
    String methodName = (String) req.getAttribute(JSON_METHOD);
    if (methodName != null) {
        call.versionName = "jsonrpc";
        call.versionValue = new JsonPrimitive("2.0");
    } else {//from  ww w .  j  a  v  a 2 s  .  c o  m
        methodName = req.getParameter("method");
        call.versionName = "version";
        call.versionValue = new JsonPrimitive("1.1");
    }
    call.method = lookupMethod(methodName);
    if (call.method == null) {
        throw new NoSuchRemoteMethodException(getInterfaceClass() + "." + methodName);
    }
    final Type[] paramTypes = call.method.getParamTypes();
    String[] paramNames = call.method.getParamNames();

    final Object[] r = new Object[paramTypes.length];
    for (int i = 0; i < r.length; i++) {
        Type type = paramTypes[i];
        String name = paramNames[i];
        if (name == null && !call.versionName.equals("jsonrpc")) {
            name = "param" + i;
        }
        {
            // First search in the request attributes
            Object attribute = req.getAttribute(name);
            Object paramValue;
            if (attribute != null) {
                paramValue = attribute;
                Class attributeClass = attribute.getClass();
                // we try to convert string and jsonelements to the parameter type (if the parameter type is not string or jsonelement)
                if (attributeClass.equals(String.class) && !type.equals(String.class)) {
                    JsonParser parser = new JsonParser();
                    JsonElement parsed = parser.parse((String) attribute);
                    paramValue = gs.fromJson(parsed, type);

                } else if (JsonElement.class.isAssignableFrom(attributeClass)
                        && !type.equals(JsonElement.class)) {
                    JsonElement parsed = (JsonElement) attribute;
                    paramValue = gs.fromJson(parsed, type);
                } else {
                    paramValue = attribute;
                }
            }
            // then in request parameters
            else {
                String v = null;
                v = req.getParameter(name);
                // if not found in request use body
                if (v == null && body != null && !body.isEmpty()) {
                    v = body;
                }
                if (v == null) {
                    paramValue = null;
                } else if (type == String.class) {
                    paramValue = v;
                } else if (type == Date.class) {
                    // special case for handling date parameters with the
                    // ':' char i it
                    try {
                        paramValue = SerializableDateTimeFormat.INSTANCE.parseTimestamp(v);
                    } catch (ParseDateException e) {
                        throw new JsonSyntaxException(v, e);
                    }
                } else if (type instanceof Class<?> && ((Class<?>) type).isPrimitive()) {
                    // Primitive type, use the JSON representation of that
                    // type.
                    //
                    paramValue = gs.fromJson(v, type);
                } else {
                    // Assume it is like a java.sql.Timestamp or something
                    // and treat
                    // the value as JSON string.
                    //
                    JsonParser parser = new JsonParser();
                    JsonElement parsed = parser.parse(v);
                    paramValue = gs.fromJson(parsed, type);
                }
            }
            r[i] = paramValue;
        }
    }
    call.params = r;
}

From source file:project.celine.infinitescroll.service.MyDateTypeAdapter.java

License:Apache License

private synchronized Date deserializeToDate(String json) {
    try {//from   ww  w  . j  a  va 2 s. co m
        return localFormat.parse(json);
    } catch (ParseException ignored) {
    }
    try {
        return enUsFormat.parse(json);
    } catch (ParseException ignored) {
    }
    try {
        return iso8601Format.parse(json);
    } catch (ParseException e) {

    }
    try {
        return nsdDateFormat.parse(json);
    } catch (ParseException e) {
        throw new JsonSyntaxException(json, e);
    }

}