| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library dart2js.serialization.json; | 5 library dart2js.serialization.json; |
| 6 | 6 |
| 7 import 'dart:convert'; | 7 import 'dart:convert'; |
| 8 | 8 |
| 9 import 'keys.dart'; | 9 import 'keys.dart'; |
| 10 import 'serialization.dart'; | 10 import 'serialization.dart'; |
| 11 import 'values.dart'; | 11 import 'values.dart'; |
| 12 | 12 |
| 13 /// Serialization encoder for JSON. | 13 /// Serialization encoder for JSON. |
| 14 class JsonSerializationEncoder implements SerializationEncoder { | 14 class JsonSerializationEncoder implements SerializationEncoder { |
| 15 const JsonSerializationEncoder(); | 15 const JsonSerializationEncoder(); |
| 16 | 16 |
| 17 String encode(ObjectValue objectValue) { | 17 String encode(ObjectValue objectValue) { |
| 18 return new JsonEncoder.withIndent(' ') | 18 try { |
| 19 .convert(const JsonValueEncoder().convert(objectValue)); | 19 return new JsonEncoder.withIndent(' ') |
| 20 .convert(const JsonValueEncoder().convert(objectValue)); |
| 21 } on JsonUnsupportedObjectError catch (e) { |
| 22 throw 'Error encoding `${e.unsupportedObject}` ' |
| 23 '(${e.unsupportedObject.runtimeType})'; |
| 24 } |
| 20 } | 25 } |
| 21 } | 26 } |
| 22 | 27 |
| 23 /// Serialization decoder for JSON. | 28 /// Serialization decoder for JSON. |
| 24 class JsonSerializationDecoder implements SerializationDecoder { | 29 class JsonSerializationDecoder implements SerializationDecoder { |
| 25 const JsonSerializationDecoder(); | 30 const JsonSerializationDecoder(); |
| 26 | 31 |
| 27 Map decode(String text) => JSON.decode(text); | 32 Map decode(String text) => JSON.decode(text); |
| 28 | 33 |
| 29 /// Returns the name of the [key] which used for to store a [Key] into a | 34 /// Returns the name of the [key] which used for to store a [Key] into a |
| (...skipping 11 matching lines...) Expand all Loading... |
| 41 @override | 46 @override |
| 42 visit(Value value, [arg]) => value.accept(this, arg); | 47 visit(Value value, [arg]) => value.accept(this, arg); |
| 43 | 48 |
| 44 @override | 49 @override |
| 45 bool visitBool(BoolValue value, arg) => value.value; | 50 bool visitBool(BoolValue value, arg) => value.value; |
| 46 | 51 |
| 47 @override | 52 @override |
| 48 visitConstant(ConstantValue value, arg) => visit(value.id); | 53 visitConstant(ConstantValue value, arg) => visit(value.id); |
| 49 | 54 |
| 50 @override | 55 @override |
| 51 double visitDouble(DoubleValue value, arg) => value.value; | 56 visitDouble(DoubleValue value, arg) { |
| 57 double d = value.value; |
| 58 if (d.isNaN) { |
| 59 return 'NaN'; |
| 60 } else if (d.isInfinite) { |
| 61 if (d.isNegative) { |
| 62 return '-Infinity'; |
| 63 } else { |
| 64 return 'Infinity'; |
| 65 } |
| 66 } |
| 67 return d; |
| 68 } |
| 52 | 69 |
| 53 @override | 70 @override |
| 54 visitElement(ElementValue value, arg) => visit(value.id); | 71 visitElement(ElementValue value, arg) => visit(value.id); |
| 55 | 72 |
| 56 @override | 73 @override |
| 57 visitEnum(EnumValue value, arg) => value.value.index; | 74 visitEnum(EnumValue value, arg) => value.value.index; |
| 58 | 75 |
| 59 @override | 76 @override |
| 60 int visitInt(IntValue value, arg) => value.value; | 77 int visitInt(IntValue value, arg) => value.value; |
| 61 | 78 |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 @override | 222 @override |
| 206 void visitType(TypeValue value, String indentation) { | 223 void visitType(TypeValue value, String indentation) { |
| 207 buffer.write('Type(${value.id}):${value.type}'); | 224 buffer.write('Type(${value.id}):${value.type}'); |
| 208 } | 225 } |
| 209 | 226 |
| 210 @override | 227 @override |
| 211 void visitUri(UriValue value, String indentation) { | 228 void visitUri(UriValue value, String indentation) { |
| 212 buffer.write('Uri(${value.value})'); | 229 buffer.write('Uri(${value.value})'); |
| 213 } | 230 } |
| 214 } | 231 } |
| OLD | NEW |