Skip to content

Reject empty documents in Strictness.STRICT - #3207

Open
gsonntag wants to merge 1 commit into
google:mainfrom
gsonntag:strict-reject-empty-document
Open

gsonntag wants to merge 1 commit into
google:mainfrom
gsonntag:strict-reject-empty-document

Conversation

@gsonntag

@gsonntag gsonntag commented Dec 5, 2026

Copy link
Copy Markdown

Purpose

Relates to #3169.

Description

I ran the RFC 8259 cases from the benchmark in #3169 with Strictness.STRICT (and strict UTF-8 decoding). Four cases that should fail still pass:

  • json-8259-empty ("")
  • jts-8259-n_structure_no_data ("")
  • jts-8259-n_single_space (" ")
  • jts-8259-n_structure_UTF8_BOM_no_data ("\uFEFF")

JsonReader already throws EOFException for these, but Gson.fromJson and JsonParser.parseReader catch it and return null for compatibility with Gson 1.5. This PR keeps that fallback for every mode except STRICT. Mode STRICT will now throw JsonSyntaxException.

Behavior change for truncated mid-document reads in STRICT mode: they would previously return null since isEmpty was true when reader.peek() threw EOFException. Now they throw JsonSyntaxException.

This will break anyone on STRICT who relies on an empty body turning into null.

Checklist

  • New code follows the Google Java Style Guide
  • New unit tests have been added, using Truth, including cases which failed previously
  • mvn clean verify javadoc:jar passes without errors

Relates to google#3169.

Restrict the legacy EOFException fallback in Gson.fromJson(JsonReader, TypeToken) and Streams.parse(JsonReader) to reader.getStrictness() != Strictness.STRICT.

Behavior changes when effective strictness is Strictness.STRICT:
- Empty, whitespace-only, and BOM-only documents throw JsonSyntaxException instead of returning null / JsonNull.INSTANCE (RFC 8259 §2).
- Truncated mid-document input passed to Gson.fromJson(JsonReader, ...) or JsonParser.parseReader(JsonReader) throws JsonSyntaxException instead of returning null / JsonNull.INSTANCE.
- TreeTypeAdapter (JsonDeserializer) throws JsonSyntaxException on empty input when read with a STRICT JsonReader.

Default Gson, LEGACY_STRICT, LENIENT, and Gson.fromJson((String) null, ...) are unchanged.

This branch has not been deployed

No deployments
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.

1 participant