mirror of
https://github.com/dart-lang/sdk
synced 2024-10-03 01:25:41 +00:00
Support encoding of NaN, infinity and negative infinity.
BUG= R=sigmund@google.com Review URL: https://codereview.chromium.org/2250643002 .
This commit is contained in:
parent
502b6d55ff
commit
3266e73128
|
@ -15,8 +15,13 @@ class JsonSerializationEncoder implements SerializationEncoder {
|
|||
const JsonSerializationEncoder();
|
||||
|
||||
String encode(ObjectValue objectValue) {
|
||||
return new JsonEncoder.withIndent(' ')
|
||||
.convert(const JsonValueEncoder().convert(objectValue));
|
||||
try {
|
||||
return new JsonEncoder.withIndent(' ')
|
||||
.convert(const JsonValueEncoder().convert(objectValue));
|
||||
} on JsonUnsupportedObjectError catch (e) {
|
||||
throw 'Error encoding `${e.unsupportedObject}` '
|
||||
'(${e.unsupportedObject.runtimeType})';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,7 +53,19 @@ class JsonValueEncoder implements ValueVisitor {
|
|||
visitConstant(ConstantValue value, arg) => visit(value.id);
|
||||
|
||||
@override
|
||||
double visitDouble(DoubleValue value, arg) => value.value;
|
||||
visitDouble(DoubleValue value, arg) {
|
||||
double d = value.value;
|
||||
if (d.isNaN) {
|
||||
return 'NaN';
|
||||
} else if (d.isInfinite) {
|
||||
if (d.isNegative) {
|
||||
return '-Infinity';
|
||||
} else {
|
||||
return 'Infinity';
|
||||
}
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
@override
|
||||
visitElement(ElementValue value, arg) => visit(value.id);
|
||||
|
|
|
@ -540,13 +540,21 @@ abstract class AbstractDecoder<K> {
|
|||
/// If no value is associated with [key], then if [isOptional] is `true`,
|
||||
/// [defaultValue] is returned, otherwise an exception is thrown.
|
||||
double getDouble(K key, {bool isOptional: false, double defaultValue}) {
|
||||
double value = _map[_getKeyValue(key)];
|
||||
var value = _map[_getKeyValue(key)];
|
||||
if (value == null) {
|
||||
if (isOptional || defaultValue != null) {
|
||||
return defaultValue;
|
||||
}
|
||||
throw new StateError("double value '$key' not found in $_map.");
|
||||
}
|
||||
// Support alternative encoding of NaN and +/- infinity for JSON.
|
||||
if (value == 'NaN') {
|
||||
return double.NAN;
|
||||
} else if (value == '-Infinity') {
|
||||
return double.NEGATIVE_INFINITY;
|
||||
} else if (value == 'Infinity') {
|
||||
return double.INFINITY;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
|
|
@ -683,6 +683,14 @@ main() => y;
|
|||
}, preserializedSourceFiles: const {
|
||||
'a.dart': '''
|
||||
var x, y = 2;
|
||||
''',
|
||||
}),
|
||||
|
||||
const Test('Double values', const {},
|
||||
preserializedSourceFiles: const {
|
||||
'main.dart': '''
|
||||
const a = 1e+400;
|
||||
main() => a;
|
||||
''',
|
||||
}),
|
||||
];
|
||||
|
|
Loading…
Reference in a new issue