From 806767169b73111b38d73903a75a8b43b18f746c Mon Sep 17 00:00:00 2001 From: Scott Blum Date: Tue, 12 Aug 2014 19:14:06 -0400 Subject: [PATCH] Add the missing primitive converters. --- .../squareup/moshi/StandardJsonAdapters.java | 56 +++++++++++++++++++ .../java/com/squareup/moshi/MoshiTest.java | 28 ++++++++++ 2 files changed, 84 insertions(+) diff --git a/moshi/src/main/java/com/squareup/moshi/StandardJsonAdapters.java b/moshi/src/main/java/com/squareup/moshi/StandardJsonAdapters.java index 2efd3e6..64dad04 100644 --- a/moshi/src/main/java/com/squareup/moshi/StandardJsonAdapters.java +++ b/moshi/src/main/java/com/squareup/moshi/StandardJsonAdapters.java @@ -25,11 +25,18 @@ final class StandardJsonAdapters { Type type, AnnotatedElement annotations, Moshi moshi) { // TODO: support all 8 primitive types. if (type == boolean.class) return BOOLEAN_JSON_ADAPTER; + if (type == byte.class) return BYTE_JSON_ADAPTER; + if (type == char.class) return null; if (type == double.class) return DOUBLE_JSON_ADAPTER; + if (type == float.class) return null; if (type == int.class) return INTEGER_JSON_ADAPTER; + if (type == long.class) return LONG_JSON_ADAPTER; + if (type == short.class) return null; if (type == Boolean.class) return BOOLEAN_JSON_ADAPTER.nullSafe(); + if (type == byte.class) return BYTE_JSON_ADAPTER.nullSafe(); if (type == Double.class) return DOUBLE_JSON_ADAPTER.nullSafe(); if (type == Integer.class) return INTEGER_JSON_ADAPTER.nullSafe(); + if (type == Long.class) return LONG_JSON_ADAPTER.nullSafe(); if (type == String.class) return STRING_JSON_ADAPTER.nullSafe(); return null; } @@ -44,6 +51,13 @@ final class StandardJsonAdapters { } }; + static final JsonAdapter BYTE_JSON_ADAPTER = new + IntegerAdapter("a byte", Byte.MIN_VALUE, Byte.MAX_VALUE) { + @Override protected Byte convert(int value) { + return Byte.valueOf((byte) value); + } + }; + static final JsonAdapter DOUBLE_JSON_ADAPTER = new JsonAdapter() { @Override public Double fromJson(JsonReader reader) throws IOException { return reader.nextDouble(); @@ -62,6 +76,15 @@ final class StandardJsonAdapters { } }; + static final JsonAdapter LONG_JSON_ADAPTER = new JsonAdapter() { + @Override public Long fromJson(JsonReader reader) throws IOException { + return reader.nextLong(); + } + @Override public void toJson(JsonWriter writer, Long value) throws IOException { + writer.value(value.longValue()); + } + }; + static final JsonAdapter STRING_JSON_ADAPTER = new JsonAdapter() { @Override public String fromJson(JsonReader reader) throws IOException { return reader.nextString(); @@ -70,4 +93,37 @@ final class StandardJsonAdapters { writer.value(value); } }; + + private abstract static class IntegerAdapter extends JsonAdapter { + private final String typeMessage; + private final int min; + private final int max; + + IntegerAdapter(String typeMessage, int min, int max) { + this.typeMessage = typeMessage; + this.min = min; + this.max = max; + } + + @Override public T fromJson(JsonReader reader) throws IOException { + int value = reader.nextInt(); + if (value < min || value > max) { + throw new NumberFormatException("Expected " + + typeMessage + + " but was " + + value + + " at path " + + reader.getPath()); + } + + return convert(value); + } + + protected abstract T convert(int value); + + @Override public void toJson(JsonWriter writer, T value) throws IOException { + writer.value(value.intValue()); + } + } + } diff --git a/moshi/src/test/java/com/squareup/moshi/MoshiTest.java b/moshi/src/test/java/com/squareup/moshi/MoshiTest.java index cd59071..3dd5ab8 100644 --- a/moshi/src/test/java/com/squareup/moshi/MoshiTest.java +++ b/moshi/src/test/java/com/squareup/moshi/MoshiTest.java @@ -34,6 +34,34 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; public final class MoshiTest { + /** No nulls for byte.class. */ + @Test public void byteAdapter() throws Exception { + Moshi moshi = new Moshi.Builder().build(); + JsonAdapter adapter = moshi.adapter(byte.class).lenient(); + assertThat(adapter.fromJson("1")).isEqualTo((byte) 1); + assertThat(adapter.toJson((byte) 2)).isEqualTo("2"); + + try { + adapter.fromJson("200"); + fail(); + } catch (NumberFormatException expected) { + assertThat(expected.getMessage()).isEqualTo("Expected a byte but was 200 at path $"); + } + + try { + adapter.fromJson("null"); + fail(); + } catch (IllegalStateException expected) { + assertThat(expected.getMessage()).isEqualTo("Expected an int but was NULL at path $"); + } + + try { + moshi.adapter(int.class).toJson(null); + fail(); + } catch (NullPointerException expected) { + } + } + /** No nulls for int.class. */ @Test public void intAdapter() throws Exception { Moshi moshi = new Moshi.Builder().build();