Skip to content

Commit

Permalink
ensuring that the type field is written by runtime type adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
inder123 committed Dec 10, 2014
1 parent 7d96ce8 commit fdaa6b0
Showing 1 changed file with 2 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void testSubclassesAutomaticallySerialzed() throws Exception {
json = gson.toJson(shape);
shape = gson.fromJson(json, Shape.class);
assertEquals(15, ((Square)shape).side);
assertEquals(ShapeType.SQUARE, shape.type);
}

@JsonAdapter(Shape.JsonAdapterFactory.class)
Expand Down Expand Up @@ -163,7 +164,7 @@ public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
return new TypeAdapter<R>() {
@Override public R read(JsonReader in) throws IOException {
JsonElement jsonElement = Streams.parse(in);
JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName);
JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName);
if (labelJsonElement == null) {
throw new JsonParseException("cannot deserialize " + baseType
+ " because it does not define a field named " + typeFieldName);
Expand Down

0 comments on commit fdaa6b0

Please sign in to comment.