fix: accept NNBSP before AM/PM in default Date parsing - #3091
Open
arimu1 wants to merge 1 commit into
Open
Conversation
JDK 21+ CLDR uses U+202F (narrow no-break space) before AM/PM in DateFormat output, while older JDKs use a regular space. Default Date deserialization now retries with both space variants so dates remain interoperable across JDK versions. Fixes google#2689
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
Fixes #2689 — default
Datedeserialization fails when the date string uses a different space character before AM/PM than the current JDK'sDateFormatexpects.Starting with the CLDR 42 update in JDK 21 (
JDK-8284840),DateFormat.getDateTimeInstancefor some locales emits U+202F (narrow no-break space) before AM/PM instead of U+0020. Dates written on one JDK version therefore often fail to parse on another when using Gson's defaultDateadapter (DefaultDateTypeAdapter, formerlyDateTypeAdapter).Description
In
DefaultDateTypeAdapter.deserializeToDate, ifDateFormat.parsefails for the original string, retry with alternate space variants before the AM/PM marker:This keeps default
Dateadapters interoperable across JDK 8–21+ without changing the serialization format (still whatever the running JDK'sDateFormatproduces). ISO-8601 fallback is unchanged; the failure was never inISO8601Utilsitself but in theDateFormatattempts that precede it.Related discussion: #2450 (test fragility around the same space character), #2472 (possible future default ISO-8601 date format).
Checklist
Checked with
mvn -pl gson spotless:check.nullN/A — no new public API.
N/A
@since $next-version$N/A
TestCase)DefaultDateTypeAdapterTest#testParsingAcceptsNarrowNoBreakSpaceBeforeAmPmcovers regular space, U+202F, and U+00A0 for AM and PM.mvn clean verify javadoc:jarpasses without errorsFocused:
mvn -pl gson -Dtest=DefaultDateTypeAdapterTest test→ 12/12 (Temurin 21). Also manually verified issue JDK21 toJson failure #2689 payloads parse on JDK 21 and JDK 17 after the fix.