Ejemplo n.º 1
0
        public void TestReadWrite()
        {
            string      s = RetrieveJsonExample();
            JsonNode    n = ParseAny(s);
            JsonMonitor m = n.CreateMonitor();

            // Try to read some properties
            Assert.AreEqual("Jane", n["person"]["firstName"].AsString);
            Assert.AreEqual("Doe", n["person"]["lastName"].AsString);
            Assert.AreEqual("12345", n["person"]["zipCode"].AsString);
            Assert.IsFalse(m.IsModified);

            try
            {
                m.IsReadOnly         = true;
                n["abc"]["def"][100] = 0;
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
                m.IsReadOnly = false;
            }

            Assert.IsFalse(m.IsModified);

            // Try to change an existing property
            n["person"]["firstName"].AsString = "John";
            Assert.AreEqual("John", n["person"]["firstName"].AsString);
            Assert.IsTrue(m.IsModified);

            // Try to add a new property
            int c = n["person"].Count;

            Assert.AreEqual(JsonNodeType.Missing, n["person"]["newProperty"].NodeType);
            n["person"]["newProperty"].AsInt32 = 333;
            Assert.AreEqual(c + 1, n["person"].Count);
            Assert.AreEqual(JsonNodeType.Number, n["person"]["newProperty"].NodeType);
            Assert.AreEqual("333", n["person"]["newProperty"].AsString);

            // Try to delete a property
            c = n["person"].Count;
            Assert.AreEqual(JsonNodeType.String, n["person"]["lastName"].NodeType);
            n["person"]["lastName"].Remove();
            Assert.AreEqual(c - 1, n["person"].Count);
            Assert.AreEqual(JsonNodeType.Missing, n["person"]["lastName"].NodeType);
            Assert.IsNull(n["person"]["lastName"].AsString);
        }
Ejemplo n.º 2
0
        public void TestReadOnly()
        {
            string   s = RetrieveJsonExample();
            JsonNode n = ParseAny(s);

            Assert.AreEqual("Jane", n["person"]["firstName"].AsString);
            Assert.AreEqual("Doe", n["person"]["lastName"].AsString);
            Assert.AreEqual("12345", n["person"]["zipCode"].AsString);

            Assert.IsFalse(n["person"]["street"].IsReadOnly);
            JsonMonitor m = n.CreateMonitor();

            Assert.IsFalse(m.IsModified);
            Assert.IsFalse(m.IsReadOnly);
            m.IsReadOnly = true;
            Assert.IsTrue(n["person"]["street"].IsReadOnly);

            Assert.IsTrue(n["test"]["emptyArray"].IsArray);
            Assert.AreEqual(0, n["test"]["emptyArray"].Count);
            Assert.IsTrue(n["test"]["emptyObject"].IsObject);
            Assert.AreEqual(0, n["test"]["emptyObject"].Count);

            Assert.IsTrue(n["test"]["testArray"].IsArray);
            Assert.AreEqual(4, n["test"]["testArray"].Count);
            Assert.AreEqual(10, n["test"]["testArray"][0].AsInt32);
            Assert.AreEqual(20, n["test"]["testArray"][1].AsInt32);
            Assert.AreEqual(30, n["test"]["testArray"][2].AsInt32);
            Assert.AreEqual(40, n["test"]["testArray"][3].AsInt32);
            Assert.AreEqual(0, n["test"]["testArray"][4].AsInt32); // Access to missing entry
            Assert.IsFalse(n["test"]["testArray"][4].IsValue);     // Check missing entry
            Assert.AreEqual(4, n["test"]["testArray"].Count);      // Check count again

            Assert.AreEqual(JsonNodeType.Object, n["test"]["testObject"].NodeType);
            Assert.AreEqual(2, n["test"]["testObject"].Count);
            Assert.IsNull(n["test"]["testObject"].AsString);

            Assert.AreEqual(JsonNodeType.Missing, n["test"]["testValueMissing__"].NodeType);
            Assert.IsFalse(n["test"]["testValueMissing__"].AsBoolean);
            Assert.AreEqual(0, n["test"]["testValueMissing__"].AsInt32);
            Assert.IsNull(n["test"]["testValueMissing__"].AsString);

            Assert.AreEqual(JsonNodeType.Null, n["test"]["testValueNull"].NodeType);
            Assert.IsFalse(n["test"]["testValueNull"].AsBoolean);
            Assert.AreEqual(0, n["test"]["testValueNull"].AsInt32);
            Assert.IsNull(n["test"]["testValueNull"].AsString);

            Assert.AreEqual(JsonNodeType.Boolean, n["test"]["testValueTrue"].NodeType);
            Assert.IsTrue(n["test"]["testValueTrue"].AsBoolean);
            Assert.AreEqual(1, n["test"]["testValueTrue"].AsInt32);
            Assert.AreEqual("true", n["test"]["testValueTrue"].AsString);

            Assert.AreEqual(JsonNodeType.Number, n["test"]["testValue32"].NodeType);
            Assert.AreEqual(256, n["test"]["testValue32"].AsInt32);
            Assert.AreEqual("256", n["test"]["testValue32"].AsString);

            Assert.AreEqual(JsonNodeType.String, n["test"]["testValueString1"].NodeType);
            Assert.AreEqual("abc 'def' ghi", n["test"]["testValueString1"].AsString);

            Assert.AreEqual(JsonNodeType.String, n["test"]["testValueString2"].NodeType);
            Assert.AreEqual("ABC 'DEF' GHI", n["test"]["testValueString2"].AsString);

            Assert.AreEqual(JsonNodeType.String, n["test"]["testValueString3"].NodeType);
            Assert.AreEqual("First Line\r\nSecond Line\r\nThird Line\0", n["test"]["testValueString3"].AsString);

            // .NET MF seems to work internally with zero-terminated strings. As a result the test case fails.
            if ("123\0".Length == 4)
            {
                Assert.AreEqual(@"""First Line\r\nSecond Line\r\nThird Line\u0000""", n["test"]["testValueString3"].AsJsonCompact);
            }

            Assert.IsFalse(m.IsModified);
            Assert.IsTrue(m.IsReadOnly);
        }