List of usage examples for com.google.gson JsonParseException JsonParseException
public JsonParseException(Throwable cause)
From source file:it.newfammulfin.api.util.converters.CurrencyUnitConverter.java
@Override public CurrencyUnit deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jdc) throws JsonParseException { if (!(jsonElement instanceof JsonPrimitive)) { throw new JsonParseException("Json object expected"); }//from w ww. ja va 2 s .com return CurrencyUnit.getInstance(jsonElement.getAsString()); }
From source file:it.newfammulfin.api.util.converters.KeyConverter.java
@Override public Key<?> deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jdc) throws JsonParseException { if (!(jsonElement instanceof JsonObject)) { throw new JsonParseException("Json object expected"); }//from w w w. j a va 2s. c o m if (((JsonObject) jsonElement).get("raw") == null) { throw new JsonParseException("Json object expected"); } String rawString = ((JsonObject) jsonElement).get("raw").getAsString(); return Key.create(rawString); }
From source file:it.newfammulfin.api.util.converters.MoneyConverter.java
@Override public Money deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jdc) throws JsonParseException { if (!(jsonElement instanceof JsonObject)) { throw new JsonParseException("Json object expected"); }/*from w w w. ja va 2s . c o m*/ JsonObject jsonObject = (JsonObject) jsonElement; if (!jsonObject.has(VALUE_FIELD) || !jsonObject.has(CURRENCY_FIELD)) { throw new JsonParseException( "Fields missing (expected " + VALUE_FIELD + " and " + CURRENCY_FIELD + ")"); } Money money = Money.of(CurrencyUnit.getInstance(jsonObject.get(CURRENCY_FIELD).getAsString()), jsonObject.get(VALUE_FIELD).getAsDouble()); return money; }
From source file:jp.yokomark.utils.gson.utils.JsonElementUtils.java
License:Open Source License
public static JsonPrimitive getAsJsonPrimitiveOrThrow(JsonElement element) throws JsonParseException { if (!element.isJsonPrimitive()) { throw new JsonParseException("this element is not a json primitive: " + element); }/* w w w .j a v a2 s. co m*/ return element.getAsJsonPrimitive(); }
From source file:jp.yokomark.utils.gson.utils.JsonElementUtils.java
License:Open Source License
public static String getAsStringOrThrow(JsonPrimitive primitive) throws JsonParseException { if (!primitive.isString()) { throw new JsonParseException("this primitive is not a json string: " + primitive); }/*from w w w. j av a2 s.c o m*/ return primitive.getAsString(); }
From source file:jp.yokomark.utils.gson.utils.JsonElementUtils.java
License:Open Source License
public static long getAsLongOrThrow(JsonPrimitive primitive) throws JsonParseException { if (!primitive.isNumber()) { throw new JsonParseException("this primitive is not a json number: " + primitive); }// w ww . j ava 2s . c o m return primitive.getAsLong(); }
From source file:jsondiscoverer.JsonSource.java
License:Open Source License
/** * Builds a {@link JsonData} element out of a JSON document representing the input and another * JSON document representing the output. * <p>//w w w . j a v a 2 s . c om * The input/output must be provided as a valid JSON objects. * * @param input The {@link Reader} from which obtain JSON document used as input (optional) * @param output The {@link Reader} from which obtain the JSON document * @throws IllegalArgumentException If any reader is null * @return The {@link JsonData} with the JSON document and the input */ private JsonData buildJsonData(Reader input, Reader output) { if (output == null) throw new IllegalArgumentException("The JSON document cannot be null and must exist"); JsonObject inputJsonObject = null; if (input != null) { JsonElement inputElement = (new JsonParser()).parse(new JsonReader(input)); if (!inputElement.isJsonObject()) throw new JsonParseException("The input value must be a valid JSON object. Received " + input); inputJsonObject = inputElement.getAsJsonObject(); } JsonElement rootElement = (new JsonParser()).parse(new JsonReader(output)); JsonData data = new JsonData(inputJsonObject, rootElement); return data; }
From source file:leafcutter.client.Serializer.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }//from w w w .ja v a 2s. co 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); } }.nullSafe(); }
From source file:me.cybermaxke.elementarrows.forge.json.JsonItemStack.java
License:Open Source License
@Override public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject json0 = json.getAsJsonObject(); String id = json0.get("id").getAsString(); Item item = (Item) Item.itemRegistry.getObject(id); /**// www. j av a2 s. c om * Try to find the item using it's legacy id. */ if (item == null) { try { item = Item.getItemById(Integer.parseInt(id)); } catch (NumberFormatException e) { } } if (item == null) { throw new JsonParseException("Unknown effect id! (" + id + ")"); } int amount = json0.get("amount").getAsInt(); int data = json0.get("data").getAsInt(); if (data < 0) { data = 0; } return new ItemStack(item, amount, data); }
From source file:me.cybermaxke.elementarrows.forge.json.JsonPotionEffect.java
License:Open Source License
@Override public PotionEffect deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject json0 = json.getAsJsonObject(); String id = json0.get("effectId").getAsString(); int id0;//w ww.j av a 2 s . c om try { id0 = Integer.parseInt(id); if (id0 >= Potion.potionTypes.length || Potion.potionTypes[id0] == null) { throw new JsonParseException("Unknown effect id! (" + id0 + ")"); } } catch (NumberFormatException e) { id = id.toLowerCase(); if (this.byName.containsKey(id)) { id0 = this.byName.get(id); } else { throw new JsonParseException("Unknown effect id! (" + id + ")"); } } int duration = json0.get("duration").getAsInt(); int amplifier = json0.get("amplifier").getAsInt(); boolean ambient = json0.get("ambient").getAsBoolean(); return new PotionEffect(id0, duration, amplifier, ambient); }