@Override public BlockMeshPart deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final JsonObject meshObj = json.getAsJsonObject(); final Vector3f[] vertices = context.deserialize(meshObj.get("vertices"), Vector3f[].class); final Vector3f[] normals = context.deserialize(meshObj.get("normals"), Vector3f[].class); final Vector2f[] texCoords = context.deserialize(meshObj.get("texcoords"), Vector2f[].class); throw new JsonParseException("Vertices missing"); throw new JsonParseException("Normals missing"); throw new JsonParseException("Texcoords missing"); if (!meshObj.has("faces")) { throw new JsonParseException("Faces missing"); throw new JsonParseException("vertices, normals and texcoords must have the same length"); throw new JsonParseException("Face value out of range: " + value + ", max vertex is " + (vertices.length - 1));
private void invalidJsonPayload(JsonParseException ex, Request req, Response res) { res.body(new Gson().toJson(Collections.singletonMap("error", "Payload data is not valid JSON: " + ex.getMessage()))); res.status(HttpStatus.SC_BAD_REQUEST); }
@Override public Message deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jObj = json.getAsJsonObject(); Map.Entry<String, JsonElement> entry = jObj.entrySet().iterator().next(); String className = entry.getKey(); Class<?> clazz; try { clazz = Class.forName(className); } catch (ClassNotFoundException e) { throw new JsonParseException("Unable to deserialize class " + className, e); } return (Message) this.gson.fromJson(entry.getValue(), clazz); }
/** * Read metadata from reader. * * @param reader reader to read from * @return Metadata or null if nothing could be read from the reader * @throws TikaException in case of parse failure by Gson or IO failure with Reader */ public static Metadata fromJson(Reader reader) throws TikaException { Metadata m = null; try { m = GSON.fromJson(reader, Metadata.class); } catch (com.google.gson.JsonParseException e){ //covers both io and parse exceptions throw new TikaException(e.getMessage()); } return m; }
@Override public T deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); JsonPrimitive jsonPrimitive = (JsonPrimitive) jsonObject.get(CLASSNAME); String className = jsonPrimitive.getAsString(); Class<?> clazz; try { clazz = Class.forName(className); } catch (ClassNotFoundException e) { throw new JsonParseException(e.getMessage()); } return context.deserialize(jsonObject.get(VALUE), clazz); }
@Override public IMaterialRenderInfoDeserializer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); String type = jsonObject.get("type").getAsString(); Class<? extends IMaterialRenderInfoDeserializer> deserializerClass = renderInfoDeserializers.get(type); if(deserializerClass == null) { throw new JsonParseException("Unknown material texture type: " + type); } JsonElement parameters = jsonObject.get("parameters"); IMaterialRenderInfoDeserializer deserializer = GSON.fromJson(parameters, deserializerClass); if(deserializer != null && jsonObject.has("suffix")) { deserializer.setSuffix(jsonObject.get("suffix").getAsString()); } return deserializer; } }
if (root.has("id")) { builder.id(root.get("id").getAsString()); 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()) { throw new JsonParseException( String.format("Couldn't deserialize %s : %s. Not a primitive or object", VALUE_PROPERTY, value));
@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) && !srcType.getSimpleName().equals("OldInput")) { throw new JsonParseException("cannot serialize " + srcType.getName() + " because it already defines a field named " + typeFieldName); } JsonObject clone = new JsonObject(); if (!srcType.getSimpleName().equals("OldInput")) { 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();
public Stats deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = (JsonObject) json; Stats statsObj = null; //First, check we have all our required fields .. for (String fieldName : requiredFields) { if (jsonObject.get(fieldName) == null) { throw new JsonParseException("Required Field Not Found: " + fieldName); } } return new Gson().fromJson(json, Stats.class); }
public JsonReader jsonReaderFrom(String string) { try { if (StringUtils.isBlank(string)) { string = "{}"; } return new JsonReader(GSON.fromJson(string, JsonElement.class).getAsJsonObject()); } catch (Exception e) { throw new JsonParseException(e); } }
@Override public Throwable deserialize(JsonElement json, Type type, JsonDeserializationContext ctx) throws JsonParseException { JsonObject obj = (JsonObject)json; String className = obj.get("class").getAsString(); try { Class<Throwable> clazz = (Class<Throwable>)Class.forName(className); Throwable cause = s_gson.fromJson(obj.get("cause"), Throwable.class); String msg = obj.get("msg").getAsString(); Constructor<Throwable> constructor = clazz.getConstructor(String.class, Throwable.class); Throwable th = constructor.newInstance(msg, cause); return th; } catch (ClassNotFoundException e) { throw new JsonParseException("Unable to find " + className); } catch (NoSuchMethodException e) { throw new JsonParseException("Unable to find constructor for " + className); } catch (SecurityException e) { throw new JsonParseException("Unable to get over security " + className); } catch (InstantiationException e) { throw new JsonParseException("Unable to instantiate " + className); } catch (IllegalAccessException e) { throw new JsonParseException("Illegal access to " + className, e); } catch (IllegalArgumentException e) { throw new JsonParseException("Illegal argument to " + className, e); } catch (InvocationTargetException e) { throw new JsonParseException("Cannot invoke " + className, e); } }
@Override public Map<String, String> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject obj = json.getAsJsonObject(); JsonElement texElem = obj.get("textures"); if(texElem == null) { throw new JsonParseException("Missing textures entry in json"); } return GSON.fromJson(texElem, TYPE); } }
@SuppressWarnings("unchecked") @Override public void write(JsonWriter out, R value) throws IOException { Class<?> valueClass = value.getClass(); String valueTypeName = valueClass.getName(); TypeToken<?> valueType = TypeToken.get(valueClass); TypeAdapter<R> delegate = (TypeAdapter<R>) gson.getDelegateAdapter(PolymorphicTypeAdapterFactory.this, valueType); if (delegate == null) { throw new JsonParseException("Could not serialize " + valueClass.getName()); } JsonElement jsonElement = delegate.toJsonTree(value); if (valueClass != baseClass) { JsonObject jsonObject = jsonElement.getAsJsonObject(); JsonObject clone = new JsonObject(); clone.add(TYPE_FIELD_NAME, new JsonPrimitive(valueTypeName)); for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } Streams.write(clone, out); } else { Streams.write(jsonElement, out); } }
private static DynamoDBEntry toEntry(JsonElement data) { if (data == null || data.isJsonNull()) { return DynamoDBNull.NULL; if (data.isJsonObject()) { final Document doc = new Document(); for (final Entry<String, JsonElement> entry : data.getAsJsonObject().entrySet()) { final String key = entry.getKey(); final JsonElement element = entry.getValue(); final JsonPrimitive primitive = data.getAsJsonPrimitive(); if (primitive.isBoolean()) { return new DynamoDBBool(primitive.getAsBoolean()); if (primitive.isString()) { return new Primitive(primitive.getAsString()); throw new JsonParseException("unable to parse json for key " + data.toString());
@Override public Filters deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final JsonObject j = json.getAsJsonObject(); final JsonElement filters = j.get(KEY_FILTERS); if (null == filters) { throw new JsonParseException("Missing filters array!"); } final ArrayList<DashboardFilter> viewFilters = new ArrayList<>(); filters.getAsJsonArray().forEach((f) -> viewFilters.add(context.deserialize(f, DashboardFilter.class))); return new Filters(viewFilters); } }
Class<?> valueClass; if (jsonElement.isJsonObject()) { JsonElement typeNameJsonElement = jsonElement.getAsJsonObject().remove(TYPE_FIELD_NAME); String typeName = typeNameJsonElement.getAsString(); throw new JsonParseException("Could not find class " + typeName); throw new JsonParseException(valueClass.getName() + " does not derive from " + baseClass.getName()); gson.getDelegateAdapter(PolymorphicTypeAdapterFactory.this, valueType); throw new JsonParseException("Could not deserialize " + valueClass.getName());
@Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName); String label = (labelJsonElement == null ? null : 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 Food deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { FoodBase food = new FoodBase(); if (json.isJsonObject()) { JsonObject jsonObject = json.getAsJsonObject(); food.setName(jsonObject.getAsJsonPrimitive("name").getAsString()); JsonObject materials = jsonObject.getAsJsonObject("materials"); materials.entrySet().forEach(entry -> food.getMaterialMap().put(FoodManager.INSTANCE.materialMap.get(entry.getKey()), entry.getValue().getAsInt())); } else { throw new JsonParseException("JSON " + json + " is not a JsonObject!"); } return food; }
@Override public DashboardFilter deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); final String name = defensivelyGetString(jsonObject, KEY_NAME); jsonObject.addProperty(KEY_NAME, name); final String type = defensivelyGetString(jsonObject, KEY_TYPE); if (StringUtils.isBlank(type)) throw new JsonParseException("Missing filter type"); switch (type) { case TYPE_WHITELIST: return context.deserialize(jsonObject, WhitelistFilter.class); case TYPE_BLACKLIST: return context.deserialize(jsonObject, BlacklistFilter.class); default: throw new JsonParseException(format("Don't know how to deserialize filter type: \"%s\"", type)); } } }
@Override public Function deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); String className = CommonGsonHelper.getOrDie(jsonObject, "class").getAsJsonPrimitive().getAsString(); Class clazz; try { clazz = Class.forName(className); } catch (ClassNotFoundException e) { e.printStackTrace(); throw new JsonParseException(e); } return context.deserialize(CommonGsonHelper.getOrDie(jsonObject, "body"), clazz); } }