2 // JsonValueTest.cs: Tests for JSonValue
4 // Copyright 2011 Xamarin, Inc.
14 using System.Globalization;
15 using System.Threading;
17 namespace MonoTests.System
20 public class JsonValueTests {
21 // Tests that a trailing comma is allowed in dictionary definitions
23 public void LoadWithTrailingComma ()
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");
30 JsonValue.Parse ("[{ \"a\": \"b\",}]");
34 public void LoadWithTrailingComma2 ()
36 JsonValue.Parse ("[{ \"a\": \"b\",}]");
39 // Test that we correctly serialize JsonArray with null elements.
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]");
49 // Test that we correctly serialize JsonObject with null elements.
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}");
60 public void JsonObjectOrder () {
61 var obj = new JsonObject ();
65 var str = obj.ToString ();
66 Assert.AreEqual (str, "{\"a\": 1, \"b\": 2, \"c\": 3}");
70 public void QuoteEscapeBug_20869 ()
72 Assert.AreEqual ((new JsonPrimitive ("\"\"")).ToString (), "\"\\\"\\\"\"");
75 void ExpectError (string s)
79 Assert.Fail ("Expected ArgumentException for `" + s + "'");
80 } catch (ArgumentException) {
84 // Test whether an exception is thrown for invalid JSON
86 public void CheckErrors ()
96 ExpectError (@"[1,]");
98 //ExpectError (@"{""a"":1,}"); // Not valid JSON, allowed anyway
101 // Parse a json string and compare to the expected value
102 void CheckDouble (double expected, string json)
104 double jvalue = (double) JsonValue.Parse (json);
105 Assert.AreEqual (expected, jvalue);
108 // Convert a number to json and parse the string, then compare the result to the original value
109 void CheckDouble (double number)
111 double jvalue = (double) JsonValue.Parse (new JsonPrimitive (number).ToString ());
112 Assert.AreEqual (number, jvalue); // should be exactly the same
116 public void CheckIntegers ()
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 ()));
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 ()));
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 ()));
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 ()));
140 public void CheckNumbers ()
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");
155 CheckDouble (1, "\"1\"");
156 CheckDouble (1.1, "\"1.1\"");
157 CheckDouble (-1, "\"-1\"");
158 CheckDouble (-1.1, "\"-1.1\"");
160 CheckDouble (double.NaN, "\"NaN\"");
161 CheckDouble (double.PositiveInfinity, "\"Infinity\"");
162 CheckDouble (double.NegativeInfinity, "\"-Infinity\"");
165 ExpectError ("Infinity");
166 ExpectError ("-Infinity");
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 ());
178 Assert.AreEqual ("1E-30", JsonValue.Parse ("1e-30").ToString ());
179 Assert.AreEqual ("1E+30", JsonValue.Parse ("1e+30").ToString ());
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);
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);
203 // Values around the smallest positive decimal value
204 CheckDouble (1.123456789e-29);
205 CheckDouble (1.123456789e-28);
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");
212 // Retry the test with different locales
214 public void CheckNumbersCulture ()
216 CultureInfo old = Thread.CurrentThread.CurrentCulture;
218 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en");
220 Thread.CurrentThread.CurrentCulture = new CultureInfo ("fr");
222 Thread.CurrentThread.CurrentCulture = new CultureInfo ("de");
225 Thread.CurrentThread.CurrentCulture = old;
229 // Convert a string to json and parse the string, then compare the result to the original value
230 void CheckString (string str)
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);
239 // String handling: http://tools.ietf.org/html/rfc7159#section-7
241 public void CheckStrings ()
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\""));
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 ());
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 ());
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 ());
269 CheckString ("test\b\f\n\r\t\"\\/</\0x");
270 for (int i = 0; i < 65536; i++)
271 CheckString ("x" + ((char) i));
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 ());
294 // indent-tabs-mode: t