Fix double.infinity serialization (#40099)

This commit is contained in:
LongCatIsLooong 2019-09-09 18:30:12 -07:00 committed by GitHub
parent 477ae6b8ce
commit 663d0b13bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -2669,7 +2669,11 @@ class DiagnosticsProperty<T> extends DiagnosticsNode {
json['defaultLevel'] = describeEnum(_defaultLevel);
if (value is Diagnosticable || value is DiagnosticsNode)
json['isDiagnosticableValue'] = true;
if (value is num || value is String || value is bool || value == null)
if (v is num)
// Workaround for https://github.com/flutter/flutter/issues/39937#issuecomment-529558033.
// JSON.stringify replaces infinity and NaN with null.
json['value'] = v.isFinite ? v : v.toString();
if (value is String || value is bool || value == null)
json['value'] = value;
return json;
}

View File

@ -1050,6 +1050,15 @@ void main() {
validateDoublePropertyJsonSerialization(doubleWithUnit);
});
test('double.infinity serialization test', () {
final DoubleProperty infProperty1 = DoubleProperty('double1', double.infinity);
validateDoublePropertyJsonSerialization(infProperty1);
expect(infProperty1.toString(), equals('double1: Infinity'));
final DoubleProperty infProperty2 = DoubleProperty('double2', double.negativeInfinity);
validateDoublePropertyJsonSerialization(infProperty2);
expect(infProperty2.toString(), equals('double2: -Infinity'));
});
test('unsafe double property test', () {
final DoubleProperty safe = DoubleProperty.lazy(