Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(683)

Side by Side Diff: sdk/lib/json/json.dart

Issue 23554004: Made old dart:json library use convert to parse JSON. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addres review comments. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/convert/json.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 dart.json;
10 10
11 import "dart:convert";
12 export "dart:convert" show JsonUnsupportedObjectError, JsonCyclicError;
13
11 // JSON parsing and serialization. 14 // JSON parsing and serialization.
12 15
13 /** 16 /**
14 * Error thrown by JSON serialization if an object cannot be serialized.
15 *
16 * The [unsupportedObject] field holds that object that failed to be serialized.
17 *
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
20 * [cause] field. If the call returns an object that isn't directly
21 * serializable, the [cause] will be null.
22 */
23 class JsonUnsupportedObjectError extends Error {
24 /** The object that could not be serialized. */
25 final unsupportedObject;
26 /** The exception thrown by object's [:toJson:] method, if any. */
27 final cause;
28
29 JsonUnsupportedObjectError(this.unsupportedObject, { this.cause });
30
31 String toString() {
32 if (cause != null) {
33 return "Calling toJson method on object failed.";
34 } else {
35 return "Object toJson method returns non-serializable value.";
36 }
37 }
38 }
39
40
41 /**
42 * Reports that an object could not be stringified due to cyclic references.
43 *
44 * An object that references itself cannot be serialized by [stringify].
45 * When the cycle is detected, a [JsonCyclicError] is thrown.
46 */
47 class JsonCyclicError extends JsonUnsupportedObjectError {
48 /** The first object that was detected as part of a cycle. */
49 JsonCyclicError(Object object): super(object);
50 String toString() => "Cyclic error in JSON stringify";
51 }
52
53
54 /**
55 * Parses [json] and build the corresponding parsed JSON value. 17 * Parses [json] and build the corresponding parsed JSON value.
56 * 18 *
57 * Parsed JSON values are of the types [num], [String], [bool], [Null], 19 * Parsed JSON values are of the types [num], [String], [bool], [Null],
58 * [List]s of parsed JSON values or [Map]s from [String] to parsed 20 * [List]s of parsed JSON values or [Map]s from [String] to parsed
59 * JSON values. 21 * JSON values.
60 * 22 *
61 * The optional [reviver] function, if provided, is called once for each 23 * The optional [reviver] function, if provided, is called once for each
62 * object or list property parsed. The arguments are the property name 24 * object or list property parsed. The arguments are the property name
63 * ([String]) or list index ([int]), and the value is the parsed value. 25 * ([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 26 * The return value of the reviver will be used as the value of that property
65 * instead the parsed value. 27 * instead the parsed value.
66 * 28 *
67 * Throws [FormatException] if the input is not valid JSON text. 29 * Throws [FormatException] if the input is not valid JSON text.
68 */ 30 */
69 external parse(String json, [reviver(var key, var value)]); 31 parse(String json, [reviver(var key, var value)]) {
70 32 return JSON.decode(json, reviver: reviver);
71 _parse(String json, reviver(var key, var value)) {
72 BuildJsonListener listener;
73 if (reviver == null) {
74 listener = new BuildJsonListener();
75 } else {
76 listener = new ReviverJsonListener(reviver);
77 }
78 new JsonParser(json, listener).parse();
79 return listener.result;
80 } 33 }
81 34
82 /** 35 /**
83 * Serializes [object] into a JSON string. 36 * Serializes [object] into a JSON string.
84 * 37 *
85 * Directly serializable values are [num], [String], [bool], and [Null], as well 38 * Directly serializable values are [num], [String], [bool], and [Null], as well
86 * as some [List] and [Map] values. 39 * as some [List] and [Map] values.
87 * For [List], the elements must all be serializable. 40 * For [List], the elements must all be serializable.
88 * For [Map], the keys must be [String] and the values must be serializable. 41 * For [Map], the keys must be [String] and the values must be serializable.
89 * 42 *
(...skipping 731 matching lines...) Expand 10 before | Expand all | Expand 10 after
821 first = false; 774 first = false;
822 }); 775 });
823 sink.write('}'); 776 sink.write('}');
824 seen.removeLast(); 777 seen.removeLast();
825 return true; 778 return true;
826 } else { 779 } else {
827 return false; 780 return false;
828 } 781 }
829 } 782 }
830 } 783 }
OLDNEW
« no previous file with comments | « sdk/lib/convert/json.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698