コード例 #1
0
        public void ReadJsonWithInnerObjectTest()
        {
            const string             json   = "{\"properties\":{\"test1\":\"value1\",\"test2\": { \"innertest1\":\"innervalue1\" }}}";
            AttributesTableConverter target = new AttributesTableConverter();

            using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))
            {
                JsonSerializer serializer = new JsonSerializer();

                // read start object token and prepare the next token
                reader.Read();
                reader.Read();
                AttributesTable result =
                    (AttributesTable)
                    target.ReadJson(reader, typeof(AttributesTable), new AttributesTable(), serializer);
                Assert.IsFalse(reader.Read()); // read the end of object and ensure there are no more tokens available
                Assert.IsNotNull(result);
                Assert.AreEqual(2, result.Count);
                Assert.AreEqual("value1", result["test1"]);
                Assert.IsNotNull(result["test2"]);
                Assert.IsInstanceOf <IAttributesTable>(result["test2"]);
                IAttributesTable inner = (IAttributesTable)result["test2"];
                Assert.AreEqual(1, inner.Count);
                Assert.AreEqual("innervalue1", inner["innertest1"]);
            }
        }
コード例 #2
0
        public void ReadJsonWithArrayWithTwoObjectsTest()
        {
            const string             json   = "{\"test1\":\"value1\",\"test2\": [{ \"innertest1\":\"innervalue1\" }, { \"innertest2\":\"innervalue2\", \"innertest3\":\"innervalue3\"}]}}";
            AttributesTableConverter target = new AttributesTableConverter();

            using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))
            {
                JsonSerializer serializer = new JsonSerializer();

                // read start object token and prepare the next token
                reader.Read();
                AttributesTable result =
                    (AttributesTable)
                    target.ReadJson(reader, typeof(AttributesTable), new AttributesTable(), serializer);
                Assert.IsNotNull(result);
                Assert.AreEqual(2, result.Count);
                Assert.AreEqual("value1", result["test1"]);
                Assert.IsNotNull(result["test2"]);
                Assert.IsInstanceOf <IList <object> >(result["test2"]);
                IList <object> list = (IList <object>)result["test2"];
                Assert.IsNotEmpty(list);
                Assert.AreEqual(2, list.Count);
                Assert.IsInstanceOf <IAttributesTable>(list[0]);
                Assert.IsInstanceOf <IAttributesTable>(list[1]);
                IAttributesTable first = (IAttributesTable)list[0];
                Assert.AreEqual(1, first.Count);
                Assert.AreEqual("innervalue1", first["innertest1"]);
                IAttributesTable second = (IAttributesTable)list[1];
                Assert.AreEqual(2, second.Count);
                Assert.AreEqual("innervalue2", second["innertest2"]);
                Assert.AreEqual("innervalue3", second["innertest3"]);
            }
        }
        public void ReadJsonWithArrayTest()
        {
            const string json   = "{\"test1\":\"value1\",\"test2\": [{ \"innertest1\":\"innervalue1\" }]}}";
            var          target = new AttributesTableConverter();

            using (var reader = new JsonTextReader(new StringReader(json)))
            {
                var serializer = new JsonSerializer();

                // read start object token and prepare the next token
                reader.Read();
                var result =
                    (AttributesTable)
                    target.ReadJson(reader, typeof(AttributesTable), new AttributesTable(), serializer);
                Assert.IsNotNull(result);
                Assert.AreEqual(2, result.Count);
                Assert.AreEqual("value1", result["test1"]);
                Assert.IsNotNull(result["test2"]);
                Assert.IsInstanceOf <IList <object> >(result["test2"]);
                var list = (IList <object>)result["test2"];
                Assert.IsNotEmpty(list);
                Assert.AreEqual(1, list.Count);
                Assert.IsInstanceOf <IAttributesTable>(list[0]);
                var inner = (IAttributesTable)list[0];
                Assert.AreEqual(1, inner.Count);
                Assert.AreEqual("innervalue1", inner["innertest1"]);
            }
        }
コード例 #4
0
        public void ReadJsonTest()
        {
            const string             json   = "{\"test1\":\"value1\",\"test2\":\"value2\"}}";
            AttributesTableConverter target = new AttributesTableConverter();

            using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))
            {
                JsonSerializer serializer = new JsonSerializer();

                // read start object token and prepare the next token
                reader.Read();
                AttributesTable result =
                    (AttributesTable)
                    target.ReadJson(reader, typeof(AttributesTable), new AttributesTable(), serializer);
                Assert.IsNotNull(result);
                Assert.AreEqual(2, result.Count);
                Assert.AreEqual("value1", result["test1"]);
                Assert.AreEqual("value2", result["test2"]);
            }
        }