Skip to content

Commit d994cbc

Browse files
authored
proto3 JSON: Serialize integer floats and doubles as integers (#695)
This is not required by the spec, but it seems to be more consistent with how other libraries in other languages generate JSON. Internally this change was made in cl/405998525.
1 parent 9297b3d commit d994cbc

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

protobuf/lib/src/protobuf/proto3_json.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ Object? _writeToProto3Json(_FieldSet fs, TypeRegistry typeRegistry) {
6767
if (value.isInfinite) {
6868
return value.isNegative ? negativeInfinity : infinity;
6969
}
70+
if (fieldValue.toInt() == fieldValue) {
71+
return fieldValue.toInt();
72+
}
7073
return value;
7174
case PbFieldType._UINT64_BIT:
7275
return (fieldValue as Int64).toStringUnsigned();

protoc_plugin/test/proto3_json_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:convert' show jsonEncode;
56
import 'dart:core' hide Duration;
67

78
import 'package:fixnum/fixnum.dart';
@@ -1253,6 +1254,7 @@ void main() {
12531254
var proto = TestAllTypes()..optionalDouble = 5.0;
12541255
expect(TestAllTypes()..mergeFromProto3Json(json), proto);
12551256
expect(proto.toProto3Json(), json);
1257+
expect(jsonEncode(proto.toProto3Json()), '{"optionalDouble":5}');
12561258
});
12571259

12581260
test('Infinity', () {

0 commit comments

Comments
 (0)