OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** | 5 /** |
6 * Utilities for encoding and decoding JSON (JavaScript Object Notation) data. | 6 * Utilities for encoding and decoding JSON (JavaScript Object Notation) data. |
7 */ | 7 */ |
8 | 8 |
9 library dart.json; | 9 library json; |
10 | 10 |
11 // JSON parsing and serialization. | 11 // JSON parsing and serialization. |
12 | 12 |
13 /** | 13 /** |
14 * Error thrown by JSON serialization if an object cannot be serialized. | 14 * Error thrown by JSON serialization if an object cannot be serialized. |
15 * | 15 * |
16 * The [unsupportedObject] field holds that object that failed to be serialized. | 16 * The [unsupportedObject] field holds that object that failed to be serialized. |
17 * | 17 * |
18 * If an object isn't directly serializable, the serializer calls the 'toJson' | 18 * If an object isn't directly serializable, the serializer calls the 'toJson' |
19 * method on the object. If that call fails, the error will be stored in the | 19 * method on the object. If that call fails, the error will be stored in the |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 * JSON values. | 59 * JSON values. |
60 * | 60 * |
61 * The optional [reviver] function, if provided, is called once for each | 61 * The optional [reviver] function, if provided, is called once for each |
62 * object or list property parsed. The arguments are the property name | 62 * object or list property parsed. The arguments are the property name |
63 * ([String]) or list index ([int]), and the value is the parsed value. | 63 * ([String]) or list index ([int]), and the value is the parsed value. |
64 * The return value of the reviver will be used as the value of that property | 64 * The return value of the reviver will be used as the value of that property |
65 * instead the parsed value. | 65 * instead the parsed value. |
66 * | 66 * |
67 * Throws [FormatException] if the input is not valid JSON text. | 67 * Throws [FormatException] if the input is not valid JSON text. |
68 */ | 68 */ |
69 external parse(String json, [reviver(var key, var value)]); | 69 parse(String json, [reviver(var key, var value)]) { |
70 | |
71 _parse(String json, reviver(var key, var value)) { | |
72 BuildJsonListener listener; | 70 BuildJsonListener listener; |
73 if (reviver == null) { | 71 if (reviver == null) { |
74 listener = new BuildJsonListener(); | 72 listener = new BuildJsonListener(); |
75 } else { | 73 } else { |
76 listener = new ReviverJsonListener(reviver); | 74 listener = new ReviverJsonListener(reviver); |
77 } | 75 } |
78 new JsonParser(json, listener).parse(); | 76 new JsonParser(json, listener).parse(); |
79 return listener.result; | 77 return listener.result; |
80 } | 78 } |
81 | 79 |
(...skipping 739 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
821 first = false; | 819 first = false; |
822 }); | 820 }); |
823 sink.write('}'); | 821 sink.write('}'); |
824 seen.removeLast(); | 822 seen.removeLast(); |
825 return true; | 823 return true; |
826 } else { | 824 } else { |
827 return false; | 825 return false; |
828 } | 826 } |
829 } | 827 } |
830 } | 828 } |
OLD | NEW |