Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ public Map<K, V> read(JsonReader in) throws IOException {
while (in.hasNext()) {
in.beginArray(); // entry array
K key = keyTypeAdapter.read(in);
if (key == null) {
throw new JsonSyntaxException("Map key is null");
}
V value = valueTypeAdapter.read(in);
if (map.containsKey(key)) {
throw new JsonSyntaxException("duplicate key: " + key);
Expand All @@ -200,6 +203,9 @@ public Map<K, V> read(JsonReader in) throws IOException {
while (in.hasNext()) {
JsonReaderInternalAccess.INSTANCE.promoteNameToValue(in);
K key = keyTypeAdapter.read(in);
if (key == null) {
throw new JsonSyntaxException("Map key is null");
}
V value = valueTypeAdapter.read(in);
if (map.containsKey(key)) {
throw new JsonSyntaxException("duplicate key: " + key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ public void testDuplicateKeyWithNullFirstValueArrayForm() {
assertThat(e).hasMessageThat().isEqualTo("duplicate key: a");
}

@Test
public void testNullKeyArrayForm() {
Gson gson = new Gson();
Type type = new TypeToken<Map<String, Object>>() {}.getType();
JsonSyntaxException e =
assertThrows(JsonSyntaxException.class, () -> gson.fromJson("[[null,1]]", type));
assertThat(e).hasMessageThat().isEqualTo("Map key is null");
}

@Test
public void testMultipleEnableComplexKeyRegistrationHasNoEffect() {
Type type = new TypeToken<Map<Point, String>>() {}.getType();
Expand Down