mirror of
https://github.com/fankes/moshi.git
synced 2025-10-18 23:49:21 +08:00
Add the missing primitive converters.
This commit is contained in:
@@ -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> BYTE_JSON_ADAPTER = new
|
||||
IntegerAdapter<Byte>("a byte", Byte.MIN_VALUE, Byte.MAX_VALUE) {
|
||||
@Override protected Byte convert(int value) {
|
||||
return Byte.valueOf((byte) value);
|
||||
}
|
||||
};
|
||||
|
||||
static final JsonAdapter<Double> DOUBLE_JSON_ADAPTER = new JsonAdapter<Double>() {
|
||||
@Override public Double fromJson(JsonReader reader) throws IOException {
|
||||
return reader.nextDouble();
|
||||
@@ -62,6 +76,15 @@ final class StandardJsonAdapters {
|
||||
}
|
||||
};
|
||||
|
||||
static final JsonAdapter<Long> LONG_JSON_ADAPTER = new JsonAdapter<Long>() {
|
||||
@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> STRING_JSON_ADAPTER = new JsonAdapter<String>() {
|
||||
@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<T extends Number> extends JsonAdapter<T> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -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<Byte> 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();
|
||||
|
Reference in New Issue
Block a user