aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/web/JsonConverter.java
diff options
context:
space:
mode:
authorRafael Guterres <guterresrafael@gmail.com>2015-11-27 03:32:59 -0200
committerRafael Guterres <guterresrafael@gmail.com>2015-11-27 03:32:59 -0200
commitc57bd2d472467b1b3a45aee1b97c9a0aeef5958a (patch)
tree26fae20adf1970b5834f06393a3c703c972f39b1 /src/org/traccar/web/JsonConverter.java
parent77cd23da84ebebcf99a97a8aef88aa9a4884ca40 (diff)
downloadtrackermap-server-c57bd2d472467b1b3a45aee1b97c9a0aeef5958a.tar.gz
trackermap-server-c57bd2d472467b1b3a45aee1b97c9a0aeef5958a.tar.bz2
trackermap-server-c57bd2d472467b1b3a45aee1b97c9a0aeef5958a.zip
Initial implementation of new api with jax-rs.
Revert servlets to old api and remove plurals. Fix findbugs for header origin.
Diffstat (limited to 'src/org/traccar/web/JsonConverter.java')
-rw-r--r--src/org/traccar/web/JsonConverter.java17
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) {