fdeantoni / prost-wkt

Prost Well-Known-Types serialization and deserialization.
Apache License 2.0
76 stars 35 forks source link

Duration serialization is non-canonical (according to protobuf JSON mapping specification) #60

Closed chrnorm closed 6 months ago

chrnorm commented 8 months ago

From the protobuf JSON Mapping specification, a Duration should be serialized as follows:

Generated output always contains 0, 3, 6, or 9 fractional digits, depending on required precision, followed by the suffix "s". Accepted are any fractional digits (also none) as long as they fit into nano-seconds precision and the suffix "s" is required.

Examples of this are "1.000340012s", and "1s".

Currently this library serializes Durations as an object, like so:

 { "seconds": 2, "nanos": 406273834 }

The specific issue I've run into here is that I am trying to use protojson in Go to parse a JSON protobuf generated from Rust. protojson expects the canonical encoding of durations (like "1s"), so my generated JSON is incompatible.