fix: encode NaN/Inf floats as null in JSON encoder#1553
Open
toller892 wants to merge 1 commit into
Open
Conversation
The JSON encoder previously encoded NaN, +Inf, and -Inf as quoted
strings ("NaN", "+Inf", "-Inf"), producing invalid JSON. This was
inconsistent with Go's encoding/json, which returns an error for these
values.
Encode these special float values as null instead, which is valid JSON
and the most common representation for non-finite numbers.
Fixes uber-go#690
|
|
shenaba
approved these changes
Jun 3, 2026
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.
Problem
The JSON encoder encodes NaN, +Inf, and -Inf as quoted strings (
"NaN","+Inf","-Inf"), producing invalid JSON. This is inconsistent with Go'sencoding/json, which returns anUnsupportedValueErrorfor these values.This means:
zap.Reflect()with a struct containing NaN returns an error, butenc.AddFloat64()silently produces a string — inconsistent behavior within zap itself."NaN"is a string, not a numeric value.Fix
Encode NaN, +Inf, and -Inf as
nullin the JSON encoder.nullis valid JSON and the most common representation for non-finite numbers in JSON serializers.Changes
zapcore/json_encoder.go: ChangeappendFloat()to emitnullinstead of"NaN"/"+Inf"/"-Inf"zapcore/json_encoder_impl_test.go: Update test expectations for NaN/Inf float64 and float32 casesFixes #690