diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-12-01 15:20:05 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-12-01 15:20:05 +1300 |
commit | ef2ae4103726dae12a0153886219d4ac43d20ae0 (patch) | |
tree | 79291f74ce2631a4a9a176b7e510028e534a23d4 /src/org/traccar/web/JsonConverter.java | |
parent | c6692703c8099577cb86c3eace5d8d1bc11f212d (diff) | |
parent | 53f70085891cdb0feae56df8990c1245091352a1 (diff) | |
download | trackermap-server-ef2ae4103726dae12a0153886219d4ac43d20ae0.tar.gz trackermap-server-ef2ae4103726dae12a0153886219d4ac43d20ae0.tar.bz2 trackermap-server-ef2ae4103726dae12a0153886219d4ac43d20ae0.zip |
Merge branch 'rest' into master
Diffstat (limited to 'src/org/traccar/web/JsonConverter.java')
-rw-r--r-- | src/org/traccar/web/JsonConverter.java | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/src/org/traccar/web/JsonConverter.java b/src/org/traccar/web/JsonConverter.java index c01ce8bd6..2ef61fb13 100644 --- a/src/org/traccar/web/JsonConverter.java +++ b/src/org/traccar/web/JsonConverter.java @@ -43,6 +43,14 @@ public final class JsonConverter { private JsonConverter() { } + private static <T> T newClassInstance(Class<T> clazz) { + try { + return clazz.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + throw new IllegalArgumentException(); + } + } + private static final DateTimeFormatter DATE_FORMAT = ISODateTimeFormat.dateTime(); public static Date parseDate(String value) { @@ -57,9 +65,23 @@ public final class JsonConverter { public static <T extends Factory> T objectFromJson(JsonObject json, T prototype) { T object = (T) prototype.create(); + Method[] methods = object.getClass().getMethods(); + return objectFromJson(json, object, methods); + } + public static <T> T objectFromJson(Reader reader, Class<T> clazz) throws ParseException { + try (JsonReader jsonReader = Json.createReader(reader)) { + return objectFromJson(jsonReader.readObject(), clazz); + } + } + + public static <T> T objectFromJson(JsonObject json, Class<T> clazz) { + T object = newClassInstance(clazz); Method[] methods = object.getClass().getMethods(); + return objectFromJson(json, object, methods); + } + private static <T> T objectFromJson(JsonObject json, T object, Method[] methods) { for (final Method method : methods) { if (method.getName().startsWith("set") && method.getParameterTypes().length == 1) { @@ -91,7 +113,6 @@ public final class JsonConverter { } } } - return object; } |