luxas / deklarative

A library to work with serialized declarative configuration, e.g. Kubernetes-style YAML in a file
Apache License 2.0
0 stars 0 forks source link

Roundtripping any `int64` through a serialized format (like JSON) should not lose/alter data #3

Open luxas opened 3 years ago

luxas commented 3 years ago

By default, encoding/json decodes all JSON numbers into float64. Due to the nature of how float64's work, it can store integers "only" in the range [-2^53, 2^53] (see https://en.wikipedia.org/wiki/Double-precision_floating-point_format#Precision_limitations_on_integer_values and https://datatracker.ietf.org/doc/html/rfc8259#section-6).

Illustration: https://play.golang.org/p/mpZK8T1NChA

Kubernetes requires round-trips to work for all 64-bit (signed) integers; and has hence built machinery here and here to fix this by first trying to decode the JSON number (at that point represented as a string) first using strconv.ParseInt() and then, only if that fails, strconv.ParseFloat.

The JSON package of deklarative shall have this feature built-into the decoder, and use it as the default mode.

luxas commented 3 years ago

It can be clarified that yaml.v2 and yaml.v3 implement the k8s logic described correctly by default to let integers round-trip successfully.