| 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_tests; | 5 library json_tests; |
| 6 import 'dart:json' as json; | 6 import 'dart:json' as json; |
| 7 import 'dart:html'; | 7 import 'dart:html'; |
| 8 import '../../pkg/unittest/lib/unittest.dart'; | 8 import '../../pkg/unittest/lib/unittest.dart'; |
| 9 import '../../pkg/unittest/lib/html_config.dart'; | 9 import '../../pkg/unittest/lib/html_config.dart'; |
| 10 | 10 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 expect(json.parse(' {"x":3, "y": -4.5, "z" : "hi",' | 64 expect(json.parse(' {"x":3, "y": -4.5, "z" : "hi",' |
| 65 '"w":null, "u" : true, "v": false } '), | 65 '"w":null, "u" : true, "v": false } '), |
| 66 equals({"x":3, "y": -4.5, "z" : "hi", | 66 equals({"x":3, "y": -4.5, "z" : "hi", |
| 67 "w":null, "u" : true, "v": false })); | 67 "w":null, "u" : true, "v": false })); |
| 68 | 68 |
| 69 expect(json.parse('{"x": {"a":3, "b": -4.5}, "y":[{}], ' | 69 expect(json.parse('{"x": {"a":3, "b": -4.5}, "y":[{}], ' |
| 70 '"z":"hi","w":{"c":null,"d":true}, "v":null}'), | 70 '"z":"hi","w":{"c":null,"d":true}, "v":null}'), |
| 71 equals({"x": {"a":3, "b": -4.5}, "y":[{}], | 71 equals({"x": {"a":3, "b": -4.5}, "y":[{}], |
| 72 "z":"hi","w":{"c":null,"d":true}, "v":null})); | 72 "z":"hi","w":{"c":null,"d":true}, "v":null})); |
| 73 | 73 |
| 74 // Errors. | |
| 75 expect(() => json.parse('True'), throwsFormatException); | |
| 76 // Unfinished maps. | |
| 77 expect(() => json.parse('{"a":10)'), throwsFormatException); | |
| 78 expect(() => json.parse('{"a":}'), throwsFormatException); | |
| 79 // Insist on double quotes. | |
| 80 expect(() => json.parse("{'a':1}"), throwsFormatException); | |
| 81 // Insist on quoted keys. | |
| 82 expect(() => json.parse('{a:1}'), throwsFormatException); | |
| 83 }); | |
| 84 | |
| 85 test('Parse-V8-bugs', () { | |
| 86 expect(json.parse('{"__proto__": {"x":42}, "y": 37}'), | |
| 87 equals({"__proto__": {"x":42}, "y": 37})); | |
| 88 | |
| 89 expect(json.parse('{"__proto__": []}'), | |
| 90 equals({"__proto__": []})); | |
| 91 }); | |
| 92 | |
| 93 test('Reviver', () { | |
| 94 reviver(k, v) { | |
| 95 if (k == '') return [v]; // wrap top level in a list. | |
| 96 if (v is num || v is String || v is bool || v == null) return '$k$v'; | |
| 97 return v; | |
| 98 } | |
| 99 | |
| 100 go(input, expected) { | |
| 101 expect(json.parse(input, reviver), equals(expected)); | |
| 102 } | |
| 103 | |
| 104 go('null', [null]); | |
| 105 go('["a", "b", true]', [['0a', '1b', '2true']]); | |
| 106 go('{"a":100, "b":200}', [{"a":'a100', "b":'b200'}]); | |
| 107 go('{"a":100, "b":{"c":200}}', [{"a":'a100', "b":{"c":'c200'}}]); | |
| 108 }); | |
| 109 | |
| 110 test('stringify', () { | 74 test('stringify', () { |
| 111 // Scalars. | 75 // Scalars. |
| 112 expect(json.stringify(5), equals('5')); | 76 expect(json.stringify(5), equals('5')); |
| 113 expect(json.stringify(-42), equals('-42')); | 77 expect(json.stringify(-42), equals('-42')); |
| 114 // Dart does not guarantee a formatting for doubles, | 78 // Dart does not guarantee a formatting for doubles, |
| 115 // so reparse and compare to the original. | 79 // so reparse and compare to the original. |
| 116 validateRoundTrip(3.14); | 80 validateRoundTrip(3.14); |
| 117 expect(json.stringify(true), equals('true')); | 81 expect(json.stringify(true), equals('true')); |
| 118 expect(json.stringify(false), equals('false')); | 82 expect(json.stringify(false), equals('false')); |
| 119 expect(json.stringify(null), equals('null')); | 83 expect(json.stringify(null), equals('null')); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 }); | 126 }); |
| 163 | 127 |
| 164 }); | 128 }); |
| 165 | 129 |
| 166 test('stringify throws if argument cannot be converted', () { | 130 test('stringify throws if argument cannot be converted', () { |
| 167 /** | 131 /** |
| 168 * Checks that we get an exception (rather than silently returning null) if | 132 * Checks that we get an exception (rather than silently returning null) if |
| 169 * we try to stringify something that cannot be converted to json. | 133 * we try to stringify something that cannot be converted to json. |
| 170 */ | 134 */ |
| 171 expect(() => json.stringify(new TestClass()), throws); | 135 expect(() => json.stringify(new TestClass()), throws); |
| 136 }); |
| 172 }); | 137 }); |
| 173 } | 138 } |
| 174 | 139 |
| 175 class TestClass { | 140 class TestClass { |
| 176 int x; | 141 int x; |
| 177 String y; | 142 String y; |
| 178 | 143 |
| 179 TestClass() : x = 3, y = 'joe' { } | 144 TestClass() : x = 3, y = 'joe' { } |
| 180 } | 145 } |
| 181 | 146 |
| 182 class ToJson { | 147 class ToJson { |
| 183 final object; | 148 final object; |
| 184 const ToJson(this.object); | 149 const ToJson(this.object); |
| 185 toJson() => object; | 150 toJson() => object; |
| 186 } | 151 } |
| 187 | 152 |
| 188 /** | 153 /** |
| 189 * Checks that the argument can be converted to a JSON string and | 154 * Checks that the argument can be converted to a JSON string and |
| 190 * back, and produce something equivalent to the argument. | 155 * back, and produce something equivalent to the argument. |
| 191 */ | 156 */ |
| 192 validateRoundTrip(expected) { | 157 validateRoundTrip(expected) { |
| 193 expect(json.parse(json.stringify(expected)), equals(expected)); | 158 expect(json.parse(json.stringify(expected)), equals(expected)); |
| 194 } | 159 } |
| 160 |
| 161 |
| OLD | NEW |