List of usage examples for com.google.gson JsonParseException JsonParseException
public JsonParseException(Throwable cause)
From source file:net.minecraftforge.gradle.util.json.DateAdapter.java
License:Open Source 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 w w w . j a va 2s.c o 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"); if (tmp.length() < 22) return new Date(); else return iso8601Format.parse(tmp.substring(0, 22) + tmp.substring(23)); } catch (ParseException e3) { throw new JsonSyntaxException("Invalid date: " + value, e3); } } } } }
From source file:net.minecraftforge.oredict.ShapelessOreRecipe.java
License:Open Source License
public static ShapelessOreRecipe factory(JsonContext context, JsonObject json) { String group = JsonUtils.getString(json, "group", ""); NonNullList<Ingredient> ings = NonNullList.create(); for (JsonElement ele : JsonUtils.getJsonArray(json, "ingredients")) ings.add(CraftingHelper.getIngredient(ele, context)); if (ings.isEmpty()) throw new JsonParseException("No ingredients for shapeless recipe"); ItemStack itemstack = CraftingHelper.getItemStack(JsonUtils.getJsonObject(json, "result"), context); return new ShapelessOreRecipe(group.isEmpty() ? null : new ResourceLocation(group), ings, itemstack); }
From source file:net.segoia.event.eventbus.config.json.EventBusJsonConfigLoader.java
License:Apache License
public static EventBusJsonConfig load(Reader reader) { GsonBuilder gb = new GsonBuilder(); gb.registerTypeAdapter(Condition.class, new JsonDeserializer<Condition>() { @Override/* w w w. j av a 2 s . co m*/ public Condition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jo = json.getAsJsonObject(); JsonElement conditionType = jo.get("ctype"); String ctype = null; if (conditionType != null) { ctype = conditionType.getAsString().trim(); if ("".equals(ctype)) { ctype = null; } } JsonDeserializer<?> deserializerForType = jsonDeserializers.get(ctype); if (deserializerForType == null) { throw new JsonParseException("No deserializer defined for condition type " + ctype); } return (Condition) deserializerForType.deserialize(json, typeOfT, context); } }); gb.registerTypeAdapterFactory(new TypeAdapterFactory() { @Override public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { final TypeAdapter<T> delegateAdapter = gson.getDelegateAdapter(this, type); TypeAdapter<T> typeAdapter = new TypeAdapter<T>() { @Override public void write(JsonWriter out, T value) throws IOException { delegateAdapter.write(out, value); } @Override public T read(JsonReader in) throws IOException { JsonElement value = Streams.parse(in); if (value.isJsonNull()) { return null; } if (!value.isJsonObject()) { return delegateAdapter.fromJsonTree(value); } // System.out.println(value+" "+value.getClass()); JsonObject jo = value.getAsJsonObject(); JsonElement cnameElem = jo.remove("className"); if (cnameElem != null) { String cname = cnameElem.getAsString(); try { // System.out.println("using clazz " + cname); return (T) gson.fromJson(value, Class.forName(cname)); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } else { return delegateAdapter.fromJsonTree(value); } } }; return typeAdapter; } }); final Gson gson = gb.create(); EventBusJsonConfig ebusConfig = gson.fromJson(reader, EventBusJsonConfig.class); return ebusConfig; }
From source file:net.simonvt.cathode.jobqueue.JobSerializer.java
License:Apache License
private JsonElement get(final JsonObject wrapper, final String memberName) { final JsonElement elem = wrapper.get(memberName); if (elem == null) { throw new JsonParseException("no '" + memberName + "' member found in json file."); }/*from w w w. j a v a2 s . c om*/ return elem; }
From source file:net.technicpack.launchercore.util.DateTypeAdapter.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 ww w .j av a 2 s .c o m*/ Date date = deserializeToDate(json); if (typeOfT == Date.class) { return date; } throw new IllegalArgumentException(getClass() + " cannot deserialize to " + typeOfT); }
From source file:networkUtils.RuntimeTypeAdapterFactory.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 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); String label = predicate.process(jsonElement); @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 { // Unimplemented as we don't use write. /*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(null, out); } }; }
From source file:ninja.leaping.configurate.gson.GsonConfigurationLoader.java
License:Apache License
private void parseArray(JsonReader parser, ConfigurationNode node) throws IOException { parser.beginArray();/* w w w .j a v a 2s . c o m*/ JsonToken token; while ((token = parser.peek()) != null) { switch (token) { case END_ARRAY: parser.endArray(); return; default: parseValue(parser, node.getAppendedNode()); } } throw new JsonParseException("Reached end of stream with unclosed array at!"); }
From source file:ninja.leaping.configurate.gson.GsonConfigurationLoader.java
License:Apache License
private void parseObject(JsonReader parser, ConfigurationNode node) throws IOException { parser.beginObject();//www .jav a 2s .c o m JsonToken token; while ((token = parser.peek()) != null) { switch (token) { case END_OBJECT: case END_DOCUMENT: parser.endObject(); return; case NAME: parseValue(parser, node.getNode(parser.nextName())); break; default: throw new JsonParseException("Received improper object value " + token); } } throw new JsonParseException("Reached end of stream with unclosed object!"); }
From source file:ooo.oxo.mr.net.ColorTypeAdapter.java
License:Open Source License
@Override public Color read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull();/*from w ww . j a va 2 s. c o m*/ return null; } else { try { return Color.parse(in.nextString()); } catch (IllegalArgumentException e) { throw new JsonParseException(e); } } }
From source file:org.apache.aurora.common.zookeeper.JsonCodec.java
License:Apache License
private static void assertRequiredField(String fieldName, Object fieldValue) { if (fieldValue == null) { throw new JsonParseException(String.format("Field %s is required", fieldName)); }/*from www . j a va2 s .c om*/ }