Skip to content

Commit 765ba8a

Browse files
authored
Fix non-proto3 JSON format double decoding (#1043)
When the field type is a proto `float` or `double` we always use a Dart `double` for the value. Trying to set the field an `int` causes an assertion failure in `FieldSet`. This code is also a bit inconsistent in itself: when the jspblite2 value is a string it decodes as `double`, but when it's a number it tries to convert to an `int`. So if the value is a string `"1"` it stores as `double` but if it's the number `1` it tries to convert to double. This isn't an issue with dart2js as with dart2js `int` and `double` are the same thing, but it causes assertion failures when this library is used with dart2wasm which distinguishes `int`s and `double`s. Fix by always storing proto `float`s and `double`s as Dart `double`s. Note: this code is currently not used in dart2wasm, instead dart2wasm uses the VM's library. However if I start using this library instead (for benchmarking, to potentially switch to this library) then it breaks. So currently this change is not a user-visible change because dart2wasm doesn't use this library. cl/799924250
1 parent 703bf7c commit 765ba8a

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

protobuf/lib/src/protobuf/json/json_web.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ Object? _convertRawJsValue(
389389
// Allow quoted values, although we don't emit them.
390390
if (value.isA<JSNumber>()) {
391391
final jsNum = value._as<JSNumber>();
392-
return _Number._isInteger(jsNum) ? jsNum.toDartInt : jsNum.toDartDouble;
392+
return jsNum.toDartDouble;
393393
} else if (value.isA<JSString>()) {
394394
return double.parse(value._as<JSString>().toDart);
395395
}

0 commit comments

Comments
 (0)