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:me.jamiemansfield.mc.text.serialiser.JsonTextSerialiser.java

License:MIT License

@Override
public Text deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("Terribly sorry, but I will not be able to deserialise " + json);
    }/*  w  w w .j  a v a2  s.c  om*/
    final JsonObject obj = json.getAsJsonObject();
    final Text.Builder text;

    if (obj.has("text")) {
        text = Text.builder(obj.get("text").getAsString());
    } else if (obj.has("translate")) {
        if (obj.has("with") && obj.get("with").isJsonArray()) {
            final JsonArray with = obj.getAsJsonArray("with");
            final Text[] arguments = new Text[with.size()];

            for (int i = 0; i < with.size(); i++) {
                final JsonElement element = with.get(i);
                arguments[i] = this.deserialize(element, element.getClass(), context);
            }

            text = Text.translatableBuilder(obj.get("translate").getAsString(), arguments);
        } else {
            text = Text.translatableBuilder(obj.get("translate").getAsString());
        }
    } else {
        throw new JsonParseException("Terribly sorry, but I will not be able to deserialise " + json);
    }

    DECORATION.getEntries().stream().filter(decoration -> obj.has(decoration.getInternalName())).forEach(
            decoration -> text.apply(decoration, obj.get(decoration.getInternalName()).getAsBoolean()));

    if (obj.has("color")) {
        final Optional<TextColour> colour = COLOUR.getEntries().stream()
                .filter(clr -> clr.getInternalName().equals(obj.get("color").getAsString())).findAny();
        if (!colour.isPresent()) {
            throw new JsonParseException("Terribly sorry, but I will not be able to deserialise " + json);
        }
        text.apply(colour.get());
    }

    if (obj.has("insertion")) {
        text.insertion(obj.get("insertion").getAsString());
    }

    if (obj.has("clickEvent")) {
        final Optional<ClickEvent.Action> action = CLICK_EVENT_ACTION.getEntries().stream().filter(a -> obj
                .get("clickEvent").getAsJsonObject().get("action").getAsString().equals(a.getInternalName()))
                .findAny();
        if (!action.isPresent()) {
            throw new JsonParseException("Terribly sorry, but I will not be able to deserialise " + json);
        }
        final JsonElement element = obj.get("clickEvent").getAsJsonObject().get("value");
        text.click(new ClickEvent(action.get(), this.deserialize(element, element.getClass(), context)));
    }

    if (obj.has("hoverEvent")) {
        final Optional<HoverEvent.Action> action = HOVER_EVENT_ACTION.getEntries().stream().filter(a -> obj
                .get("hoverEvent").getAsJsonObject().get("action").getAsString().equals(a.getInternalName()))
                .findAny();
        if (!action.isPresent()) {
            throw new JsonParseException("Terribly sorry, but I will not be able to deserialise " + json);
        }
        final JsonElement element = obj.get("hoverEvent").getAsJsonObject().get("value");
        text.hover(new HoverEvent(action.get(), this.deserialize(element, element.getClass(), context)));
    }

    if (obj.has("extra")) {
        final JsonArray extra = obj.getAsJsonArray("extra");

        for (int i = 0; i < extra.size(); i++) {
            final JsonElement element = extra.get(i);
            text.append(this.deserialize(element, element.getClass(), context));
        }
    }

    return text.build();
}

From source file:me.trashout.model.serialize.CollectionPointSizeSerializer.java

License:Open Source License

@Override
public Constants.CollectionPointSize deserialize(JsonElement json, Type typeOfT,
        JsonDeserializationContext context) throws JsonParseException {
    Constants.CollectionPointSize collectionPointSize = Constants.CollectionPointSize
            .getCollectionPointSizeByName(json.getAsString());

    if (collectionPointSize == null)
        throw new JsonParseException("Unsupported Collection Point size: " + json.getAsString());

    return collectionPointSize;

}

From source file:me.trashout.model.serialize.CollectionPointTypeSerializer.java

License:Open Source License

@Override
public Constants.CollectionPointType deserialize(JsonElement json, Type typeOfT,
        JsonDeserializationContext context) throws JsonParseException {
    Constants.CollectionPointType collectionPointType = Constants.CollectionPointType
            .getCollectionPointTypeByName(json.getAsString());

    if (collectionPointType == null)
        throw new JsonParseException("Unsupported Collection Point type: " + json.getAsString());

    return collectionPointType;
}

From source file:Misc.GsonUTCdateAdapter.java

License:Apache License

@Override
public synchronized Date deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) {
    try {/*from  w  w w. j  a v a 2 s .  c om*/
        return dateFormat.parse(jsonElement.getAsString());
    } catch (ParseException e) {
        throw new JsonParseException(e);
    }
}

From source file:mobi.tattu.utils.persistance.datastore.gson.RuntimeTypeAdapterFactory.java

License:Apache License

public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
    if (!baseType.isAssignableFrom(type.getRawType())) {
        return null;
    }/*from   ww  w  .j  a  v a2  s .  c om*/

    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 configure a subtype?");
            }
            try {
                return delegate.fromJsonTree(jsonElement);
            } catch (JsonSyntaxException e) {
                return null;
            }
        }

        @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 configure 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);
        }
    }.nullSafe();
}

From source file:net.billforward.gson.typeadapters.RuntimeTypeAdapterFactory.java

License:Apache License

public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
    if (type.getRawType() != baseType) {
        return null;
    }/*ww w. j a va2  s .com*/

    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) {
                delegate = (TypeAdapter<R>) labelToDelegate.get("default_value_mapping");
            }
            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:net.bpiwowar.experimaestro.tasks.ClassChooserAdapter.java

License:Open Source License

@Override
public Object read(JsonReader in) throws IOException {
    // If string, use this
    if (in.peek() == JsonToken.STRING) {
        final String type = in.nextString();
        final Class<?> aClass = types.get(type);
        if (aClass == null) {
            throw new JsonParseException("No type " + type + " defined");
        }// w  w  w. ja v  a 2s .c o  m
        return gson.fromJson(new JsonObject(), aClass);
    }

    // Get the Json object
    final JsonObject json;
    if (in instanceof JsonTreeReader) {
        json = ((JsonTreeReader) in).getJsonObject();
    } else {
        json = new JsonParser().parse(in).getAsJsonObject();
    }

    // Get the type
    final JsonElement _type = json.get("type");
    if (_type == null) {
        throw new JsonParseException("No type defined");
    }
    String type = _type.getAsString();

    // Get the class of the object to create
    final Class<?> aClass = types.get(type);
    if (aClass == null) {
        throw new JsonParseException("No type " + type + " defined");
    }

    return gson.fromJson(json, aClass);
}

From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java

License:Open Source License

public static <T> T gsonDeserialize(Gson gsonIn, Reader readerIn, Class<T> adapter, boolean lenient) {
    try {/*from  w ww  .ja v a  2  s  . c o m*/
        JsonReader jsonreader = new JsonReader(readerIn);
        jsonreader.setLenient(lenient);
        return gsonIn.getAdapter(adapter).read(jsonreader);
    } catch (IOException ioexception) {
        throw new JsonParseException(ioexception);
    }
}

From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java

License:Open Source License

public static <T> T fromJson(Gson p_193838_0_, Reader p_193838_1_, Type p_193838_2_, boolean p_193838_3_) {
    try {/*  www .  j  a v  a2  s  .  c  o m*/
        JsonReader jsonreader = new JsonReader(p_193838_1_);
        jsonreader.setLenient(p_193838_3_);
        return (T) p_193838_0_.getAdapter(TypeToken.get(p_193838_2_)).read(jsonreader);
    } catch (IOException ioexception) {
        throw new JsonParseException(ioexception);
    }
}

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);
    }/*from  www  . jav a 2  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");
                    return iso8601Format.parse(tmp.substring(0, 22) + tmp.substring(23));
                } catch (ParseException e3) {
                    throw new JsonSyntaxException("Invalid date: " + value, e3);
                }
            }
        }
    }
}