Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct complex encoding in json encoder (#995) #1001

Merged
merged 1 commit into from Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion zapcore/json_encoder.go
Expand Up @@ -228,7 +228,11 @@ func (enc *jsonEncoder) AppendComplex128(val complex128) {
// Because we're always in a quoted string, we can use strconv without
// special-casing NaN and +/-Inf.
enc.buf.AppendFloat(r, 64)
enc.buf.AppendByte('+')
// If imaginary part is less than 0, minus (-) sign is added by default
// by AppendFloat.
if i >= 0 {
enc.buf.AppendByte('+')
}
enc.buf.AppendFloat(i, 64)
enc.buf.AppendByte('i')
enc.buf.AppendByte('"')
Expand Down
2 changes: 2 additions & 0 deletions zapcore/json_encoder_test.go
Expand Up @@ -64,6 +64,7 @@ func TestJSONEncodeEntry(t *testing.T) {
"so": "passes",
"answer": 42,
"common_pie": 3.14,
"complex_value": "3.14-2.71i",
"null_value": null,
"array_with_null_elements": [{}, null, null, 2],
"such": {
Expand All @@ -86,6 +87,7 @@ func TestJSONEncodeEntry(t *testing.T) {
zap.String("so", "passes"),
zap.Int("answer", 42),
zap.Float64("common_pie", 3.14),
zap.Complex128("complex_value", 3.14-2.71i),
// Cover special-cased handling of nil in AddReflect() and
// AppendReflect(). Note that for the latter, we explicitly test
// correct results for both the nil static interface{} value
Expand Down