OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of dart.convert; | 5 part of dart.convert; |
6 | 6 |
7 /** | 7 /** |
8 * Error thrown by JSON serialization if an object cannot be serialized. | 8 * Error thrown by JSON serialization if an object cannot be serialized. |
9 * | 9 * |
10 * The [unsupportedObject] field holds that object that failed to be serialized. | 10 * The [unsupportedObject] field holds that object that failed to be serialized. |
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 } | 419 } |
420 | 420 |
421 /** | 421 /** |
422 * Serializes a [num], [String], [bool], [Null], [List] or [Map] value. | 422 * Serializes a [num], [String], [bool], [Null], [List] or [Map] value. |
423 * | 423 * |
424 * Returns true if the value is one of these types, and false if not. | 424 * Returns true if the value is one of these types, and false if not. |
425 * If a value is both a [List] and a [Map], it's serialized as a [List]. | 425 * If a value is both a [List] and a [Map], it's serialized as a [List]. |
426 */ | 426 */ |
427 bool stringifyJsonValue(final object) { | 427 bool stringifyJsonValue(final object) { |
428 if (object is num) { | 428 if (object is num) { |
429 // TODO: use writeOn. | 429 if (!object.isFinite) return false; |
430 sink.write(numberToString(object)); | 430 sink.write(numberToString(object)); |
431 return true; | 431 return true; |
432 } else if (identical(object, true)) { | 432 } else if (identical(object, true)) { |
433 sink.write('true'); | 433 sink.write('true'); |
434 return true; | 434 return true; |
435 } else if (identical(object, false)) { | 435 } else if (identical(object, false)) { |
436 sink.write('false'); | 436 sink.write('false'); |
437 return true; | 437 return true; |
438 } else if (object == null) { | 438 } else if (object == null) { |
439 sink.write('null'); | 439 sink.write('null'); |
440 return true; | 440 return true; |
441 } else if (object is String) { | 441 } else if (object is String) { |
442 sink.write('"'); | 442 sink.write('"'); |
443 escape(sink, object); | 443 escape(sink, object); |
444 sink.write('"'); | 444 sink.write('"'); |
445 return true; | 445 return true; |
446 } else if (object is List) { | 446 } else if (object is List) { |
447 checkCycle(object); | 447 checkCycle(object); |
448 List a = object; | 448 List a = object; |
449 sink.write('['); | 449 sink.write('['); |
450 if (a.length > 0) { | 450 if (a.length > 0) { |
451 stringifyValue(a[0]); | 451 stringifyValue(a[0]); |
452 // TODO: switch to Iterables. | |
453 for (int i = 1; i < a.length; i++) { | 452 for (int i = 1; i < a.length; i++) { |
454 sink.write(','); | 453 sink.write(','); |
455 stringifyValue(a[i]); | 454 stringifyValue(a[i]); |
456 } | 455 } |
457 } | 456 } |
458 sink.write(']'); | 457 sink.write(']'); |
459 seen.remove(object); | 458 seen.remove(object); |
460 return true; | 459 return true; |
461 } else if (object is Map) { | 460 } else if (object is Map) { |
462 checkCycle(object); | 461 checkCycle(object); |
(...skipping 12 matching lines...) Expand all Loading... |
475 first = false; | 474 first = false; |
476 }); | 475 }); |
477 sink.write('}'); | 476 sink.write('}'); |
478 seen.remove(object); | 477 seen.remove(object); |
479 return true; | 478 return true; |
480 } else { | 479 } else { |
481 return false; | 480 return false; |
482 } | 481 } |
483 } | 482 } |
484 } | 483 } |
OLD | NEW |