Skip to content

Throw JsonSyntaxException for null map keys in array form#3056

Open
ATKasem wants to merge 1 commit into
google:mainfrom
ATKasem:fix/map-null-key-json-syntax-exception
Open

Throw JsonSyntaxException for null map keys in array form#3056
ATKasem wants to merge 1 commit into
google:mainfrom
ATKasem:fix/map-null-key-json-syntax-exception

Conversation

@ATKasem

@ATKasem ATKasem commented Jul 13, 2026

Copy link
Copy Markdown

Fixes #3037

Summary

  • Reject null map keys in MapTypeAdapterFactory.Adapter.read before insertion
  • Throw JsonSyntaxException instead of letting LinkedTreeMap.put surface an unchecked NullPointerException

Root cause

Array-encoded map entries ([[key, value], ...]) decode a JSON null key to Java null and pass it straight to the backing map. LinkedTreeMap rejects that with NullPointerException: key == null, which escapes Gson's documented JsonParseException error contract.

Test plan

  • mvn -pl gson -Dtest=MapAsArrayTypeAdapterTest test
  • mvn -pl gson -Dtest=MapTest,MapAsArrayTypeAdapterTest test

When deserializing array-encoded map entries, a JSON null key was passed
through to LinkedTreeMap and surfaced as an unchecked NullPointerException.
Reject null keys at the adapter boundary so malformed input stays within
Gson's JsonParseException contract.

Fixes google#3037
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Uncaught NullPointerException ("key == null") when deserializing a JSON object with a null key into a Map

1 participant