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. |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 static int length(String str) { | 25 static int length(String str) { |
| 26 return _JsonParser.objectLength(str); | 26 return _JsonParser.objectLength(str); |
| 27 } | 27 } |
| 28 | 28 |
| 29 /** | 29 /** |
| 30 * Serializes [:object:] into JSON string. | 30 * Serializes [:object:] into JSON string. |
| 31 */ | 31 */ |
| 32 static String stringify(Object object) { | 32 static String stringify(Object object) { |
| 33 return JsonStringifier.stringify(object); | 33 return JsonStringifier.stringify(object); |
| 34 } | 34 } |
| 35 | |
| 36 /** | |
| 37 * Serializes [:object:] into [:output:] stream. | |
| 38 */ | |
| 39 static void dump(Object object, StringBuffer output) { | |
|
Anton Muhin
2012/06/21 11:02:19
maybe make StringBuffer an optional argument of st
podivilov
2012/06/21 11:44:17
I think these are just different functions, no nee
Anton Muhin
2012/06/21 11:50:00
Okay, than maybe rename it to printOn---there is a
podivilov
2012/06/21 12:03:58
Done.
| |
| 40 return JsonStringifier.dump(object, output); | |
| 41 } | |
| 35 } | 42 } |
| 36 | 43 |
| 37 //// Implementation /////////////////////////////////////////////////////////// | 44 //// Implementation /////////////////////////////////////////////////////////// |
| 38 | 45 |
| 39 // TODO(ajohnsen): Introduce when we have a common exception interface for json. | 46 // TODO(ajohnsen): Introduce when we have a common exception interface for json. |
| 40 class JSONParseException { | 47 class JSONParseException { |
| 41 JSONParseException(int position, String message) : | 48 JSONParseException(int position, String message) : |
| 42 position = position, | 49 position = position, |
| 43 message = 'JSONParseException: $message, at offset $position'; | 50 message = 'JSONParseException: $message, at offset $position'; |
| 44 | 51 |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 393 int position = 0; | 400 int position = 0; |
| 394 static List<int> tokens; | 401 static List<int> tokens; |
| 395 } | 402 } |
| 396 | 403 |
| 397 // TODO: proper base class. | 404 // TODO: proper base class. |
| 398 class JsonUnsupportedObjectType { | 405 class JsonUnsupportedObjectType { |
| 399 const JsonUnsupportedObjectType(); | 406 const JsonUnsupportedObjectType(); |
| 400 } | 407 } |
| 401 | 408 |
| 402 class JsonStringifier { | 409 class JsonStringifier { |
| 403 static String stringify(final object) { | 410 static String stringify(final object) { |
|
Anton Muhin
2012/06/21 11:02:19
maybe we should drop this method altogether and mo
podivilov
2012/06/21 11:44:17
It might be a good idea, but I prefer to keep this
| |
| 404 JsonStringifier stringifier = new JsonStringifier._internal(); | 411 StringBuffer output = new StringBuffer(); |
| 412 JsonStringifier stringifier = new JsonStringifier._internal(output); | |
| 405 stringifier._stringify(object); | 413 stringifier._stringify(object); |
| 406 return stringifier._result; | 414 return output.toString(); |
| 407 } | 415 } |
| 408 | 416 |
| 409 JsonStringifier._internal() | 417 static void dump(final object, StringBuffer output) { |
| 410 : _sb = new StringBuffer(), _seen = new List<Object>() {} | 418 JsonStringifier stringifier = new JsonStringifier._internal(output); |
| 419 stringifier._stringify(object); | |
| 420 } | |
| 421 | |
| 422 JsonStringifier._internal(this._sb) | |
| 423 : _seen = new List<Object>() {} | |
|
Anton Muhin
2012/06/21 11:02:19
nit: please, use _seen = []
podivilov
2012/06/21 11:44:17
Done.
| |
| 424 | |
| 411 StringBuffer _sb; | 425 StringBuffer _sb; |
| 412 List<Object> _seen; // TODO: that should be identity set. | 426 List<Object> _seen; // TODO: that should be identity set. |
| 413 String get _result() { return _sb.toString(); } | |
| 414 | 427 |
| 415 static String _numberToString(num x) { | 428 static String _numberToString(num x) { |
| 416 // TODO: need some more investigation what to do with precision | 429 // TODO: need some more investigation what to do with precision |
| 417 // of double values. | 430 // of double values. |
| 418 if (x is int) { | 431 if (x is int) { |
| 419 return x.toString(); | 432 return x.toString(); |
| 420 } else if (x is double) { | 433 } else if (x is double) { |
| 421 return x.toString(); | 434 return x.toString(); |
| 422 } else { | 435 } else { |
| 423 return x.toDouble().toString(); | 436 return x.toDouble().toString(); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 533 first = false; | 546 first = false; |
| 534 }); | 547 }); |
| 535 _sb.add('}'); | 548 _sb.add('}'); |
| 536 _seen.removeLast(); | 549 _seen.removeLast(); |
| 537 return; | 550 return; |
| 538 } else { | 551 } else { |
| 539 throw const JsonUnsupportedObjectType(); | 552 throw const JsonUnsupportedObjectType(); |
| 540 } | 553 } |
| 541 } | 554 } |
| 542 } | 555 } |
| OLD | NEW |