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.jadarstudios.developercapes.standalone.DevCapesStandalone.java

License:Open Source License

@EventHandler
public void preLoad(FMLPreInitializationEvent event) {
    this.logger = event.getModLog();
    this.modId = event.getModMetadata().modId;

    InputStream is = getClass().getResourceAsStream("/capeInfo.json");
    InputStreamReader reader = new InputStreamReader(is);
    try {/*from   ww  w  .ja  va2 s  .c om*/
        Gson gson = new Gson();

        HashMap map = gson.fromJson(reader, HashMap.class);

        Object urlStringObject = map.get("capeConfigUrl");
        if (urlStringObject != null && (urlStringObject instanceof String)) {
            capeConfigUrl = Strings.nullToEmpty((String) urlStringObject);
        } else {
            throw new JsonParseException("Could not find key 'capeConfigUrl'");
        }
    } catch (JsonParseException e) {
        logger.error("Failed to parse capeInfo.json. This means you could have a corrupt mod jar.");
        e.printStackTrace();
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }

}

From source file:com.javacreed.examples.gson.part3.AuthorDeserializer.java

License:Apache License

@Override
public Author deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
        throws JsonParseException {

    // Only the ID is available
    if (json.isJsonPrimitive()) {
        final JsonPrimitive primitive = json.getAsJsonPrimitive();
        return getOrCreate(primitive.getAsInt());
    }//from  w w w .j  av  a2s. c om

    // The whole object is available
    if (json.isJsonObject()) {
        final JsonObject jsonObject = json.getAsJsonObject();

        final Author author = getOrCreate(jsonObject.get("id").getAsInt());
        author.setName(jsonObject.get("name").getAsString());
        return author;
    }

    throw new JsonParseException("Unexpected JSON type: " + json.getClass().getSimpleName());
}

From source file:com.jboss.examples.SQLDateTypeAdapter.java

License:Open Source License

public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!(json instanceof JsonPrimitive)) {
        throw new JsonParseException("The date should be a string value");
    }//from  w ww  .  ja  v a 2  s.  c  o m

    try {
        return format.parse(json.getAsString());
    } catch (ParseException e) {
        throw new JsonParseException(e);
    }

}

From source file:com.keydap.sparrow.DateTimeSerializer.java

License:Apache License

@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    String d = json.getAsString();
    try {/*w ww .j  a  v a 2  s  .com*/
        return df.parse(d);
    } catch (Exception e) {
        throw new JsonParseException(e);
    }
}

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.");
    }/*w  w  w  .  j ava 2 s . c  om*/
    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.kotcrab.vis.editor.serializer.json.ClassJsonSerializer.java

License:Apache License

@Override
public Class<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    try {/*from  w w w.  jav  a 2s  . c  om*/
        return getFullClassName(json.getAsString());
    } catch (ClassNotFoundException e) {
        throw new JsonParseException(e);
    }
}

From source file:com.kotcrab.vis.editor.serializer.json.RuntimeTypeAdapterFactory.java

License:Apache License

public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
    if (baseType.isAssignableFrom(type.getRawType()) == false) {
        return null;
    }/*from  w ww .  j  av  a 2 s .c  o m*/

    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);
            if (labelJsonElement == null) {
                throw new JsonParseException("cannot deserialize " + baseType
                        + " because it does not define a field named " + typeFieldName);
            }
            String label = 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)) {
                throw new JsonParseException("cannot serialize " + srcType.getName()
                        + " because it already defines a field named " + typeFieldName);
            }
            JsonObject clone = new JsonObject();
            clone.add(typeFieldName, new JsonPrimitive(label));
            for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
                clone.add(e.getKey(), e.getValue());
            }
            Streams.write(clone, out);
        }
    };
}

From source file:com.kurento.kmf.content.jsonrpc.GsonUtils.java

License:Open Source License

@Override
public JsonRpcResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    if (!(json instanceof JsonObject)) {
        throw new JsonParseException("JonObject expected, found " + json.getClass().getSimpleName());
    }// www  .  j  ava  2 s.  c om

    JsonObject jObject = (JsonObject) json;

    if (!jObject.has("jsonrpc")) {
        throw new JsonParseException("Invalid JsonRpc response lacking version 'jsonrpc' field");
    }

    if (!jObject.get("jsonrpc").getAsString().equals(JsonRpcConstants.JSON_RPC_VERSION)) {
        throw new JsonParseException("Invalid JsonRpc version");
    }

    if (!jObject.has("id")) {
        throw new JsonParseException("Invalid JsonRpc response lacking 'id' field");
    }

    if (jObject.has("result")) {
        return new JsonRpcResponse((JsonRpcResponseResult) context
                .deserialize(jObject.get("result").getAsJsonObject(), JsonRpcResponseResult.class),
                jObject.get("id").getAsInt());
    } else if (jObject.has("error")) {
        return new JsonRpcResponse((JsonRpcResponseError) context
                .deserialize(jObject.get("error").getAsJsonObject(), JsonRpcResponseError.class),
                jObject.get("id").getAsInt());
    } else {
        throw new JsonParseException("Invalid JsonRpc response lacking 'result' and 'error' fields");
    }

}

From source file:com.kurento.kmf.content.jsonrpc.GsonUtils.java

License:Open Source License

@Override
public JsonRpcRequest deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    if (!(json instanceof JsonObject)) {
        throw new JsonParseException(
                "Invalid JsonRpc request showning JsonElement type " + json.getClass().getSimpleName());
    }//from   w w w. ja v a 2  s  .  c  o  m

    JsonObject jObject = (JsonObject) json;

    if (!jObject.has("jsonrpc")) {
        throw new JsonParseException("Invalid JsonRpc request lacking version 'jsonrpc' field");
    }

    if (!jObject.get("jsonrpc").getAsString().equals(JsonRpcConstants.JSON_RPC_VERSION)) {
        throw new JsonParseException("Invalid JsonRpc version");
    }

    if (!jObject.has("method")) {
        throw new JsonParseException("Invalid JsonRpc request lacking 'method' field");
    }

    if (!jObject.has("params")) {
        throw new JsonParseException("Invalid JsonRpc request lacking 'params' field");
    }

    if (!jObject.has("id")) {
        throw new JsonParseException("Invalid JsonRpc request lacking 'id' field");
    }

    return new JsonRpcRequest(
            jObject.get("method").getAsString(), (JsonRpcRequestParams) context
                    .deserialize(jObject.get("params").getAsJsonObject(), JsonRpcRequestParams.class),
            jObject.get("id").getAsInt());

}

From source file:com.kurento.kmf.jsonrpcconnector.JsonUtils.java

License:Open Source License

@Override
public Response<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    if (!(json instanceof JsonObject)) {
        throw new JsonParseException("JonObject expected, found " + json.getClass().getSimpleName());
    }/*from   w ww.ja  v a 2 s  .com*/

    JsonObject jObject = (JsonObject) json;

    if (!jObject.has(JSON_RPC_PROPERTY)) {
        throw new JsonParseException(
                "Invalid JsonRpc response lacking version '" + JSON_RPC_PROPERTY + "' field");
    }

    if (!jObject.get(JSON_RPC_PROPERTY).getAsString().equals(JsonRpcConstants.JSON_RPC_VERSION)) {
        throw new JsonParseException("Invalid JsonRpc version");
    }

    Integer id;
    try {
        id = jObject.get(ID_PROPERTY).getAsInt();
    } catch (Exception e) {
        throw new JsonParseException("Invalid JsonRpc response. It lacks a valid '" + ID_PROPERTY + "' field");
    }

    if (jObject.has(RESULT_PROPERTY)) {

        ParameterizedType parameterizedType = (ParameterizedType) typeOfT;

        return new Response<Object>(id, context.deserialize(jObject.get(RESULT_PROPERTY),
                parameterizedType.getActualTypeArguments()[0]));

    } else if (jObject.has(ERROR_PROPERTY)) {

        return new Response<Object>(id,
                (ResponseError) context.deserialize(jObject.get(ERROR_PROPERTY), ResponseError.class));

    } else {
        throw new JsonParseException("Invalid JsonRpc response lacking '" + RESULT_PROPERTY + "' and '"
                + ERROR_PROPERTY + "' fields");
    }

}