Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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("json"); | 5 #library("json"); |
| 6 | 6 |
| 7 // Pure Dart implementation of JSON protocol. | 7 // Pure Dart implementation of JSON protocol. |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * Utility class to parse JSON and serialize objects to JSON. | 10 * Utility class to parse JSON and serialize objects to JSON. |
| 11 */ | 11 */ |
| 12 class JSON { | 12 class JSON { |
| 13 /** | 13 /** |
| 14 * Parses [:json:] and build the corresponding object. | 14 * Parses [:json:] and build the corresponding object. |
| 15 */ | 15 */ |
| 16 static parse(String json) { | 16 static parse(String json) { |
| 17 return _JsonParser.parse(json); | 17 return _JsonParser.parse(json); |
| 18 } | 18 } |
| 19 | 19 |
| 20 /** | 20 /** |
| 21 * Checks validity of JSON source in [:str:] and returns its text | |
| 22 * length. Returns 0 if [:str:] does not begin with a valid JSON | |
| 23 * object. | |
| 24 */ | |
| 25 static int length(String str) { | |
| 26 return _JsonParser.objectLength(str); | |
| 27 } | |
| 28 | |
| 29 /** | |
| 21 * Serializes [:object:] into JSON string. | 30 * Serializes [:object:] into JSON string. |
| 22 */ | 31 */ |
| 23 static String stringify(Object object) { | 32 static String stringify(Object object) { |
| 24 return JsonStringifier.stringify(object); | 33 return JsonStringifier.stringify(object); |
| 25 } | 34 } |
| 26 } | 35 } |
| 27 | 36 |
| 28 //// Implementation /////////////////////////////////////////////////////////// | 37 //// Implementation /////////////////////////////////////////////////////////// |
| 29 | 38 |
| 30 // TODO(ajohnsen): Introduce when we have a common exception interface for json. | 39 // TODO(ajohnsen): Introduce when we have a common exception interface for json. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 | 98 |
| 90 static final String NULL_STRING = "null"; | 99 static final String NULL_STRING = "null"; |
| 91 static final String TRUE_STRING = "true"; | 100 static final String TRUE_STRING = "true"; |
| 92 static final String FALSE_STRING = "false"; | 101 static final String FALSE_STRING = "false"; |
| 93 | 102 |
| 94 | 103 |
| 95 static parse(String json) { | 104 static parse(String json) { |
| 96 return new _JsonParser._internal(json)._parseToplevel(); | 105 return new _JsonParser._internal(json)._parseToplevel(); |
| 97 } | 106 } |
| 98 | 107 |
| 108 static objectLength(String str) { | |
| 109 var p = new _JsonParser._internal(str); | |
| 110 try { | |
| 111 p._parseObject(); | |
|
Anton Muhin
2012/05/15 15:17:56
I am not sure it's valid implementation: _parseObj
hausner
2012/05/15 15:32:56
Nice catch! Thank you for taking a look. I'll fix
| |
| 112 assert(p.position <= p.length); | |
| 113 return p.position; | |
| 114 } catch (var e) { | |
| 115 return 0; | |
| 116 } | |
| 117 } | |
| 118 | |
| 99 _JsonParser._internal(String json) | 119 _JsonParser._internal(String json) |
| 100 : json = json, | 120 : json = json, |
| 101 length = json.length { | 121 length = json.length { |
| 102 if (tokens !== null) return; | 122 if (tokens !== null) return; |
| 103 | 123 |
| 104 // Use a list as jump-table, faster then switch and if. | 124 // Use a list as jump-table, faster then switch and if. |
| 105 tokens = new List<int>(LAST_ASCII + 1); | 125 tokens = new List<int>(LAST_ASCII + 1); |
| 106 tokens[TAB] = WHITESPACE; | 126 tokens[TAB] = WHITESPACE; |
| 107 tokens[NEW_LINE] = WHITESPACE; | 127 tokens[NEW_LINE] = WHITESPACE; |
| 108 tokens[CARRIAGE_RETURN] = WHITESPACE; | 128 tokens[CARRIAGE_RETURN] = WHITESPACE; |
| (...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 520 }); | 540 }); |
| 521 _sb.add('}'); | 541 _sb.add('}'); |
| 522 _seen.removeLast(); | 542 _seen.removeLast(); |
| 523 return; | 543 return; |
| 524 | 544 |
| 525 default: | 545 default: |
| 526 throw const JsonUnsupportedObjectType(); | 546 throw const JsonUnsupportedObjectType(); |
| 527 } | 547 } |
| 528 } | 548 } |
| 529 } | 549 } |
| OLD | NEW |