uber-go/zap

float NaN are encoded as string "NaN"

Offen

#690 geöffnet am 19.03.2019

 (1 Kommentar) (0 Reaktionen) (0 zugewiesene Personen)Go (1.448 Forks)batch import
help wanted

Repository-Metriken

Stars
 (22.391 Sterne)
PR-Merge-Metriken
 (Keine gemergten PRs in 30 T)

Beschreibung

float NaN, +Inf, -Inf are encoded as corresponding string, which is inconsistent with json.Marshal in golang stdlib. json.Marshal will return error if float value can not be encoded in json. For the following code

type C struct {
    Field float64 `json:"field"`
}

func (c C) MarshalLogObject(enc zapcore.ObjectEncoder) error {
    enc.AddFloat64("field", c.Field)
    return nil
}

c := C{
    Field: math.NaN(),
}
logger.Error("reflection", zap.Reflect("c", c))
logger.Error("marshall", zap.Object("c", c))

The two logging statement will output different values

{"msg":"reflection","cError":"json: unsupported value: NaN"}
{"msg":"marshall","c":{"field":"NaN"}}

Output string for float value is undesired, since they are different types.

Contributor Guide