X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2FSystem.Json%2FTest%2FSystem.Json%2FJsonValueTest.cs;h=baff3cc32b181579cbcbb199970fde60290d7cb4;hb=7c6607a000b6239e86d1736a1815264af55c13e2;hp=02dd106dfd8b82a5b3f3f9bf2ea71663d7aead57;hpb=905d434b4ece4075140bd551a8bef7d7097cf223;p=mono.git diff --git a/mcs/class/System.Json/Test/System.Json/JsonValueTest.cs b/mcs/class/System.Json/Test/System.Json/JsonValueTest.cs index 02dd106dfd8..baff3cc32b1 100644 --- a/mcs/class/System.Json/Test/System.Json/JsonValueTest.cs +++ b/mcs/class/System.Json/Test/System.Json/JsonValueTest.cs @@ -44,6 +44,29 @@ namespace MonoTests.System Assert.AreEqual (JsonType.Array, j.JsonType, "type"); var str = j.ToString (); Assert.AreEqual (str, "[1, 2, 3, null]"); + ((JsonArray) j).Add (null); + str = j.ToString (); + Assert.AreEqual (str, "[1, 2, 3, null, null]"); + } + + // Test that we correctly serialize JsonObject with null elements. + [Test] + public void ToStringOnJsonObjectWithNulls () { + var j = JsonValue.Load (new StringReader ("{\"a\":null,\"b\":2}")); + Assert.AreEqual (2, j.Count, "itemcount"); + Assert.AreEqual (JsonType.Object, j.JsonType, "type"); + var str = j.ToString (); + Assert.AreEqual (str, "{\"a\": null, \"b\": 2}"); + } + + [Test] + public void JsonObjectOrder () { + var obj = new JsonObject (); + obj["a"] = 1; + obj["c"] = 3; + obj["b"] = 2; + var str = obj.ToString (); + Assert.AreEqual (str, "{\"a\": 1, \"b\": 2, \"c\": 3}"); } [Test] @@ -92,6 +115,30 @@ namespace MonoTests.System Assert.AreEqual (number, jvalue); // should be exactly the same } + [Test] + public void CheckIntegers () + { + Assert.AreEqual (sbyte.MinValue, (sbyte) JsonValue.Parse (new JsonPrimitive (sbyte.MinValue).ToString ())); + Assert.AreEqual (sbyte.MaxValue, (sbyte) JsonValue.Parse (new JsonPrimitive (sbyte.MaxValue).ToString ())); + Assert.AreEqual (byte.MinValue, (byte) JsonValue.Parse (new JsonPrimitive (byte.MinValue).ToString ())); + Assert.AreEqual (byte.MaxValue, (byte) JsonValue.Parse (new JsonPrimitive (byte.MaxValue).ToString ())); + + Assert.AreEqual (short.MinValue, (short) JsonValue.Parse (new JsonPrimitive (short.MinValue).ToString ())); + Assert.AreEqual (short.MaxValue, (short) JsonValue.Parse (new JsonPrimitive (short.MaxValue).ToString ())); + Assert.AreEqual (ushort.MinValue, (ushort) JsonValue.Parse (new JsonPrimitive (ushort.MinValue).ToString ())); + Assert.AreEqual (ushort.MaxValue, (ushort) JsonValue.Parse (new JsonPrimitive (ushort.MaxValue).ToString ())); + + Assert.AreEqual (int.MinValue, (int) JsonValue.Parse (new JsonPrimitive (int.MinValue).ToString ())); + Assert.AreEqual (int.MaxValue, (int) JsonValue.Parse (new JsonPrimitive (int.MaxValue).ToString ())); + Assert.AreEqual (uint.MinValue, (uint) JsonValue.Parse (new JsonPrimitive (uint.MinValue).ToString ())); + Assert.AreEqual (uint.MaxValue, (uint) JsonValue.Parse (new JsonPrimitive (uint.MaxValue).ToString ())); + + Assert.AreEqual (long.MinValue, (long) JsonValue.Parse (new JsonPrimitive (long.MinValue).ToString ())); + Assert.AreEqual (long.MaxValue, (long) JsonValue.Parse (new JsonPrimitive (long.MaxValue).ToString ())); + Assert.AreEqual (ulong.MinValue, (ulong) JsonValue.Parse (new JsonPrimitive (ulong.MinValue).ToString ())); + Assert.AreEqual (ulong.MaxValue, (ulong) JsonValue.Parse (new JsonPrimitive (ulong.MaxValue).ToString ())); + } + [Test] public void CheckNumbers () { @@ -181,5 +228,71 @@ namespace MonoTests.System Thread.CurrentThread.CurrentCulture = old; } } + + // Convert a string to json and parse the string, then compare the result to the original value + void CheckString (string str) + { + var json = new JsonPrimitive (str).ToString (); + // Check whether the string is valid Unicode (will throw for broken surrogate pairs) + new UTF8Encoding (false, true).GetBytes (json); + string jvalue = (string) JsonValue.Parse (json); + Assert.AreEqual (str, jvalue); + } + + // String handling: http://tools.ietf.org/html/rfc7159#section-7 + [Test] + public void CheckStrings () + { + Assert.AreEqual ("\"test\"", new JsonPrimitive ("test").ToString ()); + // Handling of characters + Assert.AreEqual ("\"f\"", new JsonPrimitive ('f').ToString ()); + Assert.AreEqual ('f', (char) JsonValue.Parse ("\"f\"")); + + // Control characters with special escape sequence + Assert.AreEqual ("\"\\b\\f\\n\\r\\t\"", new JsonPrimitive ("\b\f\n\r\t").ToString ()); + // Other characters which must be escaped + Assert.AreEqual (@"""\""\\""", new JsonPrimitive ("\"\\").ToString ()); + // Control characters without special escape sequence + for (int i = 0; i < 32; i++) + if (i != '\b' && i != '\f' && i != '\n' && i != '\r' && i != '\t') + Assert.AreEqual ("\"\\u" + i.ToString ("x04") + "\"", new JsonPrimitive ("" + (char) i).ToString ()); + + // JSON does not require U+2028 and U+2029 to be escaped, but + // JavaScript does require this: + // http://stackoverflow.com/questions/2965293/javascript-parse-error-on-u2028-unicode-character/9168133#9168133 + Assert.AreEqual ("\"\\u2028\\u2029\"", new JsonPrimitive ("\u2028\u2029").ToString ()); + + // '/' also does not have to be escaped, but escaping it when + // preceeded by a '<' avoids problems with JSON in HTML