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:com.kolich.common.entities.gson.KolichDefaultDateTypeAdapter.java

License:Open Source License

@Override
public Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
        throws JsonParseException {
    if (!(json instanceof JsonPrimitive)) {
        throw new JsonParseException("The date should be a String type.");
    }/*  ww  w .jav a 2  s  .c o  m*/
    final String jsonDate = json.getAsString();
    try {
        synchronized (format_) {
            return format_.parse(jsonDate);
        }
    } catch (ParseException e) {
        throw new JsonSyntaxException("Failed to de-serialize " + "date. Invalid format? = " + jsonDate, e);
    }
}

From source file:com.mycompany.testeproject.serialization.GsonDateSerialization.java

@Override
public Date deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) {
    Date date;/*from  w w  w .  jav  a 2s  .  com*/
    try {
        date = DateFormatUtils.ISO_8601_EXTENDED_DATETIME_FORMAT.parse(jsonElement.getAsString());
    } catch (ParseException e) {
        throw new JsonSyntaxException(jsonElement.getAsString(), e);
    }
    return date;
}

From source file:com.orange.client.githubimport.adapter.ISODateAdapter.java

License:Apache License

private Date deserializeToDate(JsonElement json) {
    try {/*from  ww w .j a  va2s  .  c om*/
        return iso8601Format.parse(json.getAsString());
    } catch (ParseException e) {
        throw new JsonSyntaxException(json.getAsString(), e);
    }
}

From source file:com.vmware.xenon.common.serialization.UtcDateTypeAdapter.java

License:Open Source License

@Override
public synchronized Date deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) {
    try {//from  w  w  w . j  a  v  a  2 s.  c  o  m
        ZonedDateTime zdt = ZonedDateTime.parse(jsonElement.getAsString(), DATE_FORMAT);
        return Date.from(zdt.toInstant());
    } catch (DateTimeParseException e) {
        throw new JsonSyntaxException(jsonElement.getAsString(), e);
    }
}

From source file:eu.vranckaert.worktime.web.json.model.JsonResult.java

License:Apache License

public <Y extends JsonEntity> Y getSingleResult(Class<Y> entityClass) {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Date.class, new DateTimeDeserializer());
    Gson gson = builder.create();//  w w  w  . j  ava  2  s  .  co m

    JSONObject j;
    Y object = null;

    try {
        j = new JSONObject(json);
        object = gson.fromJson(j.toString(), entityClass);
    } catch (JSONException e) {
        String msg = "Could not parse the json data!";
        Log.e(LOG_TAG, msg, e);
        throw new JsonSyntaxException(msg, e);
    }
    return object;
}

From source file:eu.vranckaert.worktime.web.json.model.JsonResult.java

License:Apache License

public <Y extends JsonEntity> List<Y> getResultList(Class<Y> entityClass) {
    //Type listType = new TypeToken<List<Y>>() {}.getType();
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();//from www.  j av  a2s  .co m

    JSONArray j;
    List<Y> resultList = new ArrayList<Y>();

    try {
        j = new JSONArray(json);
        for (int i = 0; i < j.length(); i++) {
            Y object = gson.fromJson(j.getJSONObject(i).toString(), entityClass);
            resultList.add(object);
        }
        //list = gson.fromJson(j.toString(), listType);
    } catch (JSONException e) {
        String msg = "Could not parse the json data!";
        Log.e(LOG_TAG, msg, e);
        throw new JsonSyntaxException(msg, e);
    }
    return resultList;
}

From source file:io.vertigo.vega.engines.webservice.json.GoogleJsonEngine.java

License:Apache License

/** {@inheritDoc} */
@Override/*from ww w. j  a v a 2  s .c  om*/
public UiContext uiContextFromJson(final String json, final Map<String, Type> paramTypes) {
    final UiContext result = new UiContext();
    try {
        final JsonElement jsonElement = new JsonParser().parse(json);
        final JsonObject jsonObject = jsonElement.getAsJsonObject();
        for (final Entry<String, Type> entry : paramTypes.entrySet()) {
            final String key = entry.getKey();
            final Type paramType = entry.getValue();
            final JsonElement jsonSubElement = jsonObject.get(key);

            final Serializable value;
            if (WebServiceTypeUtil.isAssignableFrom(DtObject.class, paramType)) {
                final Type typeOfDest = new KnownParameterizedType(UiObject.class, paramType);
                value = gson.fromJson(jsonSubElement, typeOfDest);
            } else if (WebServiceTypeUtil.isAssignableFrom(DtListDelta.class, paramType)) {
                final Class<DtObject> dtoClass = (Class<DtObject>) ((ParameterizedType) paramType)
                        .getActualTypeArguments()[0]; //we known that DtListDelta has one parameterized type
                final Type typeOfDest = new KnownParameterizedType(UiListDelta.class, dtoClass);
                value = gson.fromJson(jsonSubElement, typeOfDest);
            } else if (WebServiceTypeUtil.isAssignableFrom(DtList.class, paramType)) {
                final Class<DtObject> dtoClass = (Class<DtObject>) ((ParameterizedType) paramType)
                        .getActualTypeArguments()[0]; //we known that DtList has one parameterized type
                final Type typeOfDest = new KnownParameterizedType(UiListModifiable.class, dtoClass);
                value = gson.fromJson(jsonSubElement, typeOfDest);
            } else {
                value = gson.fromJson(jsonSubElement, paramType);
            }
            result.put(key, value);
        }
        return result;
    } catch (final IllegalStateException e) {
        throw new JsonSyntaxException("JsonObject expected", e);
    }
}

From source file:io.vertigo.vega.plugins.rest.handler.JsonConverterRestHandlerPlugin.java

License:Apache License

/** {@inheritDoc}  */
@Override//from  w w w.j a  va2 s .  com
public Object handle(final Request request, final Response response, final RouteContext routeContext,
        final HandlerChain chain) throws VSecurityException, SessionException {
    //we can't read body at first : because if it's a multipart request call body() disabled getParts() access.
    UiContext innerBodyParsed = null;
    for (final EndPointParam endPointParam : routeContext.getEndPointDefinition().getEndPointParams()) {
        try {
            final Object value;
            if (VFileUtil.isVFileParam(endPointParam)) {
                value = VFileUtil.readVFileParam(request, endPointParam);
            } else {
                switch (endPointParam.getParamType()) {
                case Body:
                    value = readValue(request.body(), endPointParam);
                    break;
                case InnerBody:
                    if (innerBodyParsed == null) {
                        //we read all InnerBody when we get the first one
                        innerBodyParsed = readInnerBodyValue(request.body(),
                                routeContext.getEndPointDefinition().getEndPointParams());
                    }
                    value = innerBodyParsed.get(endPointParam.getName());
                    break;
                case Path:
                    value = readPrimitiveValue(request.params(endPointParam.getName()),
                            endPointParam.getType());
                    break;
                case Query:
                    value = readQueryValue(request.queryMap(), endPointParam);
                    break;
                case Header:
                    value = readPrimitiveValue(request.headers(endPointParam.getName()),
                            endPointParam.getType());
                    break;
                case Implicit:
                    value = readImplicitValue(endPointParam, request, response, routeContext);
                    break;
                default:
                    throw new IllegalArgumentException("RestParamType : " + endPointParam.getFullName());
                }
            }
            Assertion.checkNotNull(value, "RestParam not found : {0}", endPointParam);
            routeContext.setParamValue(endPointParam, value);
        } catch (final JsonSyntaxException e) {
            throw new JsonSyntaxException("Error parsing param " + endPointParam.getFullName() + " on service "
                    + routeContext.getEndPointDefinition().getVerb() + " "
                    + routeContext.getEndPointDefinition().getPath(), e);
        }
    }

    final Object result = chain.handle(request, response, routeContext);
    return convertResult(result, request, response, routeContext);
}

From source file:io.vertigo.vega.plugins.webservice.handler.JsonConverterWebServiceHandlerPlugin.java

License:Apache License

private void readParameterValue(final Request request, final WebServiceCallContext routeContext,
        final WebServiceParam webServiceParam) {
    try {//from   w  w  w . ja  v a  2  s .com
        boolean found = false;
        JsonReader jsonReaderToApply = null;
        JsonConverter jsonConverterToApply = null;
        for (final JsonReader jsonReader : jsonReaders.get(webServiceParam.getParamType())) {
            jsonReaderToApply = jsonReader;

            for (final JsonConverter jsonConverter : jsonConverters.get(jsonReader.getSupportedOutput())) {

                if (jsonConverter.canHandle(webServiceParam.getType())) {
                    jsonConverterToApply = jsonConverter;
                    found = true;
                    break;
                }
            }
            if (found) {
                break;
            }
        }
        //-----
        Assertion.checkNotNull(jsonReaderToApply,
                "Can't parse param {0} of service {1} {2} no compatible JsonReader found for {3}",
                webServiceParam.getFullName(), routeContext.getWebServiceDefinition().getVerb(),
                routeContext.getWebServiceDefinition().getPath(), webServiceParam.getParamType());
        Assertion.checkNotNull(jsonConverterToApply,
                "Can't parse param {0} of service {1} {2} no compatible JsonConverter found for {3} {4}",
                webServiceParam.getFullName(), routeContext.getWebServiceDefinition().getVerb(),
                routeContext.getWebServiceDefinition().getPath(), webServiceParam.getParamType(),
                webServiceParam.getType());
        //-----
        final Object converterSource = jsonReaderToApply.extractData(request, webServiceParam, routeContext);
        if (converterSource != null) { //On ne convertit pas les null
            jsonConverterToApply.populateWebServiceCallContext(converterSource, webServiceParam, routeContext);
        } else if (webServiceParam.isOptional()) {
            routeContext.setParamValue(webServiceParam, null /*converterSource*/);
        }
        Assertion.checkNotNull(routeContext.getParamValue(webServiceParam), "RestParam not found : {0}",
                webServiceParam);
    } catch (final JsonSyntaxException e) {
        throw new JsonSyntaxException("Error parsing param " + webServiceParam.getFullName() + " on service "
                + routeContext.getWebServiceDefinition().getVerb() + " "
                + routeContext.getWebServiceDefinition().getPath(), e);
    }
}

From source file:io.vertigo.vega.rest.engine.GoogleJsonEngine.java

License:Apache License

/** {@inheritDoc} */
@Override/*  ww w .j  av  a  2s.co m*/
public UiContext uiContextFromJson(final String json, final Map<String, Type> paramTypes) {
    final UiContext result = new UiContext();
    try {
        final JsonElement jsonElement = new JsonParser().parse(json);
        final JsonObject jsonObject = jsonElement.getAsJsonObject();
        for (final Entry<String, Type> entry : paramTypes.entrySet()) {
            final String key = entry.getKey();
            final Type paramType = entry.getValue();
            final JsonElement jsonSubElement = jsonObject.get(key);

            final Serializable value;
            if (EndPointTypeUtil.isAssignableFrom(DtObject.class, paramType)) {
                final Type typeOfDest = createParameterizedType(UiObject.class, paramType);
                value = gson.fromJson(jsonSubElement, typeOfDest);
            } else if (EndPointTypeUtil.isAssignableFrom(DtListDelta.class, paramType)) {
                final Class<DtObject> dtoClass = (Class<DtObject>) ((ParameterizedType) paramType)
                        .getActualTypeArguments()[0]; //we known that DtListDelta has one parameterized type
                final Type typeOfDest = createParameterizedType(UiListDelta.class, dtoClass);
                value = gson.fromJson(jsonSubElement, typeOfDest);
            } else if (EndPointTypeUtil.isAssignableFrom(DtList.class, paramType)) {
                final Class<DtObject> dtoClass = (Class<DtObject>) ((ParameterizedType) paramType)
                        .getActualTypeArguments()[0]; //we known that DtListDelta has one parameterized type
                final Type typeOfDest = createParameterizedType(UiList.class, dtoClass);
                value = gson.fromJson(jsonSubElement, typeOfDest);
            } else {
                value = (Serializable) gson.fromJson(jsonSubElement, paramType);
            }
            result.put(key, value);
        }
        return result;
    } catch (final IllegalStateException e) {
        throw new JsonSyntaxException("JsonObject expected", e);
    }
}