diff options
Diffstat (limited to 'src/org/traccar/web/JsonConverter.java')
-rw-r--r-- | src/org/traccar/web/JsonConverter.java | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/org/traccar/web/JsonConverter.java b/src/org/traccar/web/JsonConverter.java index a8b68613b..38721db61 100644 --- a/src/org/traccar/web/JsonConverter.java +++ b/src/org/traccar/web/JsonConverter.java @@ -36,6 +36,7 @@ import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.ISODateTimeFormat; import org.traccar.helper.Clazz; import org.traccar.helper.Log; +import org.traccar.model.Factory; import org.traccar.model.MiscFormatter; public final class JsonConverter { @@ -49,6 +50,18 @@ public final class JsonConverter { return DATE_FORMAT.parseDateTime(value).toDate(); } + public static <T extends Factory> T objectFromJson(Reader reader, T prototype) throws ParseException { + try (JsonReader jsonReader = Json.createReader(reader)) { + return objectFromJson(jsonReader.readObject(), prototype); + } + } + + 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); @@ -58,6 +71,10 @@ public final class JsonConverter { public static <T> T objectFromJson(JsonObject json, Class<T> clazz) { T object = Clazz.newInstance(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) { |