Support incoming nulls in all default type adapters

This commit is contained in:
Jesse Wilson 2011-09-09 05:46:50 +00:00
parent 9424949245
commit f3c0a96f44

View File

@ -38,6 +38,10 @@ public final class TypeAdapters {
public static final TypeAdapter<Boolean> BOOLEAN = new TypeAdapter<Boolean>() { public static final TypeAdapter<Boolean> BOOLEAN = new TypeAdapter<Boolean>() {
public Boolean read(JsonReader reader) throws IOException { public Boolean read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull(); // TODO: does this belong here?
return null;
}
return reader.nextBoolean(); return reader.nextBoolean();
} }
public void write(JsonWriter writer, Boolean value) throws IOException { public void write(JsonWriter writer, Boolean value) throws IOException {
@ -50,6 +54,10 @@ public final class TypeAdapters {
public static final TypeAdapter<Number> BYTE = new TypeAdapter<Number>() { public static final TypeAdapter<Number> BYTE = new TypeAdapter<Number>() {
public Number read(JsonReader reader) throws IOException { public Number read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull(); // TODO: does this belong here?
return null;
}
try { try {
int intValue = reader.nextInt(); int intValue = reader.nextInt();
return (byte) intValue; return (byte) intValue;
@ -67,6 +75,10 @@ public final class TypeAdapters {
public static final TypeAdapter<Number> SHORT = new TypeAdapter<Number>() { public static final TypeAdapter<Number> SHORT = new TypeAdapter<Number>() {
public Number read(JsonReader reader) throws IOException { public Number read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull(); // TODO: does this belong here?
return null;
}
try { try {
return (short) reader.nextInt(); return (short) reader.nextInt();
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
@ -83,6 +95,10 @@ public final class TypeAdapters {
public static final TypeAdapter<Number> INTEGER = new TypeAdapter<Number>() { public static final TypeAdapter<Number> INTEGER = new TypeAdapter<Number>() {
public Number read(JsonReader reader) throws IOException { public Number read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull(); // TODO: does this belong here?
return null;
}
try { try {
return reader.nextInt(); return reader.nextInt();
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
@ -99,6 +115,10 @@ public final class TypeAdapters {
public static final TypeAdapter<Number> LONG = new TypeAdapter<Number>() { public static final TypeAdapter<Number> LONG = new TypeAdapter<Number>() {
public Number read(JsonReader reader) throws IOException { public Number read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull(); // TODO: does this belong here?
return null;
}
try { try {
return reader.nextLong(); return reader.nextLong();
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
@ -115,6 +135,10 @@ public final class TypeAdapters {
public static final TypeAdapter<Number> FLOAT = new TypeAdapter<Number>() { public static final TypeAdapter<Number> FLOAT = new TypeAdapter<Number>() {
public Number read(JsonReader reader) throws IOException { public Number read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull(); // TODO: does this belong here?
return null;
}
return (float) reader.nextDouble(); return (float) reader.nextDouble();
} }
public void write(JsonWriter writer, Number value) throws IOException { public void write(JsonWriter writer, Number value) throws IOException {
@ -127,6 +151,10 @@ public final class TypeAdapters {
public static final TypeAdapter<Number> DOUBLE = new TypeAdapter<Number>() { public static final TypeAdapter<Number> DOUBLE = new TypeAdapter<Number>() {
public Number read(JsonReader reader) throws IOException { public Number read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull(); // TODO: does this belong here?
return null;
}
return reader.nextDouble(); return reader.nextDouble();
} }
public void write(JsonWriter writer, Number value) throws IOException { public void write(JsonWriter writer, Number value) throws IOException {
@ -154,6 +182,10 @@ public final class TypeAdapters {
public static final TypeAdapter<StringBuilder> STRING_BUILDER = new TypeAdapter<StringBuilder>() { public static final TypeAdapter<StringBuilder> STRING_BUILDER = new TypeAdapter<StringBuilder>() {
public StringBuilder read(JsonReader reader) throws IOException { public StringBuilder read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull(); // TODO: does this belong here?
return null;
}
return new StringBuilder(reader.nextString()); return new StringBuilder(reader.nextString());
} }
public void write(JsonWriter writer, StringBuilder value) throws IOException { public void write(JsonWriter writer, StringBuilder value) throws IOException {
@ -166,6 +198,10 @@ public final class TypeAdapters {
public static final TypeAdapter<StringBuffer> STRING_BUFFER = new TypeAdapter<StringBuffer>() { public static final TypeAdapter<StringBuffer> STRING_BUFFER = new TypeAdapter<StringBuffer>() {
public StringBuffer read(JsonReader reader) throws IOException { public StringBuffer read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull(); // TODO: does this belong here?
return null;
}
return new StringBuffer(reader.nextString()); return new StringBuffer(reader.nextString());
} }
public void write(JsonWriter writer, StringBuffer value) throws IOException { public void write(JsonWriter writer, StringBuffer value) throws IOException {
@ -178,6 +214,10 @@ public final class TypeAdapters {
public static final TypeAdapter<URL> URL = new TypeAdapter<URL>() { public static final TypeAdapter<URL> URL = new TypeAdapter<URL>() {
public URL read(JsonReader reader) throws IOException { public URL read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull(); // TODO: does this belong here?
return null;
}
String nextString = reader.nextString(); String nextString = reader.nextString();
return "null".equals(nextString) ? null : new URL(nextString); return "null".equals(nextString) ? null : new URL(nextString);
} }
@ -190,6 +230,10 @@ public final class TypeAdapters {
public static final TypeAdapter<URI> URI = new TypeAdapter<URI>() { public static final TypeAdapter<URI> URI = new TypeAdapter<URI>() {
public URI read(JsonReader reader) throws IOException { public URI read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull(); // TODO: does this belong here?
return null;
}
try { try {
String nextString = reader.nextString(); String nextString = reader.nextString();
return "null".equals(nextString) ? null : new URI(nextString); return "null".equals(nextString) ? null : new URI(nextString);
@ -206,6 +250,10 @@ public final class TypeAdapters {
public static final TypeAdapter<InetAddress> INET_ADDRESS = new TypeAdapter<InetAddress>() { public static final TypeAdapter<InetAddress> INET_ADDRESS = new TypeAdapter<InetAddress>() {
public InetAddress read(JsonReader reader) throws IOException { public InetAddress read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull(); // TODO: does this belong here?
return null;
}
return InetAddress.getByName(reader.nextString()); return InetAddress.getByName(reader.nextString());
} }
public void write(JsonWriter writer, InetAddress value) throws IOException { public void write(JsonWriter writer, InetAddress value) throws IOException {
@ -218,6 +266,10 @@ public final class TypeAdapters {
public static final TypeAdapter<UUID> UUID = new TypeAdapter<UUID>() { public static final TypeAdapter<UUID> UUID = new TypeAdapter<UUID>() {
public UUID read(JsonReader reader) throws IOException { public UUID read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull(); // TODO: does this belong here?
return null;
}
return java.util.UUID.fromString(reader.nextString()); return java.util.UUID.fromString(reader.nextString());
} }
public void write(JsonWriter writer, UUID value) throws IOException { public void write(JsonWriter writer, UUID value) throws IOException {
@ -229,6 +281,10 @@ public final class TypeAdapters {
public static final TypeAdapter<Locale> LOCALE = new TypeAdapter<Locale>() { public static final TypeAdapter<Locale> LOCALE = new TypeAdapter<Locale>() {
public Locale read(JsonReader reader) throws IOException { public Locale read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull(); // TODO: does this belong here?
return null;
}
String locale = reader.nextString(); String locale = reader.nextString();
StringTokenizer tokenizer = new StringTokenizer(locale, "_"); StringTokenizer tokenizer = new StringTokenizer(locale, "_");
String language = null; String language = null;