Skip to content

Commit c65a041

Browse files
authored
Revert "fix unnecessary rounding to float64 precision when JSON-marshaling durations (#453)" (#493)
The change does not handle negative values correctly. This reverts commit 1e59b77.
1 parent 1e59b77 commit c65a041

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

jsonpb/jsonpb.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent, typeU
193193
// "Generated output always contains 3, 6, or 9 fractional digits,
194194
// depending on required precision."
195195
s, ns := s.Field(0).Int(), s.Field(1).Int()
196-
x := fmt.Sprintf("%d.%09d", s, ns)
196+
d := time.Duration(s)*time.Second + time.Duration(ns)*time.Nanosecond
197+
x := fmt.Sprintf("%.9f", d.Seconds())
197198
x = strings.TrimSuffix(x, "000")
198199
x = strings.TrimSuffix(x, "000")
199200
out.write(`"`)

jsonpb/jsonpb_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,6 @@ var marshalingTests = []struct {
407407
{"Any with WKT", marshaler, anyWellKnown, anyWellKnownJSON},
408408
{"Any with WKT and indent", marshalerAllOptions, anyWellKnown, anyWellKnownPrettyJSON},
409409
{"Duration", marshaler, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 3}}, `{"dur":"3.000s"}`},
410-
{"Duration", marshaler, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 100000000, Nanos: 1}}, `{"dur":"100000000.000000001s"}`},
411410
{"Struct", marshaler, &pb.KnownTypes{St: &stpb.Struct{
412411
Fields: map[string]*stpb.Value{
413412
"one": {Kind: &stpb.Value_StringValue{"loneliest number"}},

0 commit comments

Comments
 (0)