# HG changeset patch # User Simon Tooke # Date 1509125127 14400 # Node ID 7f94e7f977bb799589200d99c38af9949706a011 # Parent 9af3b8b15804ef1df3d3d8735ae879e6c4c6f5e9 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 diff -r 9af3b8b15804 -r 7f94e7f977bb common/src/main/java/com/redhat/thermostat/lang/schema/internal/JSONServiceImpl.java --- 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 { + + 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) { diff -r 9af3b8b15804 -r 7f94e7f977bb common/src/test/java/com/redhat/thermostat/lang/schema/internal/JSONServiceTest.java --- 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