예제 #1
0
        public void ReadWriteStandardQuotation()
        {
            // Arrange
            const string sIn          = "{\"a\":[[{\"b\":[{\"c\":[{\"d\":0},{\"e\":[{\"d\":42},{\"f\":[{\"g\":[{\"h\":[{\"g\":[\"i\"]}]}]}]}]}]}],\"j\":{\"k\":[\"l\"]}}]]}";
            const string sExpectedOut = sIn;

            // Act
            var    root = new JsonTree.Node(sIn);
            string sOut = root.ToJson();

            Assert.AreEqual(sOut, sExpectedOut);
        }
예제 #2
0
        public void DeserializerReadsSerializerOutput()
        {
            // Arrange
            const string sIn  = "{ a: 'b\\'c b\"c', b: 1, c: true, d: false, e: 1.11, f: ['g', 'h'], i:{ j:'k', l: 2, m: [1, 2], n:{o:3, p:4, q:{r:'s',t:[1,2,3], u:[{v:1,w:2},{x:3,y:4}]}} } }";
            var          root = new JsonTree.Node(sIn);

            // Act
            string sOut = root.ToJson();

            // Assert
            var check = new JsonTree.Node(sOut);

            Assert.IsNotNull(check);
        }
예제 #3
0
        public void Structures()
        {
            // Arrange
            const string sIn = "{ a: 'a', b: [ 'b0', 'b1' ] }";

            // Act
            var root = new JsonTree.Node(sIn);

            // Assert
            Assert.AreEqual(root.Object["b"].Array[0].String, "b0");
            Assert.AreEqual(root.Dictionary["b"].List[0].String, "b0");
            Assert.AreEqual(root.AsObject["b"].AsArray[0].String, "b0");
            Assert.AreEqual(root.AsDictionary["b"].AsList[0].String, "b0");
        }
예제 #4
0
        static void Example()
        {
            const string data = "[ \"first\", { \"aString\": \"Hello World\", \"aNumber\": 42 } ]";
            var          node = new JsonTree.Node(data);

            // Deserialize
            var aNumber = node.List[1].Dictionary["aNumber"].Int; // C# notation
            // or
            var aNumber2 = node.Array[1].Object["aNumber"].Int;   // Javascript notation

            // Serialize
            var serialized = node.ToJson();

            // Serialize formatted
            var formatted = node.ToJson(new JsonTree.Serializer.Options(bFormatted: true, bWrapped: true));
        }
예제 #5
0
        public void Skalars()
        {
            // Arrange
            const string sIn = "{ a: '41', b: 42, c: true, d: 3.14 }";

            // Act
            var root = new JsonTree.Node(sIn);

            // Assert
            Assert.AreEqual(root.AsDictionary["a"].String, "41");
            Assert.AreEqual(root.AsDictionary["a"].AsString, "41");
            Assert.AreEqual(root.AsDictionary["b"].Int, 42);
            Assert.AreEqual(root.AsDictionary["b"].AsInt, 42);
            Assert.AreEqual(root.AsDictionary["c"].Bool, true);
            Assert.AreEqual(root.AsDictionary["c"].AsBool, true);
            Assert.AreEqual(root.AsDictionary["d"].Float, 3.14);
            Assert.AreEqual(root.AsDictionary["d"].AsFloat, 3.14);
        }
예제 #6
0
        static void Main(string[] args)
        {
            Console.WriteLine("Goal: inspect values in JSON strings with single line expressions and simple CLR objects without using foreach/if to extract values from JSON");
            Console.WriteLine("");

            const string data = "[ \"first\", { \"aString\": \"Hello World\", \"aNumber\": 42 } ]";

            Console.WriteLine("Given the JSON: " + data);

            var json = new JsonTree.Node(data);

            Console.WriteLine("The 42 is the value of the aNumber-key in the map which is the second element of a list. JSON arrays/objects become C# lists/dictionaries.");
            Console.WriteLine("");

            Console.WriteLine("Get to the value quickly:");
            var aNumber = json.List[1].Dictionary["aNumber"].Int;

            Console.WriteLine("  json.List[1].Dictionary[\"aNumber\"].Int = " + aNumber);
            Console.WriteLine("");

            Console.WriteLine("The same in JavaScript notation:");
            var aNumber2 = json.Array[1].Object["aNumber"].Int;

            Console.WriteLine("  json.Array[1].Object[\"aNumber\"].Int = " + aNumber2);
            Console.WriteLine("");

            var serialized = json.ToJson();

            Console.WriteLine("Serialized again: " + serialized);
            Console.WriteLine("");

            Console.WriteLine("Serialized with formatting:");
            var formatted = json.ToJson(new JsonTree.Serializer.Options(bFormatted: true, bWrapped: true));

            Console.WriteLine(formatted);

            var rl = Console.ReadLine();

            Example();
        }