| 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 "package:expect/expect.dart"; |    6 import "package:expect/expect.dart"; | 
|    7 import 'dart:json' as json; |    7 import "dart:convert"; | 
|    8 import 'dart:html'; |    8 import 'dart:html'; | 
|    9 import '../../pkg/unittest/lib/unittest.dart'; |    9 import '../../pkg/unittest/lib/unittest.dart'; | 
|   10 import '../../pkg/unittest/lib/html_config.dart'; |   10 import '../../pkg/unittest/lib/html_config.dart'; | 
|   11  |   11  | 
|   12 main() { |   12 main() { | 
|   13   useHtmlConfiguration(); |   13   useHtmlConfiguration(); | 
|   14   test('Parse', () { |   14   test('Parse', () { | 
|   15     // Scalars. |   15     // Scalars. | 
|   16     expect(json.parse(' 5 '), equals(5)); |   16     expect(JSON.decode(' 5 '), equals(5)); | 
|   17     expect(json.parse(' -42 '), equals(-42)); |   17     expect(JSON.decode(' -42 '), equals(-42)); | 
|   18     expect(json.parse(' 3e0 '), equals(3)); |   18     expect(JSON.decode(' 3e0 '), equals(3)); | 
|   19     expect(json.parse(' 3.14 '), equals(3.14)); |   19     expect(JSON.decode(' 3.14 '), equals(3.14)); | 
|   20     expect(json.parse('true '), isTrue); |   20     expect(JSON.decode('true '), isTrue); | 
|   21     expect(json.parse(' false'), isFalse); |   21     expect(JSON.decode(' false'), isFalse); | 
|   22     expect(json.parse(' null '), isNull); |   22     expect(JSON.decode(' null '), isNull); | 
|   23     expect(json.parse('\n\rnull\t'), isNull); |   23     expect(JSON.decode('\n\rnull\t'), isNull); | 
|   24     expect(json.parse(' "hi there\\" bob" '), equals('hi there" bob')); |   24     expect(JSON.decode(' "hi there\\" bob" '), equals('hi there" bob')); | 
|   25     expect(json.parse(' "" '), isEmpty); |   25     expect(JSON.decode(' "" '), isEmpty); | 
|   26  |   26  | 
|   27     // Lists. |   27     // Lists. | 
|   28     expect(json.parse(' [] '), isEmpty); |   28     expect(JSON.decode(' [] '), isEmpty); | 
|   29     expect(json.parse('[ ]'), isEmpty); |   29     expect(JSON.decode('[ ]'), isEmpty); | 
|   30     expect(json.parse(' [3, -4.5, true, "hi", false] '), |   30     expect(JSON.decode(' [3, -4.5, true, "hi", false] '), | 
|   31       equals([3, -4.5, true, 'hi', false])); |   31       equals([3, -4.5, true, 'hi', false])); | 
|   32     // Nulls are tricky. |   32     // Nulls are tricky. | 
|   33     expect(json.parse('[null]'), orderedEquals([null])); |   33     expect(JSON.decode('[null]'), orderedEquals([null])); | 
|   34     expect(json.parse(' [3, -4.5, null, true, "hi", false] '), |   34     expect(JSON.decode(' [3, -4.5, null, true, "hi", false] '), | 
|   35       equals([3, -4.5, null, true, 'hi', false])); |   35       equals([3, -4.5, null, true, 'hi', false])); | 
|   36     expect(json.parse('[[null]]'), equals([[null]])); |   36     expect(JSON.decode('[[null]]'), equals([[null]])); | 
|   37     expect(json.parse(' [ [3], [], [null], ["hi", true]] '), |   37     expect(JSON.decode(' [ [3], [], [null], ["hi", true]] '), | 
|   38       equals([[3], [], [null], ['hi', true]])); |   38       equals([[3], [], [null], ['hi', true]])); | 
|   39  |   39  | 
|   40     // Maps. |   40     // Maps. | 
|   41     expect(json.parse(' {} '), isEmpty); |   41     expect(JSON.decode(' {} '), isEmpty); | 
|   42     expect(json.parse('{ }'), isEmpty); |   42     expect(JSON.decode('{ }'), isEmpty); | 
|   43  |   43  | 
|   44     expect(json.parse( |   44     expect(JSON.decode( | 
|   45         ' {"x":3, "y": -4.5,  "z" : "hi","u" : true, "v": false } '), |   45         ' {"x":3, "y": -4.5,  "z" : "hi","u" : true, "v": false } '), | 
|   46         equals({"x":3, "y": -4.5,  "z" : "hi", "u" : true, "v": false })); |   46         equals({"x":3, "y": -4.5,  "z" : "hi", "u" : true, "v": false })); | 
|   47  |   47  | 
|   48     expect(json.parse(' {"x":3, "y": -4.5,  "z" : "hi" } '), |   48     expect(JSON.decode(' {"x":3, "y": -4.5,  "z" : "hi" } '), | 
|   49         equals({"x":3, "y": -4.5,  "z" : "hi" })); |   49         equals({"x":3, "y": -4.5,  "z" : "hi" })); | 
|   50  |   50  | 
|   51     expect(json.parse(' {"y": -4.5,  "z" : "hi" ,"x":3 } '), |   51     expect(JSON.decode(' {"y": -4.5,  "z" : "hi" ,"x":3 } '), | 
|   52         equals({"y": -4.5,  "z" : "hi" ,"x":3 })); |   52         equals({"y": -4.5,  "z" : "hi" ,"x":3 })); | 
|   53  |   53  | 
|   54     expect(json.parse('{ " hi bob " :3, "": 4.5}'), |   54     expect(JSON.decode('{ " hi bob " :3, "": 4.5}'), | 
|   55         equals({ " hi bob " :3, "": 4.5})); |   55         equals({ " hi bob " :3, "": 4.5})); | 
|   56  |   56  | 
|   57     expect(json.parse(' { "x" : { } } '), equals({ 'x' : {}})); |   57     expect(JSON.decode(' { "x" : { } } '), equals({ 'x' : {}})); | 
|   58     expect(json.parse('{"x":{}}'), equals({ 'x' : {}})); |   58     expect(JSON.decode('{"x":{}}'), equals({ 'x' : {}})); | 
|   59  |   59  | 
|   60     // Nulls are tricky. |   60     // Nulls are tricky. | 
|   61     expect(json.parse('{"w":null}'), equals({ 'w' : null})); |   61     expect(JSON.decode('{"w":null}'), equals({ 'w' : null})); | 
|   62  |   62  | 
|   63     expect(json.parse('{"x":{"w":null}}'), equals({"x":{"w":null}})); |   63     expect(JSON.decode('{"x":{"w":null}}'), equals({"x":{"w":null}})); | 
|   64  |   64  | 
|   65     expect(json.parse(' {"x":3, "y": -4.5,  "z" : "hi",' |   65     expect(JSON.decode(' {"x":3, "y": -4.5,  "z" : "hi",' | 
|   66                    '"w":null, "u" : true, "v": false } '), |   66                    '"w":null, "u" : true, "v": false } '), | 
|   67         equals({"x":3, "y": -4.5,  "z" : "hi", |   67         equals({"x":3, "y": -4.5,  "z" : "hi", | 
|   68                    "w":null, "u" : true, "v": false })); |   68                    "w":null, "u" : true, "v": false })); | 
|   69  |   69  | 
|   70     expect(json.parse('{"x": {"a":3, "b": -4.5}, "y":[{}], ' |   70     expect(JSON.decode('{"x": {"a":3, "b": -4.5}, "y":[{}], ' | 
|   71                    '"z":"hi","w":{"c":null,"d":true}, "v":null}'), |   71                    '"z":"hi","w":{"c":null,"d":true}, "v":null}'), | 
|   72         equals({"x": {"a":3, "b": -4.5}, "y":[{}], |   72         equals({"x": {"a":3, "b": -4.5}, "y":[{}], | 
|   73                    "z":"hi","w":{"c":null,"d":true}, "v":null})); |   73                    "z":"hi","w":{"c":null,"d":true}, "v":null})); | 
|   74   }); |   74   }); | 
|   75  |   75  | 
|   76   test('stringify', () { |   76   test('stringify', () { | 
|   77     // Scalars. |   77     // Scalars. | 
|   78     expect(json.stringify(5), equals('5')); |   78     expect(JSON.encode(5), equals('5')); | 
|   79     expect(json.stringify(-42), equals('-42')); |   79     expect(JSON.encode(-42), equals('-42')); | 
|   80     // Dart does not guarantee a formatting for doubles, |   80     // Dart does not guarantee a formatting for doubles, | 
|   81     // so reparse and compare to the original. |   81     // so reparse and compare to the original. | 
|   82     validateRoundTrip(3.14); |   82     validateRoundTrip(3.14); | 
|   83     expect(json.stringify(true), equals('true')); |   83     expect(JSON.encode(true), equals('true')); | 
|   84     expect(json.stringify(false), equals('false')); |   84     expect(JSON.encode(false), equals('false')); | 
|   85     expect(json.stringify(null), equals('null')); |   85     expect(JSON.encode(null), equals('null')); | 
|   86     expect(json.stringify(' hi there" bob '), equals('" hi there\\" bob "')); |   86     expect(JSON.encode(' hi there" bob '), equals('" hi there\\" bob "')); | 
|   87     expect(json.stringify('hi\\there'), equals('"hi\\\\there"')); |   87     expect(JSON.encode('hi\\there'), equals('"hi\\\\there"')); | 
|   88     // TODO(devoncarew): these tests break the dartium build |   88     // TODO(devoncarew): these tests break the dartium build | 
|   89     //expect(json.stringify('hi\nthere'), equals('"hi\\nthere"')); |   89     //expect(JSON.encode('hi\nthere'), equals('"hi\\nthere"')); | 
|   90     //expect(json.stringify('hi\r\nthere'), equals('"hi\\r\\nthere"')); |   90     //expect(JSON.encode('hi\r\nthere'), equals('"hi\\r\\nthere"')); | 
|   91     expect(json.stringify(''), equals('""')); |   91     expect(JSON.encode(''), equals('""')); | 
|   92  |   92  | 
|   93     // Lists. |   93     // Lists. | 
|   94     expect(json.stringify([]), equals('[]')); |   94     expect(JSON.encode([]), equals('[]')); | 
|   95     expect(json.stringify(new List(0)), equals('[]')); |   95     expect(JSON.encode(new List(0)), equals('[]')); | 
|   96     expect(json.stringify(new List(3)), equals('[null,null,null]')); |   96     expect(JSON.encode(new List(3)), equals('[null,null,null]')); | 
|   97     validateRoundTrip([3, -4.5, null, true, 'hi', false]); |   97     validateRoundTrip([3, -4.5, null, true, 'hi', false]); | 
|   98     expect(json.stringify([[3], [], [null], ['hi', true]]), |   98     expect(JSON.encode([[3], [], [null], ['hi', true]]), | 
|   99       equals('[[3],[],[null],["hi",true]]')); |   99       equals('[[3],[],[null],["hi",true]]')); | 
|  100  |  100  | 
|  101     // Maps. |  101     // Maps. | 
|  102     expect(json.stringify({}), equals('{}')); |  102     expect(JSON.encode({}), equals('{}')); | 
|  103     expect(json.stringify(new Map()), equals('{}')); |  103     expect(JSON.encode(new Map()), equals('{}')); | 
|  104     expect(json.stringify({'x':{}}), equals('{"x":{}}')); |  104     expect(JSON.encode({'x':{}}), equals('{"x":{}}')); | 
|  105     expect(json.stringify({'x':{'a':3}}), equals('{"x":{"a":3}}')); |  105     expect(JSON.encode({'x':{'a':3}}), equals('{"x":{"a":3}}')); | 
|  106  |  106  | 
|  107     // Dart does not guarantee an order on the keys |  107     // Dart does not guarantee an order on the keys | 
|  108     // of a map literal, so reparse and compare to the original Map. |  108     // of a map literal, so reparse and compare to the original Map. | 
|  109     validateRoundTrip( |  109     validateRoundTrip( | 
|  110         {'x':3, 'y':-4.5, 'z':'hi', 'w':null, 'u':true, 'v':false}); |  110         {'x':3, 'y':-4.5, 'z':'hi', 'w':null, 'u':true, 'v':false}); | 
|  111     validateRoundTrip({"x":3, "y":-4.5, "z":'hi'}); |  111     validateRoundTrip({"x":3, "y":-4.5, "z":'hi'}); | 
|  112     validateRoundTrip({' hi bob ':3, '':4.5}); |  112     validateRoundTrip({' hi bob ':3, '':4.5}); | 
|  113     validateRoundTrip( |  113     validateRoundTrip( | 
|  114         {'x':{'a':3, 'b':-4.5}, 'y':[{}], 'z':'hi', 'w':{'c':null, 'd':true}, |  114         {'x':{'a':3, 'b':-4.5}, 'y':[{}], 'z':'hi', 'w':{'c':null, 'd':true}, | 
|  115                   'v':null}); |  115                   'v':null}); | 
|  116  |  116  | 
|  117     expect(json.stringify(new ToJson(4)), "4"); |  117     expect(JSON.encode(new ToJson(4)), "4"); | 
|  118     expect(json.stringify(new ToJson([4, "a"])), '[4,"a"]'); |  118     expect(JSON.encode(new ToJson([4, "a"])), '[4,"a"]'); | 
|  119     expect(json.stringify(new ToJson([4, new ToJson({"x":42})])), |  119     expect(JSON.encode(new ToJson([4, new ToJson({"x":42})])), | 
|  120            '[4,{"x":42}]'); |  120            '[4,{"x":42}]'); | 
|  121  |  121  | 
|  122     Expect.throws(() { |  122     Expect.throws(() { | 
|  123       json.stringify([new ToJson(new ToJson(4))]); |  123       JSON.encode([new ToJson(new ToJson(4))]); | 
|  124     }); |  124     }); | 
|  125  |  125  | 
|  126     Expect.throws(() { |  126     Expect.throws(() { | 
|  127       json.stringify([new Object()]); |  127       JSON.encode([new Object()]); | 
|  128     }); |  128     }); | 
|  129  |  129  | 
|  130   }); |  130   }); | 
|  131  |  131  | 
|  132   test('stringify throws if argument cannot be converted', () { |  132   test('stringify throws if argument cannot be converted', () { | 
|  133     /** |  133     /** | 
|  134      * Checks that we get an exception (rather than silently returning null) if |  134      * Checks that we get an exception (rather than silently returning null) if | 
|  135      * we try to stringify something that cannot be converted to json. |  135      * we try to stringify something that cannot be converted to json. | 
|  136      */ |  136      */ | 
|  137     expect(() => json.stringify(new TestClass()), throws); |  137     expect(() => JSON.encode(new TestClass()), throws); | 
|  138   }); |  138   }); | 
|  139 } |  139 } | 
|  140  |  140  | 
|  141 class TestClass { |  141 class TestClass { | 
|  142   int x; |  142   int x; | 
|  143   String y; |  143   String y; | 
|  144  |  144  | 
|  145   TestClass() : x = 3, y = 'joe' { } |  145   TestClass() : x = 3, y = 'joe' { } | 
|  146 } |  146 } | 
|  147  |  147  | 
|  148 class ToJson { |  148 class ToJson { | 
|  149   final object; |  149   final object; | 
|  150   const ToJson(this.object); |  150   const ToJson(this.object); | 
|  151   toJson() => object; |  151   toJson() => object; | 
|  152 } |  152 } | 
|  153  |  153  | 
|  154 /** |  154 /** | 
|  155  * Checks that the argument can be converted to a JSON string and |  155  * Checks that the argument can be converted to a JSON string and | 
|  156  * back, and produce something equivalent to the argument. |  156  * back, and produce something equivalent to the argument. | 
|  157  */ |  157  */ | 
|  158 validateRoundTrip(expected) { |  158 validateRoundTrip(expected) { | 
|  159   expect(json.parse(json.stringify(expected)), equals(expected)); |  159   expect(JSON.decode(JSON.encode(expected)), equals(expected)); | 
|  160 } |  160 } | 
|  161  |  161  | 
|  162  |  162  | 
| OLD | NEW |