From f35ce1eda48511c610e32ef481440160966ead1f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 18 Jul 2015 13:05:49 +1200 Subject: Implement json other decoding --- src/org/traccar/model/MiscFormatter.java | 5 +---- src/org/traccar/web/JsonConverter.java | 9 +++++---- 2 files changed, 6 insertions(+), 8 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/model/MiscFormatter.java b/src/org/traccar/model/MiscFormatter.java index a866f3ec7..d1e1ae959 100644 --- a/src/org/traccar/model/MiscFormatter.java +++ b/src/org/traccar/model/MiscFormatter.java @@ -24,7 +24,6 @@ import javax.json.JsonObject; import javax.json.JsonObjectBuilder; import javax.json.JsonString; import javax.json.JsonValue; -import org.traccar.helper.Log; /** * Format extended tracker status @@ -33,7 +32,7 @@ public class MiscFormatter { private static final String xmlRootNode = "info"; - private static DecimalFormat decimalFormat = new DecimalFormat("#.##"); + private static final DecimalFormat decimalFormat = new DecimalFormat("#.##"); private static String format(Object value) { if (value instanceof Double || value instanceof Float) { @@ -74,8 +73,6 @@ public class MiscFormatter { json.add(entry.getKey(), (Double) entry.getValue()); } else if (entry.getValue() instanceof Boolean) { json.add(entry.getKey(), (Boolean) entry.getValue()); - } else { - Log.warning("Unknown type of data"); } } diff --git a/src/org/traccar/web/JsonConverter.java b/src/org/traccar/web/JsonConverter.java index 0dc4f24eb..d01b91536 100644 --- a/src/org/traccar/web/JsonConverter.java +++ b/src/org/traccar/web/JsonConverter.java @@ -30,6 +30,7 @@ import javax.json.JsonArray; import javax.json.JsonArrayBuilder; import javax.json.JsonObject; import javax.json.JsonObjectBuilder; +import javax.json.JsonReader; import javax.json.JsonValue; import org.traccar.model.Factory; import org.traccar.model.MiscFormatter; @@ -43,7 +44,9 @@ public class JsonConverter { } public static T objectFromJson(Reader reader, T prototype) throws ParseException { - return objectFromJson(Json.createReader(reader).readObject(), prototype); + try (JsonReader jsonReader = Json.createReader(reader)) { + return objectFromJson(jsonReader.readObject(), prototype); + } } public static T objectFromJson(JsonObject json, T prototype) throws ParseException { @@ -72,10 +75,8 @@ public class JsonConverter { method.invoke(object, json.getString(name)); } else if (parameterType.equals(Date.class)) { method.invoke(object, dateFormat.parse(json.getString(name))); - } else if (parameterType.isEnum()) { - method.invoke(object, Enum.valueOf((Class) parameterType, json.getString(name))); } else if (parameterType.equals(Map.class)) { - // TODO: method.invoke(object, json.getString(name)); + method.invoke(object, MiscFormatter.fromJson(json.getJsonObject(name))); } } catch (IllegalAccessException | InvocationTargetException error) { } -- cgit v1.2.3