Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for double.infinity and double.nan in json serializers #652

Merged
merged 2 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions protobuf/lib/protobuf.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export 'src/protobuf/type_registry.dart' show TypeRegistry;
part 'src/protobuf/coded_buffer.dart';
part 'src/protobuf/coded_buffer_reader.dart';
part 'src/protobuf/coded_buffer_writer.dart';
part 'src/protobuf/consts.dart';
part 'src/protobuf/builder_info.dart';
part 'src/protobuf/event_plugin.dart';
part 'src/protobuf/exceptions.dart';
Expand Down
17 changes: 17 additions & 0 deletions protobuf/lib/src/protobuf/consts.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of protobuf;

/// Constant string value of double.infinity.toString() and the infinity value
/// recognized by double.parse(..).
const infinity = 'Infinity';

/// Constant string value of double.negativeInfinity.toString() and the negative
/// infinity value recognized by double.parse(..).
const negativeInfinity = '-Infinity';

/// Constant string value of double.nan.toString() and the NaN (not a number)
/// value recognized by double.parse(..).
const nan = 'NaN';
15 changes: 13 additions & 2 deletions protobuf/lib/src/protobuf/json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,25 @@ Map<String, dynamic> _writeToJsonMap(_FieldSet fs) {
switch (baseType) {
case PbFieldType._BOOL_BIT:
case PbFieldType._STRING_BIT:
case PbFieldType._FLOAT_BIT:
case PbFieldType._DOUBLE_BIT:
case PbFieldType._INT32_BIT:
case PbFieldType._SINT32_BIT:
case PbFieldType._UINT32_BIT:
case PbFieldType._FIXED32_BIT:
case PbFieldType._SFIXED32_BIT:
return fieldValue;
case PbFieldType._FLOAT_BIT:
case PbFieldType._DOUBLE_BIT:
final value = fieldValue as double;
if (value.isNaN) {
return nan;
}
if (value.isInfinite) {
return value.isNegative ? negativeInfinity : infinity;
}
if (fieldValue.toInt() == fieldValue) {
return fieldValue.toInt();
}
return value;
case PbFieldType._BYTES_BIT:
// Encode 'bytes' as a base64-encoded string.
return base64Encode(fieldValue as List<int>);
Expand Down
10 changes: 4 additions & 6 deletions protobuf/lib/src/protobuf/proto3_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,11 @@ Object? _writeToProto3Json(_FieldSet fs, TypeRegistry typeRegistry) {
case PbFieldType._FLOAT_BIT:
case PbFieldType._DOUBLE_BIT:
double value = fieldValue;
if (value.isNaN) return 'NaN';
if (value.isNaN) {
return nan;
}
if (value.isInfinite) {
if (value.isNegative) {
return '-Infinity';
} else {
return 'Infinity';
}
return value.isNegative ? negativeInfinity : infinity;
}
return value;
case PbFieldType._UINT64_BIT:
Expand Down
2 changes: 1 addition & 1 deletion protobuf/test/json_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void main() {
expect(decoded.int64, value);
});

test('tesFrozentInt64JsonEncoding', () {
test('testFrozentInt64JsonEncoding', () {
final value = Int64.parseInt('1234567890123456789');
final frozen = T()
..int64 = value
Expand Down
39 changes: 35 additions & 4 deletions protoc_plugin/test/json_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ import 'test_util.dart';

void main() {
final testAllJsonTypes = '{"1":101,"2":"102","3":103,"4":"104",'
'"5":105,"6":"106","7":107,"8":"108","9":109,"10":"110","11":111.0,'
'"12":112.0,"13":true,"14":"115","15":"MTE2","16":{"17":117},'
'"5":105,"6":"106","7":107,"8":"108","9":109,"10":"110","11":111,'
'"12":112,"13":true,"14":"115","15":"MTE2","16":{"17":117},'
'"18":{"1":118},"19":{"1":119},"20":{"1":120},"21":3,"22":6,"23":9,'
'"24":"124","25":"125","31":[201,301],"32":["202","302"],'
'"33":[203,303],"34":["204","304"],"35":[205,305],"36":["206","306"],'
'"37":[207,307],"38":["208","308"],"39":[209,309],"40":["210","310"],'
'"41":[211.0,311.0],"42":[212.0,312.0],"43":[true,false],'
'"41":[211,311],"42":[212,312],"43":[true,false],'
'"44":["215","315"],"45":["MjE2","MzE2"],"46":[{"47":217},{"47":317}],'
'"48":[{"1":218},{"1":318}],"49":[{"1":219},{"1":319}],'
'"50":[{"1":220},{"1":320}],"51":[2,3],"52":[5,6],"53":[8,9],'
'"54":["224","324"],"55":["225","325"],"61":401,"62":"402","63":403,'
'"64":"404","65":405,"66":"406","67":407,"68":"408","69":409,'
'"70":"410","71":411.0,"72":412.0,"73":false,"74":"415","75":"NDE2",'
'"70":"410","71":411,"72":412,"73":false,"74":"415","75":"NDE2",'
'"81":1,"82":4,"83":7,"84":"424","85":"425"}';

// Checks that message once serialized to JSON
Expand Down Expand Up @@ -128,6 +128,37 @@ void main() {
expect(parsed, expected);
});

group('testConvertDouble', () {
test('WithDecimal', () {
final json = '{"12":1.2}';
TestAllTypes proto = TestAllTypes()..optionalDouble = 1.2;
expect(TestAllTypes.fromJson(json), proto);
expect(proto.writeToJson(), json);
});

test('WholeNumber', () {
final json = '{"12":5}';
TestAllTypes proto = TestAllTypes()..optionalDouble = 5.0;
expect(TestAllTypes.fromJson(json), proto);
expect(proto.writeToJson(), json);
});

test('Infinity', () {
final json = '{"12":"Infinity"}';
TestAllTypes proto = TestAllTypes()..optionalDouble = double.infinity;
expect(TestAllTypes.fromJson(json), proto);
expect(proto.writeToJson(), json);
});

test('NegativeInfinity', () {
final json = '{"12":"-Infinity"}';
TestAllTypes proto = TestAllTypes()
..optionalDouble = double.negativeInfinity;
expect(TestAllTypes.fromJson(json), proto);
expect(proto.writeToJson(), json);
});
});

test('testParseUnsignedLegacy', () {
var parsed = TestAllTypes.fromJson(
'{"4":"-1152921500311945216","8":"-1152921500311945215"}');
Expand Down