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 import "dart:typed_data"; | 5 import "dart:typed_data"; |
6 | 6 |
7 // JSON conversion. | 7 // JSON conversion. |
8 | 8 |
9 patch _parseJson(String json, reviver(var key, var value)) { | 9 patch _parseJson(String json, reviver(var key, var value)) { |
10 _BuildJsonListener listener; | 10 _BuildJsonListener listener; |
(...skipping 15 matching lines...) Expand all Loading... |
26 void handleNumber(num value) {} | 26 void handleNumber(num value) {} |
27 void handleBool(bool value) {} | 27 void handleBool(bool value) {} |
28 void handleNull() {} | 28 void handleNull() {} |
29 void beginObject() {} | 29 void beginObject() {} |
30 void propertyName() {} | 30 void propertyName() {} |
31 void propertyValue() {} | 31 void propertyValue() {} |
32 void endObject() {} | 32 void endObject() {} |
33 void beginArray() {} | 33 void beginArray() {} |
34 void arrayElement() {} | 34 void arrayElement() {} |
35 void endArray() {} | 35 void endArray() {} |
36 /** Called on failure to parse [source]. */ | |
37 void fail(String source, int position, String message) {} | |
38 } | 36 } |
39 | 37 |
40 /** | 38 /** |
41 * A [JsonListener] that builds data objects from the parser events. | 39 * A [JsonListener] that builds data objects from the parser events. |
42 * | 40 * |
43 * This is a simple stack-based object builder. It keeps the most recently | 41 * This is a simple stack-based object builder. It keeps the most recently |
44 * seen value in a variable, and uses it depending on the following event. | 42 * seen value in a variable, and uses it depending on the following event. |
45 */ | 43 */ |
46 class _BuildJsonListener extends _JsonListener { | 44 class _BuildJsonListener extends _JsonListener { |
47 /** | 45 /** |
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 // This correctly creates -0.0 for doubles. | 559 // This correctly creates -0.0 for doubles. |
562 listener.handleNumber(_parseDouble(source, start, position)); | 560 listener.handleNumber(_parseDouble(source, start, position)); |
563 return position; | 561 return position; |
564 } | 562 } |
565 | 563 |
566 static double _parseDouble(String source, int start, int end) | 564 static double _parseDouble(String source, int start, int end) |
567 native "Double_parse"; | 565 native "Double_parse"; |
568 | 566 |
569 void fail(int position, [String message]) { | 567 void fail(int position, [String message]) { |
570 if (message == null) message = "Unexpected character"; | 568 if (message == null) message = "Unexpected character"; |
571 listener.fail(source, position, message); | 569 throw new FormatException(message, source, position); |
572 // If the listener didn't throw, do it here. | |
573 String slice; | |
574 int sliceEnd = position + 20; | |
575 if (sliceEnd > source.length) { | |
576 slice = "'${source.substring(position)}'"; | |
577 } else { | |
578 slice = "'${source.substring(position, sliceEnd)}...'"; | |
579 } | |
580 throw new FormatException("Unexpected character at $position: $slice"); | |
581 } | 570 } |
582 } | 571 } |
583 | 572 |
584 // UTF-8 conversion. | 573 // UTF-8 conversion. |
585 | 574 |
586 patch class _Utf8Encoder { | 575 patch class _Utf8Encoder { |
587 /* patch */ static List<int> _createBuffer(int size) => new Uint8List(size); | 576 /* patch */ static List<int> _createBuffer(int size) => new Uint8List(size); |
588 } | 577 } |
OLD | NEW |