diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-07-17 19:35:30 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-07-17 19:35:30 +1200 |
commit | 1262a26ffd8be27609eccc23d4c652c2b0083b46 (patch) | |
tree | 37c475ab849c886de8cff12007e80d6f711a7caf | |
parent | 3f6f92cedd969e741e65bb6e14737b98722b214e (diff) | |
download | trackermap-server-1262a26ffd8be27609eccc23d4c652c2b0083b46.tar.gz trackermap-server-1262a26ffd8be27609eccc23d4c652c2b0083b46.tar.bz2 trackermap-server-1262a26ffd8be27609eccc23d4c652c2b0083b46.zip |
Remove enum json conversion
-rw-r--r-- | src/org/traccar/web/EnumFactory.java | 20 | ||||
-rw-r--r-- | src/org/traccar/web/JsonConverter.java | 20 | ||||
-rw-r--r-- | test/org/traccar/web/JsonConverterTest.java | 187 |
3 files changed, 49 insertions, 178 deletions
diff --git a/src/org/traccar/web/EnumFactory.java b/src/org/traccar/web/EnumFactory.java deleted file mode 100644 index efbfd4b20..000000000 --- a/src/org/traccar/web/EnumFactory.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.traccar.web; - -import org.traccar.model.Factory; - -import javax.json.JsonObject; - -public class EnumFactory<T extends Enum<T> & Factory> { - private Class<T> commandTypeClass; - private String jsonKey; - - public EnumFactory(Class<T> commandTypeClass, String type) { - this.commandTypeClass = commandTypeClass; - jsonKey = type; - } - - public <K> K create(JsonObject json) { - Factory factory = Enum.valueOf(commandTypeClass, json.getString(jsonKey)); - return (K) factory.create(); - } -} diff --git a/src/org/traccar/web/JsonConverter.java b/src/org/traccar/web/JsonConverter.java index 7962ac8ab..0dc4f24eb 100644 --- a/src/org/traccar/web/JsonConverter.java +++ b/src/org/traccar/web/JsonConverter.java @@ -46,20 +46,9 @@ public class JsonConverter { return objectFromJson(Json.createReader(reader).readObject(), prototype); } - public static <T> T enumObjectFromJson(Reader reader, EnumFactory<? extends Enum<?>> factory) throws ParseException { - JsonObject json = Json.createReader(reader).readObject(); - T object = factory.<T>create(json); - populateObject(json, object); - return object; - } - public static <T extends Factory> T objectFromJson(JsonObject json, T prototype) throws ParseException { T object = (T) prototype.create(); - populateObject(json, object); - return object; - } - private static void populateObject(JsonObject json, Object object) throws ParseException { Method[] methods = object.getClass().getMethods(); for (final Method method : methods) { @@ -86,17 +75,14 @@ public class JsonConverter { } else if (parameterType.isEnum()) { method.invoke(object, Enum.valueOf((Class<? extends Enum>) parameterType, json.getString(name))); } else if (parameterType.equals(Map.class)) { - //method.invoke(object, json.getString(name)); - } else { - Object nestedObject = parameterType.newInstance(); - populateObject(json.getJsonObject(name), nestedObject); - method.invoke(object, nestedObject); + // TODO: method.invoke(object, json.getString(name)); } - } catch (IllegalAccessException | InvocationTargetException | InstantiationException error) { + } catch (IllegalAccessException | InvocationTargetException error) { } } } + return object; } public static <T> JsonObject objectToJson(T object) { diff --git a/test/org/traccar/web/JsonConverterTest.java b/test/org/traccar/web/JsonConverterTest.java index 07d70d3f2..b02ec2d40 100644 --- a/test/org/traccar/web/JsonConverterTest.java +++ b/test/org/traccar/web/JsonConverterTest.java @@ -1,170 +1,75 @@ package org.traccar.web; -import org.junit.Test; -import org.traccar.model.Factory; - -import java.io.Reader; import java.io.StringReader; import java.text.ParseException; import java.util.Calendar; import java.util.Date; - import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import org.junit.Test; +import org.traccar.model.Factory; public class JsonConverterTest { - private <T extends Factory> T convert(String jsonString, T prototype) throws ParseException { - Reader r = new StringReader( - jsonString); - - return JsonConverter.objectFromJson(r, prototype); - } - @Test public void primitiveConversion() throws ParseException { - AllPrimitives o = convert("{" + - "\"aBoolean\": true, " + - "\"anInt\": 42, " + - "\"aDouble\": 41.99, " + - "\"aString\": \"discworld\", " + - "\"aDate\":\"2015-07-09T19:02:17\"" + - "}", - new AllPrimitives()); - - assertEquals(true, o.getaBoolean()); - assertEquals(42, o.getAnInt()); - assertEquals(41.99, o.getaDouble(), 0.001); - assertEquals("discworld", o.getaString()); + Primitives o = JsonConverter.objectFromJson(new StringReader( + "{" + + "\"boolean\": true, " + + "\"int\": 42, " + + "\"double\": 41.99, " + + "\"string\": \"discworld\", " + + "\"date\":\"2015-07-09T19:02:17\"" + + "}"), + new Primitives()); + + assertEquals(true, o.getBoolean()); + assertEquals(42, o.getInt()); + assertEquals(41.99, o.getDouble(), 0.001); + assertEquals("discworld", o.getString()); Calendar c = Calendar.getInstance(); - c.setTime(o.getaDate()); + c.setTime(o.getDate()); assertEquals(2015, c.get(Calendar.YEAR)); assertEquals(Calendar.JULY, c.get(Calendar.MONTH)); assertEquals(9, c.get(Calendar.DAY_OF_MONTH)); assertEquals(19, c.get(Calendar.HOUR_OF_DAY)); assertEquals(2, c.get(Calendar.MINUTE)); assertEquals(17, c.get(Calendar.SECOND)); - } - - public static class AllPrimitives implements Factory { - - private boolean aBoolean; - private int anInt; - private double aDouble; - private String aString; - private Date aDate; - - - @Override - public Object create() { - return new AllPrimitives(); - } - - public boolean getaBoolean() { - return aBoolean; - } - - public void setaBoolean(boolean aBoolean) { - this.aBoolean = aBoolean; - } - - public int getAnInt() { - return anInt; - } - - public void setAnInt(int anInt) { - this.anInt = anInt; - } - - public double getaDouble() { - return aDouble; - } - - public void setaDouble(double aDouble) { - this.aDouble = aDouble; - } - - public String getaString() { - return aString; - } - - public void setaString(String aString) { - this.aString = aString; - } - public Date getaDate() { - return aDate; - } - - public void setaDate(Date aDate) { - this.aDate = aDate; - } - } - - - @Test - public void enumConversion() throws ParseException { - ObjectWithEnum o = convert("{\"anEnum\": \"VALUE2\"}", new ObjectWithEnum()); - assertEquals(TestEnum.VALUE2, o.getAnEnum()); - } - - - public enum TestEnum { - VALUE1, VALUE2 } - public static class ObjectWithEnum implements Factory { - private TestEnum anEnum; - - public TestEnum getAnEnum() { - return anEnum; - } - - public void setAnEnum(TestEnum anEnum) { - this.anEnum = anEnum; - } - + public static class Primitives implements Factory { + @Override - public Object create() { - return new ObjectWithEnum(); - } - } - + public Primitives create() { + return new Primitives(); + } + + private boolean b; + public boolean getBoolean() { return b; } + public void setBoolean(boolean b) { this.b = b; } + + private int i; + public int getInt() { return i; } + public void setInt(int i) { this.i = i; } + + private long l; + public long getLong() { return l; } + public void setLong(long l) { this.l = l; } + + private double d; + public double getDouble() { return d; } + public void setDouble(double d) { this.d = d; } + + private String s; + public String getString() { return s; } + public void setString(String s) { this.s = s; } + + private Date t; + public Date getDate() { return t; } + public void setDate(Date t) { this.t = t; } - @Test - public void nestedObjectsConversion() throws ParseException { - NestedObjects o = convert("{\"name\": \"Rincewind\", \"nestedObject\": {\"anEnum\":\"VALUE1\"}}", new NestedObjects()); - assertEquals("Rincewind", o.getName()); - assertNotNull("The nested object should be populated", o.getNestedObject()); - assertEquals(TestEnum.VALUE1, o.getNestedObject().getAnEnum()); } - public static class NestedObjects implements Factory { - - private String name; - private ObjectWithEnum nestedObject; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public ObjectWithEnum getNestedObject() { - return nestedObject; - } - - public void setNestedObject(ObjectWithEnum nestedObject) { - this.nestedObject = nestedObject; - } - - @Override - public Object create() { - return new NestedObjects(); - } - } } |