Repository navigation
fix: Serialize non-finite float values as protobuf JSON strings - #1278
Merged
Merged
Conversation
The proto3 JSON mapping represents NaN, Infinity and -Infinity as the strings "NaN", "Infinity" and "-Infinity", because JSON numbers cannot hold them. toJSON returned the raw numbers instead, which JSON.stringify turns into null, so these values were lost on a round trip through JSON (fromJSON already accepts the string forms). Write double and float fields, and their DoubleValue/FloatValue wrappers, through a floatToJson helper. This covers single, repeated, map and oneof fields. Fixes stephenh#1260
stephenh
approved these changes
Oct 11, 2026
| // fromJSON does not convert the values of maps of wrapper types, so leave fwm out. | ||
| const roundTripped: Floats = { ...message, fwm: {} }; | ||
| const json = JSON.parse(JSON.stringify(Floats.toJSON(roundTripped))); | ||
| expect(Floats.fromJSON(json)).toEqual(roundTripped); |
| } else if (isWholeNumber(valueType) && !(isLong(valueType) && options.forceLong === LongOption.STRING)) { | ||
| return code`Math.round(${from})`; | ||
| } else if (isFloatingPoint(valueType)) { | ||
| return code`${utils.floatToJson}(${from})`; |
Owner
|
Appreciate the PRs @ozenyunus35 ! |
stephenh
pushed a commit
that referenced
this pull request
Oct 11, 2026
Owner
|
🎉 This PR is included in version 2.13.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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.
Summary
Fixes #1260.
Serialize non-finite
floatanddoublevalues according to the protobuf JSON mapping.Previously, generated
toJSONmethods returnedNaN,Infinity, and-Infinityas JavaScript numbers. When passed throughJSON.stringify, these values becamenull, causing data loss during JSON round trips.With this change, non-finite values are serialized as the protobuf-defined strings:
NaN→"NaN"Infinity→"Infinity"-Infinity→"-Infinity"Finite numbers retain their existing representation.
Implementation
float,double,FloatValue, andDoubleValue.floatToJsonhelper that preserves finite and nullish values while converting non-finite numbers to their canonical string representations.toJSONpaths, including singular fields, repeated fields, maps, oneofs, and wrapper values.protoc3.19.1.The generated-code changes account for most of the modified files. A control regeneration using the original source produced no diff, confirming that the regeneration process matches the repository's existing output.
Testing
Added an integration case under
integration/non-finite-floatscovering non-finite serialization across supported field shapes and JSON round trips.Regression verification:
Validation:
Scope
This change addresses
toJSONserialization.A separate pre-existing limitation remains in
fromJSONfor maps containing wrapper values such asmap<string, FloatValue>. Those values are not converted consistently, independently of this change. Fixing that broader wrapper-map parsing behavior is outside the scope of #1260.AI Assistance
Claude Code assisted with implementation, test development, and local validation. The changes and test results were reviewed before submission.