diff --git a/wire-gson-support/src/main/java/com/squareup/wire/MessageTypeAdapter.java b/wire-gson-support/src/main/java/com/squareup/wire/MessageTypeAdapter.java index 728993eaf4..d7f0eea700 100644 --- a/wire-gson-support/src/main/java/com/squareup/wire/MessageTypeAdapter.java +++ b/wire-gson-support/src/main/java/com/squareup/wire/MessageTypeAdapter.java @@ -32,10 +32,6 @@ import okio.Buffer; import okio.ByteString; -import static com.squareup.wire.FieldEncoding.FIXED32; -import static com.squareup.wire.FieldEncoding.FIXED64; -import static com.squareup.wire.FieldEncoding.LENGTH_DELIMITED; -import static com.squareup.wire.FieldEncoding.VARINT; import static com.squareup.wire.WireField.Label; import static java.util.Collections.unmodifiableMap; @@ -44,18 +40,6 @@ class MessageTypeAdapter, B extends Message.Builder messageType; - enum UnknownFieldType { - VARINT, FIXED32, FIXED64, LENGTH_DELIMITED; - - public static UnknownFieldType of(String name) { - if ("varint".equals(name)) return VARINT; - if ("fixed32".equals(name)) return FIXED32; - if ("fixed64".equals(name)) return FIXED64; - if ("length-delimited".equals(name)) return LENGTH_DELIMITED; - throw new IllegalArgumentException("Unknown type " + name); - } - } - // 2^64, used to convert sint64 values >= 2^63 to unsigned decimal form private static final BigInteger POWER_64 = new BigInteger("18446744073709551616"); @@ -172,7 +156,7 @@ private void emitUint64(Long value, JsonWriter out) throws IOException { continue; } - parseUnknownField(in, builder, Integer.parseInt(name)); + in.skipValue(); } in.endObject(); @@ -195,34 +179,6 @@ private Object parseValue(Label label, Type valueType, JsonElement valueElement) } } - private void parseUnknownField(JsonReader in, B builder, int tag) throws IOException { - in.beginArray(); - UnknownFieldType type = UnknownFieldType.of(in.nextString()); - while (in.peek() != JsonToken.END_ARRAY) { - switch (type) { - case VARINT: - long varint = in.nextLong(); - builder.addUnknownField(tag, VARINT, varint); - break; - case FIXED32: - int fixed32 = in.nextInt(); - builder.addUnknownField(tag, FIXED32, fixed32); - break; - case FIXED64: - long fixed64 = in.nextLong(); - builder.addUnknownField(tag, FIXED64, fixed64); - break; - case LENGTH_DELIMITED: - ByteString byteString = ByteString.decodeBase64(in.nextString()); - builder.addUnknownField(tag, LENGTH_DELIMITED, byteString); - break; - default: - throw new AssertionError("Unknown field type " + type); - } - } - in.endArray(); - } - private Object readJson(Type valueType, JsonElement element) { return gson.fromJson(element, valueType); } diff --git a/wire-gson-support/src/test/java/com/squareup/wire/GsonTest.java b/wire-gson-support/src/test/java/com/squareup/wire/GsonTest.java index 5b2388f7ca..df49a03ce5 100644 --- a/wire-gson-support/src/test/java/com/squareup/wire/GsonTest.java +++ b/wire-gson-support/src/test/java/com/squareup/wire/GsonTest.java @@ -277,9 +277,10 @@ public void testGson() { String json = gson.toJson(allTypes); assertThat(json).isEqualTo("{" + JSON_BASE + JSON_UNKNOWN_FIELDS + "}"); + AllTypes allTypesWithKnownFields = allTypes.withoutUnknownFields(); AllTypes parsed = gson.fromJson(json, AllTypes.class); - assertThat(parsed).isEqualTo(allTypes); - assertThat(parsed.toString()).isEqualTo(allTypes.toString()); - assertThat(gson.toJson(parsed)).isEqualTo(gson.toJson(allTypes)); + assertThat(parsed).isEqualTo(allTypesWithKnownFields); + assertThat(parsed.toString()).isEqualTo(allTypesWithKnownFields.toString()); + assertThat(gson.toJson(parsed)).isEqualTo(gson.toJson(allTypesWithKnownFields)); } }