diff options
author | Peter Wu <peter@lekensteyn.nl> | 2014-05-09 01:09:29 +0200 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2014-05-09 01:09:29 +0200 |
commit | 8ab0f11e97de813d04e14b881149ea5b6fcc6782 (patch) | |
tree | 34696b9cd44078281d43354c125868c3c33f485e /src/data/ValidatingJsonDeserializer.java | |
parent | faa3102b2b79952c9def9d3c27cadb2c9048287d (diff) | |
download | Datafiller-8ab0f11e97de813d04e14b881149ea5b6fcc6782.tar.gz |
Add missing validation annotations, ...
* Add missing Validator annotations for User.
* Add entities and url properties for user.
* Properly do a recursive check if an Validator annotation is present.
Diffstat (limited to 'src/data/ValidatingJsonDeserializer.java')
-rw-r--r-- | src/data/ValidatingJsonDeserializer.java | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/src/data/ValidatingJsonDeserializer.java b/src/data/ValidatingJsonDeserializer.java index 91c1709..8711f59 100644 --- a/src/data/ValidatingJsonDeserializer.java +++ b/src/data/ValidatingJsonDeserializer.java @@ -2,6 +2,7 @@ package data; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; @@ -25,8 +26,17 @@ public class ValidatingJsonDeserializer<T> implements JsonDeserializer<T> { public T deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException { T obj = new Gson().fromJson(je, type); + checkObject(je, obj.getClass()); + return obj; + } + + private void checkObject(JsonElement je, Class type) { + if (!je.isJsonObject()) { + throw new JsonParseException("Expected object: " + type.getName()); + } JsonObject jsonObj = je.getAsJsonObject(); - for (Field f : obj.getClass().getDeclaredFields()) { + for (Field f : type.getDeclaredFields()) { + System.err.println("verif " + f.getName()); if (!jsonObj.has(f.getName())) { if (f.getAnnotation(Nullable.class) != null) { // null allowed, skip @@ -34,21 +44,29 @@ public class ValidatingJsonDeserializer<T> implements JsonDeserializer<T> { } throw new JsonParseException("Missing field: " + f.getName()); } + tryValidateProperty(jsonObj.get(f.getName()), f); // TODO: validate type? } - return obj; } - public static GsonBuilder addValidation(GsonBuilder gson, Type type) { - GsonBuilder gb = new GsonBuilder(); - gson.registerTypeAdapter(type, new ValidatingJsonDeserializer()); - for (Field f : type.getClass().getDeclaredFields()) { - Validator v = f.getAnnotation(Validator.class); - if (v != null) { - addValidation(gson, v.deserializer()); + private void tryValidateProperty(JsonElement je, Field f) { + // assume that this annotation is only applied to objects + Validator v = f.getAnnotation(Validator.class); + if (v != null) { + Class<?> type = f.getType(); + if (type.isArray()) { + if (!je.isJsonArray()) { + throw new JsonParseException("Not an array: " + f.getName()); + } + JsonArray ja = je.getAsJsonArray(); + type = type.getComponentType(); + for (JsonElement arr_je : ja) { + checkObject(arr_je, type); + } + } else { + checkObject(je, type); } } - return gb; } /** |