diff options
author | Peter Wu <peter@lekensteyn.nl> | 2014-05-10 11:02:07 +0200 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2014-05-10 11:02:07 +0200 |
commit | 670fc2a89d65d9855d50a62f87c1cc061b280215 (patch) | |
tree | a543eba5dc663f6e580c4eeafabff5a9ee7c2ae3 /src/data/ValidatingJsonDeserializer.java | |
parent | 651e477d0da9f74f3e2193de6827c9ff9d098564 (diff) | |
download | Datafiller-670fc2a89d65d9855d50a62f87c1cc061b280215.tar.gz |
Coordinates is an object with an array
Ensure that the array is of a fixed length, add tests to check for that.
Diffstat (limited to 'src/data/ValidatingJsonDeserializer.java')
-rw-r--r-- | src/data/ValidatingJsonDeserializer.java | 48 |
1 files changed, 43 insertions, 5 deletions
diff --git a/src/data/ValidatingJsonDeserializer.java b/src/data/ValidatingJsonDeserializer.java index 5dca3bd..431622b 100644 --- a/src/data/ValidatingJsonDeserializer.java +++ b/src/data/ValidatingJsonDeserializer.java @@ -31,7 +31,8 @@ public class ValidatingJsonDeserializer<T> implements JsonDeserializer<T> { return obj; } - void checkObject(String path, JsonElement je, Class type) { + void checkObject(String path, JsonElement je, Class type) + throws JsonParseException { JsonObject jsonObj = je.getAsJsonObject(); for (Field f : type.getDeclaredFields()) { JsonElement val = jsonObj.get(f.getName()); @@ -52,12 +53,33 @@ public class ValidatingJsonDeserializer<T> implements JsonDeserializer<T> { } } - private void tryValidateProperty(String path, JsonElement je, Field f) { + private void tryValidateProperty(String path, JsonElement je, Field f) + throws JsonParseException { + Class<?> type = f.getType(); // assume that this annotation is only applied to objects Validator v = f.getAnnotation(Validator.class); + ArrayValidator av = f.getAnnotation(ArrayValidator.class); path += f.getName(); + if (av != null) { + if (!type.isArray()) { + throw new RuntimeException("Invalid " + av.getClass().getName() + + " + annotation for " + path); + } + if (!je.isJsonArray()) { + throw new JsonParseException("Expected array: " + path); + } + JsonArray ja = je.getAsJsonArray(); + int minLen = av.minLen(), maxLen = av.maxLen(); + if (minLen >= 0 && ja.size() < minLen) { + throw new JsonParseException("Array smaller than " + + minLen + ": " + path); + } + if (maxLen >= 0 && ja.size() > maxLen) { + throw new JsonParseException("Array larger than " + + maxLen + ": " + path); + } + } if (v != null) { - Class<?> type = f.getType(); if (type.isArray()) { // the class expects an array, so the value must have one too. if (!je.isJsonArray()) { @@ -84,9 +106,25 @@ public class ValidatingJsonDeserializer<T> implements JsonDeserializer<T> { */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) - public @interface Validator { + public @interface ArrayValidator { - Class deserializer() default ValidatingJsonDeserializer.class; + /** + * @return Minimal length for array types (-1 if not checked). + */ + int minLen() default -1; + + /** + * @return Maximum length for array types (-1 if not checked). + */ + int maxLen() default -1; + } + + /** + * Marks a member as object that should be validated too. + */ + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.FIELD) + public @interface Validator { } /** |