Skip to content

Commit

Permalink
Nice documentation for TypeAdapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
swankjesse committed Dec 3, 2011
1 parent 8ee2c24 commit f602bce
Show file tree
Hide file tree
Showing 21 changed files with 671 additions and 384 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
}

return new TypeAdapter<T>() {
@Override public T read(JsonReader reader) throws IOException {
JsonElement jsonElement = Streams.parse(reader);
@Override public T read(JsonReader in) throws IOException {
JsonElement jsonElement = Streams.parse(in);
JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName);
if (labelJsonElement == null) {
throw new JsonParseException("cannot deserialize " + baseType
Expand All @@ -212,7 +212,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
return delegate.fromJsonTree(jsonElement);
}

@Override public void write(JsonWriter writer, T value) throws IOException {
@Override public void write(JsonWriter out, T value) throws IOException {
Class<?> srcType = value.getClass();
String label = subtypeToLabel.get(srcType);
@SuppressWarnings("unchecked") // registration requires that subtype extends T
Expand All @@ -231,7 +231,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
clone.add(e.getKey(), e.getValue());
}
Streams.write(clone, writer);
Streams.write(clone, out);
}
};
}
Expand Down
50 changes: 25 additions & 25 deletions gson/src/main/java/com/google/gson/Gson.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,21 +251,21 @@ private TypeAdapter<Number> doubleAdapter(boolean serializeSpecialFloatingPointV
return TypeAdapters.DOUBLE;
}
return new TypeAdapter<Number>() {
@Override public Double read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
@Override public Double read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
return reader.nextDouble();
return in.nextDouble();
}
@Override public void write(JsonWriter writer, Number value) throws IOException {
@Override public void write(JsonWriter out, Number value) throws IOException {
if (value == null) {
writer.nullValue();
out.nullValue();
return;
}
double doubleValue = value.doubleValue();
checkValidFloatingPoint(doubleValue);
writer.value(value);
out.value(value);
}
};
}
Expand All @@ -275,21 +275,21 @@ private TypeAdapter<Number> floatAdapter(boolean serializeSpecialFloatingPointVa
return TypeAdapters.FLOAT;
}
return new TypeAdapter<Number>() {
@Override public Float read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
@Override public Float read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
return (float) reader.nextDouble();
return (float) in.nextDouble();
}
@Override public void write(JsonWriter writer, Number value) throws IOException {
@Override public void write(JsonWriter out, Number value) throws IOException {
if (value == null) {
writer.nullValue();
out.nullValue();
return;
}
float floatValue = value.floatValue();
checkValidFloatingPoint(floatValue);
writer.value(value);
out.value(value);
}
};
}
Expand All @@ -307,19 +307,19 @@ private TypeAdapter<Number> longAdapter(LongSerializationPolicy longSerializatio
return TypeAdapters.LONG;
}
return new TypeAdapter<Number>() {
@Override public Number read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
@Override public Number read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
return reader.nextLong();
return in.nextLong();
}
@Override public void write(JsonWriter writer, Number value) throws IOException {
@Override public void write(JsonWriter out, Number value) throws IOException {
if (value == null) {
writer.nullValue();
out.nullValue();
return;
}
writer.value(value.toString());
out.value(value.toString());
}
};
}
Expand Down Expand Up @@ -828,18 +828,18 @@ public void setDelegate(TypeAdapter<T> typeAdapter) {
delegate = typeAdapter;
}

@Override public T read(JsonReader reader) throws IOException {
@Override public T read(JsonReader in) throws IOException {
if (delegate == null) {
throw new IllegalStateException();
}
return delegate.read(reader);
return delegate.read(in);
}

@Override public void write(JsonWriter writer, T value) throws IOException {
@Override public void write(JsonWriter out, T value) throws IOException {
if (delegate == null) {
throw new IllegalStateException();
}
delegate.write(writer, value);
delegate.write(out, value);
}
}

Expand Down
3 changes: 3 additions & 0 deletions gson/src/main/java/com/google/gson/JsonDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
* Gson gson = new GsonBuilder().registerTypeAdapter(Id.class, new IdDeserializer()).create();
* </pre>
*
* <p>New applications should prefer {@link TypeAdapter}, whose streaming API
* is more efficient than this interface's tree API.
*
* @author Inderjeet Singh
* @author Joel Leitch
*
Expand Down
3 changes: 3 additions & 0 deletions gson/src/main/java/com/google/gson/JsonSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
* Gson gson = new GsonBuilder().registerTypeAdapter(Id.class, new IdSerializer()).create();
* </pre>
*
* <p>New applications should prefer {@link TypeAdapter}, whose streaming API
* is more efficient than this interface's tree API.
*
* @author Inderjeet Singh
* @author Joel Leitch
*
Expand Down
14 changes: 7 additions & 7 deletions gson/src/main/java/com/google/gson/TreeTypeAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,28 @@ private TreeTypeAdapter(JsonSerializer<T> serializer, JsonDeserializer<T> deseri
this.skipPast = skipPast;
}

@Override public T read(JsonReader reader) throws IOException {
@Override public T read(JsonReader in) throws IOException {
if (deserializer == null) {
return delegate().read(reader);
return delegate().read(in);
}
JsonElement value = Streams.parse(reader);
JsonElement value = Streams.parse(in);
if (value.isJsonNull()) {
return null;
}
return deserializer.deserialize(value, typeToken.getType(), gson.deserializationContext);
}

@Override public void write(JsonWriter writer, T value) throws IOException {
@Override public void write(JsonWriter out, T value) throws IOException {
if (serializer == null) {
delegate().write(writer, value);
delegate().write(out, value);
return;
}
if (value == null) {
writer.nullValue();
out.nullValue();
return;
}
JsonElement tree = serializer.serialize(value, typeToken.getType(), gson.serializationContext);
Streams.write(tree,writer);
Streams.write(tree, out);
}

private TypeAdapter<T> delegate() {
Expand Down
Loading

0 comments on commit f602bce

Please sign in to comment.