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

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

Issue 10632002: Implement JSON serialization to stream. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rename dump to printOn. Created 8 years, 6 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 | « no previous file | 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) 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
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.
Søren Gjesse 2012/06/25 07:46:38 DBC Shouldn this be just [object] and [output] (w
podivilov 2012/06/25 13:28:44 grep showed that it probably should. Do you know i
ahe 2012/06/25 13:30:22 Sorry, Google-internal URL: http://chromegw.corp.
38 */
39 static void printOn(Object object, StringBuffer output) {
40 return JsonStringifier.printOn(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 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
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) {
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 printOn(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 = [];
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
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 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698