changeset 33:7f94e7f977bb

Add Timestamp typeadapter to JSONService This patch adds a Timetamp typeadapter to JSONService. With this change, JSONService treats Timetamps as the previous custom typeadapters did. Reviewed-by: ebaron Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-October/025563.html
author Simon Tooke <stooke@redhat.com>
date Fri, 27 Oct 2017 13:25:27 -0400
parents 9af3b8b15804
children dd224447c87d
files common/src/main/java/com/redhat/thermostat/lang/schema/internal/JSONServiceImpl.java common/src/test/java/com/redhat/thermostat/lang/schema/internal/JSONServiceTest.java
diffstat 2 files changed, 34 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/common/src/main/java/com/redhat/thermostat/lang/schema/internal/JSONServiceImpl.java	Fri Oct 27 13:23:07 2017 -0400
+++ b/common/src/main/java/com/redhat/thermostat/lang/schema/internal/JSONServiceImpl.java	Fri Oct 27 13:25:27 2017 -0400
@@ -47,6 +47,7 @@
 import com.redhat.thermostat.lang.schema.*;
 
 import com.redhat.thermostat.lang.schema.annotations.Schema;
+import com.redhat.thermostat.lang.schema.models.Timestamp;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Service;
@@ -94,6 +95,9 @@
         final LongTypeAdapter longTypeAdapter = new LongTypeAdapter();
         builder.registerTypeAdapter(long.class, longTypeAdapter);
         builder.registerTypeAdapter(Long.class, longTypeAdapter);
+
+        final TimestampTypeAdapter timestampTypeAdapter = new TimestampTypeAdapter();
+        builder.registerTypeAdapter(Timestamp.class, timestampTypeAdapter);
     }
 
     private void registerPhase2TypeAdapters() {
@@ -169,6 +173,35 @@
         }
     }
 
+    private static class TimestampTypeAdapter extends TypeAdapter<Timestamp> {
+
+        private static final String NUMBER_LONG_IDENTIFIER = "$numberLong";
+
+        @Override
+        public void write(JsonWriter out, Timestamp value) throws IOException {
+            if (value == null) {
+                out.nullValue();
+            } else {
+                out.beginObject();
+                out.name(NUMBER_LONG_IDENTIFIER);
+                out.value("" + value.get());
+                out.endObject();
+            }
+        }
+
+        @Override
+        public Timestamp read(JsonReader in) throws IOException {
+            in.beginObject();
+            String name = in.nextName();
+            if (!name.equals(NUMBER_LONG_IDENTIFIER)) {
+                throw new JsonSyntaxException("Unexpected name: " + name);
+            }
+            Timestamp returnValue = new Timestamp(in.nextLong());
+            in.endObject();
+            return returnValue;
+        }
+    }
+
     public class AutoExclusionStategy implements ExclusionStrategy {
 
         public boolean shouldSkipClass(Class<?> arg0) {
--- a/common/src/test/java/com/redhat/thermostat/lang/schema/internal/JSONServiceTest.java	Fri Oct 27 13:23:07 2017 -0400
+++ b/common/src/test/java/com/redhat/thermostat/lang/schema/internal/JSONServiceTest.java	Fri Oct 27 13:25:27 2017 -0400
@@ -167,7 +167,7 @@
     @Test
     public void testSimpleType1() {
         Timestamp timestamp = new Timestamp(123456L);
-        assertEquals("{\"timestamp\":{\"$numberLong\":\"123456\"}}", phase1service.serialiase(timestamp));
+        assertEquals("{\"$numberLong\":\"123456\"}", phase1service.serialiase(timestamp));
     }
 
     @Test