Skip to content

Commit

Permalink
Merge pull request square#515 from EddieRingle/er/gson-drop-unknown-f…
Browse files Browse the repository at this point in the history
…ields

Drop unknown fields when parsing JSON
  • Loading branch information
swankjesse committed Nov 21, 2015
2 parents 0373b9e + 7b1b7cb commit 4e9d2fe
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -44,18 +40,6 @@ class MessageTypeAdapter<M extends Message<M, B>, B extends Message.Builder<M, B

private final Class<M> 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");

Expand Down Expand Up @@ -172,7 +156,7 @@ private void emitUint64(Long value, JsonWriter out) throws IOException {
continue;
}

parseUnknownField(in, builder, Integer.parseInt(name));
in.skipValue();
}

in.endObject();
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 4e9d2fe

Please sign in to comment.