Exemple #1
0
        void TestReadWrite()
        {
            string        s = RetrieveJsonExpression();
            SLJsonNode    n = SLJsonParser.Parse(s);
            SLJsonMonitor m = n.CreateMonitor();

            // Try to read some properties
            Test.Assert(() => n["person"]["firstName"].AsString == "Jane");
            Test.Assert(() => n["person"]["lastName"].AsString == "Doe");
            Test.Assert(() => n["person"]["zipCode"].AsString == "12345");
            Test.Assert(() => !m.IsModified);

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

            Test.Assert(() => !m.IsModified);

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

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

            Test.Assert(() => n["person"]["newProperty"].NodeType == SLJsonNodeType.Missing);
            n["person"]["newProperty"].AsInt32 = 333;
            Test.Assert(() => n["person"].Count == c + 1);
            Test.Assert(() => n["person"]["newProperty"].NodeType == SLJsonNodeType.Number);
            Test.Assert(() => n["person"]["newProperty"].AsString == "333");

            // Try to delete a property
            c = n["person"].Count;
            Test.Assert(() => n["person"]["lastName"].NodeType == SLJsonNodeType.String);
            n["person"]["lastName"].Remove();
            Test.Assert(() => n["person"].Count == c - 1);
            Test.Assert(() => n["person"]["lastName"].NodeType == SLJsonNodeType.Missing);
            Test.Assert(() => n["person"]["lastName"].AsString == null);
        }
Exemple #2
0
        //------------------------------------------------------------------------

        void TestReadOnly()
        {
            string     s = RetrieveJsonExpression();
            SLJsonNode n = SLJsonParser.Parse(s);

            Test.Assert(() => n["person"]["firstName"].AsString == "Jane");
            Test.Assert(() => n["person"]["lastName"].AsString == "Doe");
            Test.Assert(() => n["person"]["zipCode"].AsString == "12345");

            Test.Assert(() => !n["person"]["street"].IsReadOnly);
            SLJsonMonitor m = n.CreateMonitor();

            Test.Assert(() => !m.IsModified);
            Test.Assert(() => !m.IsReadOnly);
            m.IsReadOnly = true;
            Test.Assert(() => n["person"]["street"].IsReadOnly);

            Test.Assert(() => n["test"]["emptyArray"].IsArray);
            Test.Assert(() => n["test"]["emptyArray"].Count == 0);
            Test.Assert(() => n["test"]["emptyObject"].IsObject);
            Test.Assert(() => n["test"]["emptyObject"].Count == 0);

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

            Test.Assert(() => n["test"]["testObject"].NodeType == SLJsonNodeType.Object);
            Test.Assert(() => n["test"]["testObject"].Count == 2);
            Test.Assert(() => n["test"]["testObject"].AsString == null);

            Test.Assert(() => n["test"]["testValueMissing__"].NodeType == SLJsonNodeType.Missing);
            Test.Assert(() => !n["test"]["testValueMissing__"].AsBoolean);
            Test.Assert(() => n["test"]["testValueMissing__"].AsInt32 == 0);
            Test.Assert(() => n["test"]["testValueMissing__"].AsString == null);

            Test.Assert(() => n["test"]["testValueNull"].NodeType == SLJsonNodeType.Null);
            Test.Assert(() => !n["test"]["testValueNull"].AsBoolean);
            Test.Assert(() => n["test"]["testValueNull"].AsInt32 == 0);
            Test.Assert(() => n["test"]["testValueNull"].AsString == null);

            Test.Assert(() => n["test"]["testValueTrue"].NodeType == SLJsonNodeType.Boolean);
            Test.Assert(() => n["test"]["testValueTrue"].AsBoolean);
            Test.Assert(() => n["test"]["testValueTrue"].AsInt32 == 0);
            Test.Assert(() => n["test"]["testValueTrue"].AsString == "true");

            Test.Assert(() => n["test"]["testValue32"].NodeType == SLJsonNodeType.Number);
            Test.Assert(() => n["test"]["testValue32"].AsInt32 == 256);
            Test.Assert(() => n["test"]["testValue32"].AsString == "+256");

            Test.Assert(() => n["test"]["testValueString1"].NodeType == SLJsonNodeType.String);
            Test.Assert(() => n["test"]["testValueString1"].AsString == "abc 'def' ghi");

            Test.Assert(() => n["test"]["testValueString2"].NodeType == SLJsonNodeType.String);
            Test.Assert(() => n["test"]["testValueString2"].AsString == "ABC 'DEF' GHI");

            Test.Assert(() => n["test"]["testValueString3"].NodeType == SLJsonNodeType.String);
            Test.Assert(() => n["test"]["testValueString3"].AsString == "First Line\r\nSecond Line\r\nThird Line\0");

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

            Test.Assert(() => !m.IsModified);
            Test.Assert(() => m.IsReadOnly);
        }