Przeglądaj źródła

Revert "fix unnecessary rounding to float64 precision when JSON-marshaling durations (#453)" (#493)

The change does not handle negative values correctly.

This reverts commit 1e59b77b52bf8e4b449a57e6f79f21226d571845.
Joe Tsai 8 lat temu
rodzic
commit
c65a0412e7
2 zmienionych plików z 2 dodań i 2 usunięć
  1. 2 1
      jsonpb/jsonpb.go
  2. 0 1
      jsonpb/jsonpb_test.go

+ 2 - 1
jsonpb/jsonpb.go

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

+ 0 - 1
jsonpb/jsonpb_test.go

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