Skip to content

Replace regex number validation in JsonWriter with explicit logic - #3178

Open
eamonnmcmanus wants to merge 2 commits into
google:mainfrom
eamonnmcmanus:number-validation
Open

eamonnmcmanus wants to merge 2 commits into
google:mainfrom
eamonnmcmanus:number-validation

Conversation

@eamonnmcmanus

@eamonnmcmanus eamonnmcmanus commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Replace VALID_JSON_NUMBER_PATTERN.matcher(string).matches() in JsonWriter.value(Number) with a zero-allocation isValidJsonNumber(String) method implementing the same RFC 8259 section 6 number grammar.

In production profiling of Java services using Gson, we observed a very frequent use case of parsing JSON into a JsonElement hierarchy (via JsonParser or Gson.fromJson(..., JsonElement.class)) and later serializing JsonElement values back out to JSON.

When JsonElementTypeAdapter parses a JSON number, it wraps the number string in a LazilyParsedNumber inside a JsonPrimitive. If that JsonElement (or a subtree containing it) is subsequently written out via JsonElementTypeAdapter.write, it calls JsonWriter.value(Number) with that LazilyParsedNumber. Unlike standard JDK types (Integer, Long, BigDecimal, Double, etc.), LazilyParsedNumber cannot be included in alwaysCreatesValidJsonNumber because new LazilyParsedNumber(String) is public and new JsonPrimitive("...").getAsNumber() can wrap an arbitrary string. As a result, every number written from a parsed JsonElement tree (as well as any Number deserialized with the default ToNumberPolicy.LAZILY_PARSED_NUMBER and re-serialized, or any custom Number subclass) hits the validation check in JsonWriter.value(Number).

In production profiles, VALID_JSON_NUMBER_PATTERN.matcher(string).matches() alone accounted for over half of the total CPU time of JsonWriter.value(Number) (~3.6% of all CPU time spent in Gson and its direct JDK calls, with >95% of those calls coming from JsonElementTypeAdapter.write) and ~5% of Gson's short-lived object allocations due to instantiating a java.util.regex.Matcher and its internal int[] group arrays on every number.

Because the RFC 8259 number grammar (-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?) is regular, deterministic, and non-backtracking, replacing the Pattern with a small character-by-character scan in isValidJsonNumber(String) eliminates all Matcher allocations and regex engine dispatch while preserving identical validation behavior.

(We already have a similar explicit DFA in JsonReader.peekNumber but the context is sufficiently different that it is probably not worth trying to share code.)

Replace `VALID_JSON_NUMBER_PATTERN.matcher(string).matches()` in
`JsonWriter.value(Number)` with a zero-allocation `isValidJsonNumber`
method implementing the same RFC 8259 section 6 number grammar.

When JSON is parsed into a `JsonElement` hierarchy and later serialized
back out, numeric values remain `LazilyParsedNumber` instances, which
must be validated on every `JsonWriter.value(Number)` call. Replacing
`java.util.regex.Matcher` avoids allocating a `Matcher` and its internal
group arrays on every number and eliminates regex engine overhead.

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