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:org.eclipse.packagedrone.utils.gson.InstantTypeAdapter.java

License:Open Source License

@Override
public Instant deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
        throws JsonParseException {
    if (!(json instanceof JsonPrimitive)) {
        throw new JsonParseException("Timestamps should be encoded as JSON strings");
    }//  www. j  a  va2s . c  om

    return Instant.from(this.formatter.parse(json.getAsString()));
}

From source file:org.eclipse.scada.base.json.VariantJsonDeserializer.java

License:Open Source License

@Override
public Variant deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonNull()) {
        return null;
    }//from  w  w  w  .j  a  v  a  2 s .co  m

    if (json instanceof JsonPrimitive) {
        return decodeFromPrimitive(json);
    }

    if (json instanceof JsonObject) {
        final JsonObject jsonObj = (JsonObject) json;
        final JsonElement type = jsonObj.get(VariantJson.FIELD_TYPE);
        final JsonElement value = jsonObj.get(VariantJson.FIELD_VALUE);

        if (type == null || type.isJsonNull()) {
            if (value == null) {
                throw new JsonParseException(String.format("Variant encoded as object must have a field '%s'",
                        VariantJson.FIELD_VALUE));
            }
            return Variant.valueOf(value.getAsString());
        }

        if (!type.isJsonPrimitive()) {
            throw new JsonParseException(
                    String.format("Variant field '%s' must be a string containing the variant type (%s)",
                            VariantJson.FIELD_TYPE, StringHelper.join(VariantType.values(), ", ")));
        }

        final String typeStr = type.getAsString();

        if (typeStr.equals("NULL")) {
            return Variant.NULL;
        }

        if (value == null || value.isJsonNull()) {
            throw new JsonParseException(String.format(
                    "The type '%s' does not support a null value. Use variant type NULL instead.", typeStr));
        }

        if (value.isJsonObject() || value.isJsonArray()) {
            throw new JsonParseException(
                    "The variant value must be a JSON primitive matching the type. Arrays and objects are not supported");
        }

        switch (type.getAsString()) {
        case "BOOLEAN":
            return Variant.valueOf(value.getAsBoolean());
        case "STRING":
            return Variant.valueOf(value.getAsString());
        case "DOUBLE":
            return Variant.valueOf(value.getAsDouble());
        case "INT32":
            return Variant.valueOf(value.getAsInt());
        case "INT64":
            return Variant.valueOf(value.getAsLong());
        default:
            throw new JsonParseException(String.format("Type '%s' is unknown (known types: %s)",
                    StringHelper.join(VariantType.values(), ", ")));
        }
    }

    throw new JsonParseException("Unknown serialization of Variant type");
}

From source file:org.faul.jql.utils.RemoteObjectHandler.java

License:Open Source License

public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    String str = null;/*from w w w  .j  a v a2s. c o m*/
    if (json instanceof JsonPrimitive) {
        str = json.getAsJsonPrimitive().getAsString();

        JsonPrimitive p = (JsonPrimitive) json;
        return getPrimitive(p);
    }
    if (json instanceof JsonArray) {
        JsonArray array = (JsonArray) json;
        return getList(array);
    }

    if (json instanceof JsonObject) {
        // Now we will assume its a map
        JsonObject jso = json.getAsJsonObject();
        Map map = new HashMap();
        mapHandler(map, jso);
        return map;
    }
    throw new JsonParseException("Can't handle: " + json);
}

From source file:org.fracturedatlas.athena.util.date.DateTypeConverter.java

License:Open Source License

@Override
public Date deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    try {//  w  w  w.  j  av a 2s .c  o  m
        return DateUtil.parseDate(json.getAsString());
    } catch (ParseException e) {
        e.printStackTrace();
        throw new JsonParseException(e);
    }
}

From source file:org.immutables.gson.adapter.ExpectedSubtypesAdapter.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//from ww  w .j  av a  2  s.  com
public T read(JsonReader in) throws IOException {
    List<Exception> exceptions = new ArrayList<>(subtypes.length);
    ReaderSupplier readerSupplier = readForSupplier(in);
    for (TypeAdapter<?> typeAdapter : adapters) {
        try {
            return (T) typeAdapter.read(readerSupplier.create());
        } catch (Exception ex) {
            exceptions.add(ex);
        }
    }
    JsonParseException failure = new JsonParseException(
            String.format("Cannot parse %s with following subtypes: %s", type, Arrays.toString(subtypes)));
    for (Exception exception : exceptions) {
        failure.addSuppressed(exception);
    }
    throw failure;
}

From source file:org.immutables.mongo.fixture.holder.HolderJsonSerializer.java

License:Apache License

@Override
public Holder deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject root = (JsonObject) json;

    ImmutableHolder.Builder builder = ImmutableHolder.builder();

    if (root.has("id")) {
        builder.id(root.get("id").getAsString());
    }/*from  w w  w. j  a va  2  s  . com*/

    JsonElement value = root.get(VALUE_PROPERTY);
    if (value == null) {
        throw new JsonParseException(String.format("%s not found for %s in JSON", VALUE_PROPERTY, type));
    }

    if (value.isJsonObject()) {
        final String valueTypeName = value.getAsJsonObject().get(Holder.TYPE_PROPERTY).getAsString();
        try {
            Class<?> valueType = Class.forName(valueTypeName);
            builder.value(context.deserialize(value, valueType));
        } catch (ClassNotFoundException e) {
            throw new JsonParseException(
                    String.format("Couldn't construct value class %s for %s", valueTypeName, type), e);
        }
    } else if (value.isJsonPrimitive()) {
        final JsonPrimitive primitive = value.getAsJsonPrimitive();
        if (primitive.isString()) {
            builder.value(primitive.getAsString());
        } else if (primitive.isNumber()) {
            builder.value(primitive.getAsInt());
        } else if (primitive.isBoolean()) {
            builder.value(primitive.getAsBoolean());
        }
    } else {
        throw new JsonParseException(String.format("Couldn't deserialize %s : %s. Not a primitive or object",
                VALUE_PROPERTY, value));
    }

    return builder.build();

}

From source file:org.jeeventstore.serialization.gson.EventListTypeConverter.java

License:Open Source License

@Override
public EventList deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {

    JsonObject obj = json.getAsJsonObject();
    Integer version = obj.getAsJsonPrimitive("version").getAsInt();
    if (version != 1)
        throw new JsonParseException("Unable to parse event of version " + version);

    Iterator<JsonElement> eit = obj.getAsJsonArray("events").iterator();
    List<Serializable> eventlist = new ArrayList<>();
    while (eit.hasNext()) {
        String clazz = null;/*from w w w.  ja v  a  2s  . c  o m*/
        try {
            JsonObject elem = eit.next().getAsJsonObject();
            clazz = elem.getAsJsonPrimitive("type").getAsString();
            Class<? extends Serializable> eventClass = (Class<? extends Serializable>) Class.forName(clazz);
            Serializable s = context.deserialize(elem.get("body"), eventClass);
            eventlist.add(s);
        } catch (ClassNotFoundException e) {
            throw new JsonParseException("Cannot deserialize events of class " + clazz, e);
        }
    }
    return new EventList(eventlist);
}

From source file:org.jetbrains.io.JsonReaderEx.java

License:Apache License

/**
 * Throws a new IO exception with the given message and a context snippet
 * with this reader's content./* w  w w. j a v  a 2 s. co  m*/
 */
private JsonParseException createParseError(String message) {
    throw new JsonParseException(message + " at line " + getLineNumber() + " column " + getColumnNumber());
}

From source file:org.jfvclient.deserialisers.DpidDeserialiser.java

License:Apache License

@Override
public Dpid deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    String s = json.getAsString();
    Dpid d = new Dpid(s);
    if (!d.isValid()) {
        throw new JsonParseException("Encountered an invalid DPID: " + s);
    }/* w w w .ja  v  a  2 s  . c om*/
    return d;
}

From source file:org.jfvclient.serialisers.DpidSerialiser.java

License:Apache License

@Override
public JsonElement serialize(Dpid src, Type typeOfSrc, JsonSerializationContext context) {
    if (!src.isValid()) {
        throw new JsonParseException("invalid DPID" + src.getDpid());
    }//from   w  w  w.  j  a  v  a  2 s  .  c  o m

    return new JsonPrimitive(src.getDpid());
}