b267bd7c98e4e10fef7e93e91cebdf63dedca924
[mono.git] / mcs / class / System.Json / Test / System.Json / JsonValueTest.cs
1 //
2 // JsonValueTest.cs: Tests for JSonValue
3 //
4 // Copyright 2011 Xamarin, Inc.
5 //
6 // Authors:
7 //   Miguel de Icaza
8 //
9 using NUnit.Framework;
10 using System;
11 using System.IO;
12 using System.Text;
13 using System.Json;
14 using System.Globalization;
15 using System.Threading;
16
17 namespace MonoTests.System
18 {
19         [TestFixture]
20         public class JsonValueTests {
21                 // Tests that a trailing comma is allowed in dictionary definitions
22                 [Test]
23                 public void LoadWithTrailingComma ()
24                 {
25                         var j = JsonValue.Load (new StringReader ("{ \"a\": \"b\",}"));
26                         Assert.AreEqual (1, j.Count, "itemcount");
27                         Assert.AreEqual (JsonType.String, j ["a"].JsonType, "type");
28                         Assert.AreEqual ("b", (string) j ["a"], "value");
29
30                         JsonValue.Parse ("[{ \"a\": \"b\",}]");
31                 }
32
33                 [Test]
34                 public void LoadWithTrailingComma2 ()
35                 {
36                         JsonValue.Parse ("[{ \"a\": \"b\",}]");
37                 }
38
39                 // Test that we correctly serialize JsonArray with null elements.
40                 [Test]
41                 public void ToStringOnJsonArrayWithNulls () {
42                         var j = JsonValue.Load (new StringReader ("[1,2,3,null]"));
43                         Assert.AreEqual (4, j.Count, "itemcount");
44                         Assert.AreEqual (JsonType.Array, j.JsonType, "type");
45                         var str = j.ToString ();
46                         Assert.AreEqual (str, "[1, 2, 3, null]");
47                 }
48
49                 // Test that we correctly serialize JsonObject with null elements.
50                 [Test]
51                 public void ToStringOnJsonObjectWithNulls () {
52                         var j = JsonValue.Load (new StringReader ("{\"a\":null,\"b\":2}"));
53                         Assert.AreEqual (2, j.Count, "itemcount");
54                         Assert.AreEqual (JsonType.Object, j.JsonType, "type");
55                         var str = j.ToString ();
56                         Assert.AreEqual (str, "{\"a\": null, \"b\": 2}");
57                 }
58
59                 [Test]
60                 public void JsonObjectOrder () {
61                         var obj = new JsonObject ();
62                         obj["a"] = 1;
63                         obj["c"] = 3;
64                         obj["b"] = 2;
65                         var str = obj.ToString ();
66                         Assert.AreEqual (str, "{\"a\": 1, \"b\": 2, \"c\": 3}");
67                 }
68
69                 [Test]
70                 public void QuoteEscapeBug_20869 () 
71                 {
72                         Assert.AreEqual ((new JsonPrimitive ("\"\"")).ToString (), "\"\\\"\\\"\"");
73                 }
74
75                 void ExpectError (string s)
76                 {
77                         try {
78                                 JsonValue.Parse (s);
79                                 Assert.Fail ("Expected ArgumentException for `" + s + "'");
80                         } catch (ArgumentException) {
81                         }
82                 }
83
84                 // Test whether an exception is thrown for invalid JSON
85                 [Test]
86                 public void CheckErrors () 
87                 {
88                         ExpectError (@"-");
89                         ExpectError (@"- ");
90                         ExpectError (@"1.");
91                         ExpectError (@"1. ");
92                         ExpectError (@"1e+");
93                         ExpectError (@"1 2");
94                         ExpectError (@"077");
95
96                         ExpectError (@"[1,]");
97
98                         //ExpectError (@"{""a"":1,}"); // Not valid JSON, allowed anyway
99                 }
100
101                 // Parse a json string and compare to the expected value
102                 void CheckDouble (double expected, string json)
103                 {
104                         double jvalue = (double) JsonValue.Parse (json);
105                         Assert.AreEqual (expected, jvalue);
106                 }
107
108                 // Convert a number to json and parse the string, then compare the result to the original value
109                 void CheckDouble (double number)
110                 {
111                         double jvalue = (double) JsonValue.Parse (new JsonPrimitive (number).ToString ());
112                         Assert.AreEqual (number, jvalue); // should be exactly the same
113                 }
114
115                 [Test]
116                 public void CheckIntegers ()
117                 {
118                         Assert.AreEqual (sbyte.MinValue, (sbyte) JsonValue.Parse (new JsonPrimitive (sbyte.MinValue).ToString ()));
119                         Assert.AreEqual (sbyte.MaxValue, (sbyte) JsonValue.Parse (new JsonPrimitive (sbyte.MaxValue).ToString ()));
120                         Assert.AreEqual (byte.MinValue, (byte) JsonValue.Parse (new JsonPrimitive (byte.MinValue).ToString ()));
121                         Assert.AreEqual (byte.MaxValue, (byte) JsonValue.Parse (new JsonPrimitive (byte.MaxValue).ToString ()));
122
123                         Assert.AreEqual (short.MinValue, (short) JsonValue.Parse (new JsonPrimitive (short.MinValue).ToString ()));
124                         Assert.AreEqual (short.MaxValue, (short) JsonValue.Parse (new JsonPrimitive (short.MaxValue).ToString ()));
125                         Assert.AreEqual (ushort.MinValue, (ushort) JsonValue.Parse (new JsonPrimitive (ushort.MinValue).ToString ()));
126                         Assert.AreEqual (ushort.MaxValue, (ushort) JsonValue.Parse (new JsonPrimitive (ushort.MaxValue).ToString ()));
127
128                         Assert.AreEqual (int.MinValue, (int) JsonValue.Parse (new JsonPrimitive (int.MinValue).ToString ()));
129                         Assert.AreEqual (int.MaxValue, (int) JsonValue.Parse (new JsonPrimitive (int.MaxValue).ToString ()));
130                         Assert.AreEqual (uint.MinValue, (uint) JsonValue.Parse (new JsonPrimitive (uint.MinValue).ToString ()));
131                         Assert.AreEqual (uint.MaxValue, (uint) JsonValue.Parse (new JsonPrimitive (uint.MaxValue).ToString ()));
132
133                         Assert.AreEqual (long.MinValue, (long) JsonValue.Parse (new JsonPrimitive (long.MinValue).ToString ()));
134                         Assert.AreEqual (long.MaxValue, (long) JsonValue.Parse (new JsonPrimitive (long.MaxValue).ToString ()));
135                         Assert.AreEqual (ulong.MinValue, (ulong) JsonValue.Parse (new JsonPrimitive (ulong.MinValue).ToString ()));
136                         Assert.AreEqual (ulong.MaxValue, (ulong) JsonValue.Parse (new JsonPrimitive (ulong.MaxValue).ToString ()));
137                 }
138
139                 [Test]
140                 public void CheckNumbers () 
141                 {
142                         CheckDouble (0, "0");
143                         CheckDouble (0, "-0");
144                         CheckDouble (0, "0.00");
145                         CheckDouble (0, "-0.00");
146                         CheckDouble (1, "1");
147                         CheckDouble (1.1, "1.1");
148                         CheckDouble (-1, "-1");
149                         CheckDouble (-1.1, "-1.1");
150                         CheckDouble (1e-10, "1e-10");
151                         CheckDouble (1e+10, "1e+10");
152                         CheckDouble (1e-30, "1e-30");
153                         CheckDouble (1e+30, "1e+30");
154
155                         CheckDouble (1, "\"1\"");
156                         CheckDouble (1.1, "\"1.1\"");
157                         CheckDouble (-1, "\"-1\"");
158                         CheckDouble (-1.1, "\"-1.1\"");
159
160                         CheckDouble (double.NaN, "\"NaN\"");
161                         CheckDouble (double.PositiveInfinity, "\"Infinity\"");
162                         CheckDouble (double.NegativeInfinity, "\"-Infinity\"");
163
164                         ExpectError ("NaN");
165                         ExpectError ("Infinity");
166                         ExpectError ("-Infinity");
167
168                         Assert.AreEqual ("1.1", new JsonPrimitive (1.1).ToString ());
169                         Assert.AreEqual ("-1.1", new JsonPrimitive (-1.1).ToString ());
170                         Assert.AreEqual ("1E-20", new JsonPrimitive (1e-20).ToString ());
171                         Assert.AreEqual ("1E+20", new JsonPrimitive (1e+20).ToString ());
172                         Assert.AreEqual ("1E-30", new JsonPrimitive (1e-30).ToString ());
173                         Assert.AreEqual ("1E+30", new JsonPrimitive (1e+30).ToString ());
174                         Assert.AreEqual ("\"NaN\"", new JsonPrimitive (double.NaN).ToString ());
175                         Assert.AreEqual ("\"Infinity\"", new JsonPrimitive (double.PositiveInfinity).ToString ());
176                         Assert.AreEqual ("\"-Infinity\"", new JsonPrimitive (double.NegativeInfinity).ToString ());
177
178                         Assert.AreEqual ("1E-30", JsonValue.Parse ("1e-30").ToString ());
179                         Assert.AreEqual ("1E+30", JsonValue.Parse ("1e+30").ToString ());
180
181                         CheckDouble (1);
182                         CheckDouble (1.1);
183                         CheckDouble (1.25);
184                         CheckDouble (-1);
185                         CheckDouble (-1.1);
186                         CheckDouble (-1.25);
187                         CheckDouble (1e-20);
188                         CheckDouble (1e+20);
189                         CheckDouble (1e-30);
190                         CheckDouble (1e+30);
191                         CheckDouble (3.1415926535897932384626433);
192                         CheckDouble (3.1415926535897932384626433e-20);
193                         CheckDouble (3.1415926535897932384626433e+20);
194                         CheckDouble (double.NaN);
195                         CheckDouble (double.PositiveInfinity);
196                         CheckDouble (double.NegativeInfinity);
197                         CheckDouble (double.MinValue);
198                         CheckDouble (double.MaxValue);
199
200                         // A number which needs 17 digits (see http://stackoverflow.com/questions/6118231/why-do-i-need-17-significant-digits-and-not-16-to-represent-a-double)
201                         CheckDouble (18014398509481982.0);
202
203                         // Values around the smallest positive decimal value
204                         CheckDouble (1.123456789e-29);
205                         CheckDouble (1.123456789e-28);
206
207                         CheckDouble (1.1E-29, "0.000000000000000000000000000011");
208                         // This is being parsed as a decimal and rounded to 1e-28, even though it can be more accurately be represented by a double
209                         //CheckDouble (1.1E-28, "0.00000000000000000000000000011");
210                 }
211
212                 // Retry the test with different locales
213                 [Test]
214                 public void CheckNumbersCulture () 
215                 {
216                         CultureInfo old = Thread.CurrentThread.CurrentCulture;
217                         try {
218                                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en");
219                                 CheckNumbers ();
220                                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("fr");
221                                 CheckNumbers ();
222                                 Thread.CurrentThread.CurrentCulture = new CultureInfo ("de");
223                                 CheckNumbers ();
224                         } finally {
225                                 Thread.CurrentThread.CurrentCulture = old;
226                         }
227                 }
228
229                 // Convert a string to json and parse the string, then compare the result to the original value
230                 void CheckString (string str)
231                 {
232                         var json = new JsonPrimitive (str).ToString ();
233                         // Check whether the string is valid Unicode (will throw for broken surrogate pairs)
234                         new UTF8Encoding (false, true).GetBytes (json);
235                         string jvalue = (string) JsonValue.Parse (json);
236                         Assert.AreEqual (str, jvalue);
237                 }
238                 
239                 // String handling: http://tools.ietf.org/html/rfc7159#section-7
240                 [Test]
241                 public void CheckStrings () 
242                 {
243                         Assert.AreEqual ("\"test\"", new JsonPrimitive ("test").ToString ());
244                         // Handling of characters
245                         Assert.AreEqual ("\"f\"", new JsonPrimitive ('f').ToString ());
246                         Assert.AreEqual ('f', (char) JsonValue.Parse ("\"f\""));
247
248                         // Control characters with special escape sequence
249                         Assert.AreEqual ("\"\\b\\f\\n\\r\\t\"", new JsonPrimitive ("\b\f\n\r\t").ToString ());
250                         // Other characters which must be escaped
251                         Assert.AreEqual (@"""\""\\""", new JsonPrimitive ("\"\\").ToString ());
252                         // Control characters without special escape sequence
253                         for (int i = 0; i < 32; i++)
254                                 if (i != '\b' && i != '\f' && i != '\n' && i != '\r' && i != '\t')
255                                         Assert.AreEqual ("\"\\u" + i.ToString ("x04") + "\"", new JsonPrimitive ("" + (char) i).ToString ());
256
257                         // JSON does not require U+2028 and U+2029 to be escaped, but
258                         // JavaScript does require this:
259                         // http://stackoverflow.com/questions/2965293/javascript-parse-error-on-u2028-unicode-character/9168133#9168133
260                         Assert.AreEqual ("\"\\u2028\\u2029\"", new JsonPrimitive ("\u2028\u2029").ToString ());
261
262                         // '/' also does not have to be escaped, but escaping it when
263                         // preceeded by a '<' avoids problems with JSON in HTML <script> tags
264                         Assert.AreEqual ("\"<\\/\"", new JsonPrimitive ("</").ToString ());
265                         // Don't escape '/' in other cases as this makes the JSON hard to read
266                         Assert.AreEqual ("\"/bar\"", new JsonPrimitive ("/bar").ToString ());
267                         Assert.AreEqual ("\"foo/bar\"", new JsonPrimitive ("foo/bar").ToString ());
268
269                         CheckString ("test\b\f\n\r\t\"\\/</\0x");
270                         for (int i = 0; i < 65536; i++)
271                                 CheckString ("x" + ((char) i));
272
273                         // Check broken surrogate pairs
274                         CheckString ("\ud800");
275                         CheckString ("x\ud800");
276                         CheckString ("\udfff\ud800");
277                         CheckString ("\ude03\ud912");
278                         CheckString ("\uc000\ubfff");
279                         CheckString ("\udfffx");
280                         // Valid strings should not be escaped:
281                         Assert.AreEqual ("\"\ud7ff\"", new JsonPrimitive ("\ud7ff").ToString ());
282                         Assert.AreEqual ("\"\ue000\"", new JsonPrimitive ("\ue000").ToString ());
283                         Assert.AreEqual ("\"\ud800\udc00\"", new JsonPrimitive ("\ud800\udc00").ToString ());
284                         Assert.AreEqual ("\"\ud912\ude03\"", new JsonPrimitive ("\ud912\ude03").ToString ());
285                         Assert.AreEqual ("\"\udbff\udfff\"", new JsonPrimitive ("\udbff\udfff").ToString ());
286                 }
287         }
288 }
289
290 // vim: noexpandtab
291 // Local Variables:
292 // tab-width: 4
293 // c-basic-offset: 4
294 // indent-tabs-mode: t
295 // End: