Ejemplo n.º 1
0
        public static void RunExample1()
        {
            Console.WriteLine("Example 1");
            Console.WriteLine("=========");
            Console.WriteLine();

            string jsonExpression = @"
      {
        addressBook: [
          {lastName: 'Average', firstName: 'Joe'},
          {lastName: 'Doe', firstName: 'Jane'},
          {lastName: 'Smith', firstName: 'John'}
        ]
      }";

            SLJsonNode root = SLJsonParser.Parse(jsonExpression);
            SLJsonNode book = root["addressBook"];

            if (book.IsArray)
            {
                int c = book.Count;
                for (int i = 0; i < c; i++)
                {
                    SLJsonNode entry = book[i];
                    string     ln    = entry["lastName"];
                    string     fn    = entry["firstName"];
                    Console.WriteLine(fn + " " + ln);
                }
            }

            Console.WriteLine();
        }
Ejemplo n.º 2
0
        void Check(
            string json,
            bool isNull, bool isArray, bool isObject, bool isValue, SLJsonNodeType valueType,
            bool valueBoolean, int valueInt32, long valueInt64, double valueNumber, string valueString)
        {
            SLJsonNode parsed = SLJsonParser.Parse(json);
            SLJsonNode n      = parsed["value"];

            Test.Assert(() => n.IsArray == isArray);
            Test.Assert(() => n.IsNull == isNull);
            Test.Assert(() => n.IsObject == isObject);
            Test.Assert(() => n.IsValue == isValue);

            Test.Assert(() => n.NodeType == valueType);
            Test.Assert(() => n.IsValue == (n.IsBoolean || n.IsNumber || n.IsString));
            Test.Assert(() => n.IsBoolean == (valueType == SLJsonNodeType.Boolean));
            Test.Assert(() => n.IsNumber == (valueType == SLJsonNodeType.Number));
            Test.Assert(() => n.IsString == (valueType == SLJsonNodeType.String));

            Test.Assert(() => n.AsBoolean == valueBoolean);
            Test.Assert(() => n.AsInt32 == valueInt32);
            Test.Assert(() => n.AsInt64 == valueInt64);
            Test.Assert(() => Math.Abs(n.AsDouble - valueNumber) <= 1e-7);
            Test.Assert(() => n.AsString == valueString);
        }
Ejemplo n.º 3
0
        void ParseAndSerialize(string jsonExpression, bool allowArraysAndValues, SLJsonNodeType nodeType)
        {
            SLJsonNode n = SLJsonParser.Parse(jsonExpression, allowArraysAndValues);

            Test.Assert(() => n.NodeType == nodeType);

            string s = RemoveWhitespace(jsonExpression);

            Test.Assert(() => n.AsJsonCompact == s);
        }
Ejemplo n.º 4
0
        //------------------------------------------------------------------------

        public static void RunExample2()
        {
            Console.WriteLine("Example 2");
            Console.WriteLine("=========");
            Console.WriteLine();

            string jsonExpression = RetrieveJsonExample();

            PrintNode(SLJsonParser.Parse(jsonExpression), 0);
            Console.WriteLine();
        }
Ejemplo n.º 5
0
        //------------------------------------------------------------------------

        void ParseInvalid(string jsonExpression, string expectedErrorMessage)
        {
            try
            {
                SLJsonParser.Parse(jsonExpression, true);
                Test.Assert(() => false);
            }
            catch (SLJsonException e)
            {
                Test.Assert(() => e.Message == expectedErrorMessage);
            }
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        public void Run()
        {
            Test = new UnitTestHelper();
            Test.PrintHeadline("UnitTest3 - Reflection-based Serialization");

            var o1 = new ExampleOuter()
            {
                ValueString      = "Test",
                ValueStringArray = new string[] { "A", "B,", "C" },
                ValueDoubleArray = new double[] { 2, 3.14, 10000 },
                PropertyInteger  = 27,
                PropertyDateTime = new DateTime(2017, 12, 27, 14, 30, 0),
            };

            o1.ValueObject      = new ExampleInner(2);
            o1.ValueObjectArray = new ExampleInner[] { new ExampleInner(4), new ExampleInner(6) };
            o1.ChangePrivateValue(2.345f);

            string s1 = Serialize(o1);
            //Console.WriteLine(s1);

            var          d  = new SLJsonDeserializer();
            ExampleOuter o2 = d.Deserialize <ExampleOuter>(s1);

            string s2 = Serialize(o1);

            Test.Assert(() => s1 == s2);

            SLJsonNode n1 = SLJsonParser.Parse(s1);
            SLJsonNode n2 = n1.Clone();
            SLJsonNode n3 = SLJsonParser.Parse(n2.AsJson);
            SLJsonNode n4 = SLJsonParser.Parse(n3.AsJsonCompact);

            Test.Assert(() => n1 != n2);
            CompareNodes(n1, n1);
            CompareNodes(n1, n2);
            CompareNodes(n1, n3);
            CompareNodes(n1, n4);

            //Console.WriteLine();
            Test.PrintSummary();
            Test = null;
        }
Ejemplo n.º 8
0
        //------------------------------------------------------------------------

        public static void RunExample4()
        {
            Console.WriteLine("Example 4");
            Console.WriteLine("=========");
            Console.WriteLine();

            string s = @"
      {
        options: { logging: true },

        sensors: [
          { name: 'Button', value: 1 },
          { name: 'Temperature', value: 17.5 }
        ],

        actors: [
          { name: 'Lamp', value: 0 }
        ]
      }";

            /*
             * Console.WriteLine(s.Trim());
             * Console.WriteLine();
             * //*/

            SLJsonNode n = SLJsonParser.Parse(s);

            Console.WriteLine("n.AsJsonCompact          => " + n.AsJsonCompact);
            Console.WriteLine();

            Console.WriteLine("n[\"options\"].NodeType    => " + n["options"].NodeType);
            Console.WriteLine("n[\"options\"][\"logging\"]  => " + n["options"]["logging"]);
            Console.WriteLine("n[\"sensors\"].NodeType    => " + n["sensors"].NodeType);
            Console.WriteLine("n[\"sensors\"].Count       => " + n["sensors"].Count);
            Console.WriteLine("n[\"sensors\"][0][\"name\"]  => " + n["sensors"][0]["name"]);
            Console.WriteLine("n[\"sensors\"][1][\"value\"] => " + n["sensors"][1]["value"]);
            Console.WriteLine("n[\"missing\"][33][\"foo\"]  => " + n["missing"][33]["foo"]);
            Console.WriteLine();
        }
Ejemplo n.º 9
0
        void TestSerialization()
        {
            string     s = RetrieveJsonExpression();
            SLJsonNode n = SLJsonParser.Parse(s);

            n["newProperty"]["value"].AsInt32 = 27;
            Test.Assert(() => n["newProperty"]["value"].NodeType == SLJsonNodeType.Number);
            Test.Assert(() => n["newProperty"]["value"].AsJson == "27");
            Test.Assert(() => RemoveWhitespace(n["newProperty"].AsJson) == "{\"value\":27}");

            n["newProperty"]["value"][0].AsInt32 = 100;
            n["newProperty"]["value"][3].AsInt32 = 333;
            Test.Assert(() => n["newProperty"]["value"].NodeType == SLJsonNodeType.Array);
            Test.Assert(() => n["newProperty"].AsJsonCompact == "{\"value\":[100,null,null,333]}");

            n["newProperty"]["value"].AsString = "Text";
            Test.Assert(() => n["newProperty"]["value"].NodeType == SLJsonNodeType.String);

            n["newProperty"]["value"].AsString = null;
            Test.Assert(() => n["newProperty"]["value"].NodeType == SLJsonNodeType.Null);

            Test.Assert(() => n["newProperty"]["value"].Remove());
            Test.Assert(() => n["newProperty"]["value"].NodeType == SLJsonNodeType.Missing);
        }
Ejemplo n.º 10
0
 public static SLJsonNode Parse(string jsonExpression)
 {
     return(SLJsonParser.Parse(jsonExpression, true));
 }
Ejemplo n.º 11
0
        public T Deserialize <T>(string jsonExpression) where T : new()
        {
            SLJsonNode n = SLJsonParser.Parse(jsonExpression);

            return(Deserialize <T>(n));
        }
Ejemplo n.º 12
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);
        }