OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import "package:expect/expect.dart"; |
| 6 import 'dart:convert'; |
| 7 |
| 8 final TESTS = [ |
| 9 [ 5, '5' ], |
| 10 [ -42, '-42' ], |
| 11 [ 3.14, '3.14' ], |
| 12 [ true, 'true' ], |
| 13 [ false, 'false' ], |
| 14 [ null, 'null' ], |
| 15 [ 'quote"or\'', '"quote\\"or\'"' ], |
| 16 [ '', '""' ], |
| 17 [ [], "[]" ], |
| 18 [ [3, -4.5, true, "hi", false], '[3,-4.5,true,"hi",false]' ], |
| 19 [ [null], "[null]" ], |
| 20 [ [[null]], "[[null]]" ], |
| 21 [ [[3]], "[[3]]" ], |
| 22 [ {}, "{}" ], |
| 23 [ { "x": 3, "y": 4.5, "z": "hi", "u": true, "v": false }, |
| 24 '{"x":3,"y":4.5,"z":"hi","u":true,"v":false}' ], |
| 25 [ { "x": null }, '{"x":null}' ], |
| 26 [ { "x": {} }, '{"x":{}}' ], |
| 27 // Note that -0.0 won't be treated the same in JS. The Json spec seems to |
| 28 // allow it, though. |
| 29 [ { "hi there": 499, "'":-0.0 }, '{"hi there":499,"\'":-0.0}' ], |
| 30 [ r'\foo', r'"\\foo"' ], |
| 31 ]; |
| 32 |
| 33 bool isJsonEqual(o1, o2) { |
| 34 if (o1 == o2) return true; |
| 35 if (o1 is List && o2 is List) { |
| 36 if (o1.length != o2.length) return false; |
| 37 for (int i = 0; i < o1.length; i++) { |
| 38 if (!isJsonEqual(o1[i], o2[i])) return false; |
| 39 } |
| 40 return true; |
| 41 } |
| 42 if (o1 is Map && o2 is Map) { |
| 43 if (o1.length != o2.length) return false; |
| 44 for (var key in o1.keys) { |
| 45 Expect.isTrue(key is String); |
| 46 if (!o2.containsKey(key)) return false; |
| 47 if (!isJsonEqual(o1[key], o2[key])) return false; |
| 48 } |
| 49 return true; |
| 50 } |
| 51 return false; |
| 52 } |
| 53 |
| 54 Object decode(String str) { |
| 55 Object result; |
| 56 var decoder = new JsonDecoder(null); |
| 57 ChunkedConversionSink objectSink = |
| 58 new ChunkedConversionSink.withCallback((x) => result = x.single); |
| 59 var stringConversionSink = decoder.startChunkedConversion(objectSink); |
| 60 stringConversionSink.add(str); |
| 61 stringConversionSink.close(); |
| 62 return result; |
| 63 } |
| 64 |
| 65 Object decode2(String str) { |
| 66 Object result; |
| 67 var decoder = new JsonDecoder(null); |
| 68 ChunkedConversionSink objectSink = |
| 69 new ChunkedConversionSink.withCallback((x) => result = x.single); |
| 70 var stringConversionSink = decoder.startChunkedConversion(objectSink); |
| 71 ClosableStringSink stringSink = stringConversionSink.asStringSink(); |
| 72 stringSink.write(str); |
| 73 stringSink.close(); |
| 74 return result; |
| 75 } |
| 76 |
| 77 Object decode3(String str) { |
| 78 Object result; |
| 79 var decoder = new JsonDecoder(null); |
| 80 ChunkedConversionSink objectSink = |
| 81 new ChunkedConversionSink.withCallback((x) => result = x.single); |
| 82 var stringConversionSink = decoder.startChunkedConversion(objectSink); |
| 83 ClosableStringSink stringSink = stringConversionSink.asStringSink(); |
| 84 str.codeUnits.forEach(stringSink.writeCharCode); |
| 85 stringSink.close(); |
| 86 return result; |
| 87 } |
| 88 |
| 89 Object decode4(String str) { |
| 90 Object result; |
| 91 var decoder = new JsonDecoder(null); |
| 92 ChunkedConversionSink objectSink = |
| 93 new ChunkedConversionSink.withCallback((x) => result = x.single); |
| 94 var stringConversionSink = decoder.startChunkedConversion(objectSink); |
| 95 ClosableStringSink stringSink = stringConversionSink.asStringSink(); |
| 96 str.runes.forEach(stringSink.writeCharCode); |
| 97 stringSink.close(); |
| 98 return result; |
| 99 } |
| 100 |
| 101 Object decode5(String str) { |
| 102 Object result; |
| 103 var decoder = new JsonDecoder(null); |
| 104 ChunkedConversionSink objectSink = |
| 105 new ChunkedConversionSink.withCallback((x) => result = x.single); |
| 106 var stringConversionSink = decoder.startChunkedConversion(objectSink); |
| 107 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(false); |
| 108 Object tmpBytes = UTF8.encode(str); |
| 109 inputByteSink.add(tmpBytes); |
| 110 inputByteSink.close(); |
| 111 return result; |
| 112 } |
| 113 |
| 114 Object decode6(String str) { |
| 115 Object result; |
| 116 var decoder = new JsonDecoder(null); |
| 117 ChunkedConversionSink objectSink = |
| 118 new ChunkedConversionSink.withCallback((x) => result = x.single); |
| 119 var stringConversionSink = decoder.startChunkedConversion(objectSink); |
| 120 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(false); |
| 121 Object tmpBytes = UTF8.encode(str); |
| 122 tmpBytes.forEach((b) => inputByteSink.addSlice([0, b, 1], 1, 2, false)); |
| 123 inputByteSink.close(); |
| 124 return result; |
| 125 } |
| 126 |
| 127 Object decode7(String str) { |
| 128 Object result; |
| 129 var decoder = new JsonDecoder(null); |
| 130 ChunkedConversionSink objectSink = |
| 131 new ChunkedConversionSink.withCallback((x) => result = x.single); |
| 132 var stringConversionSink = decoder.startChunkedConversion(objectSink); |
| 133 stringConversionSink.addSlice("1" + str + "2", 1, str.length + 1, false); |
| 134 stringConversionSink.close(); |
| 135 return result; |
| 136 } |
| 137 |
| 138 |
| 139 main() { |
| 140 var tests = TESTS.expand((test) { |
| 141 var object = test[0]; |
| 142 var string = test[1]; |
| 143 var longString = " " |
| 144 " " |
| 145 "$string" |
| 146 " " |
| 147 " "; |
| 148 return [test, [object, longString]]; |
| 149 }); |
| 150 for (var test in TESTS) { |
| 151 var o = test[0]; |
| 152 var string = test[1]; |
| 153 Expect.isTrue(isJsonEqual(o, decode(string))); |
| 154 Expect.isTrue(isJsonEqual(o, decode2(string))); |
| 155 Expect.isTrue(isJsonEqual(o, decode3(string))); |
| 156 Expect.isTrue(isJsonEqual(o, decode4(string))); |
| 157 Expect.isTrue(isJsonEqual(o, decode5(string))); |
| 158 Expect.isTrue(isJsonEqual(o, decode6(string))); |
| 159 Expect.isTrue(isJsonEqual(o, decode7(string))); |
| 160 } |
| 161 } |
OLD | NEW |