예제 #1
0
        public void ArrayOFClassOfClassWithArray()
        {
            var twa = new ThingWithArray();

            twa.stuff[0] = 123.0f;
            twa.stuff[1] = 456.0f;
            twa.stuff[2] = 789.0f;
            ThingWithClass twc = new ThingWithClass();

            twc.inner = twa;
            ThingWithClass[] arr = new ThingWithClass[2];
            arr[0] = twc;
            arr[1] = twc;
            string json     = JsonUtil.ToJson(arr);
            string expected = "[{\"inner\":{\"stuff\":[123,456,789]}},{\"inner\":{\"stuff\":[123,456,789]}}]";

            Assert.AreEqual(expected, json);

            object temp = JsonUtil.ToObject <object>(json);

            Assert.AreEqual(typeof(List <object>), temp.GetType());
            string json2 = JsonUtil.ToJson(temp);

            Assert.AreEqual(json, json2);

            ThingWithClass[] n = JsonUtil.ToObject <ThingWithClass[]>(json2);
            Assert.AreEqual(n.Length, arr.Length);
            Assert.AreEqual(n[0].inner.stuff[0], arr[0].inner.stuff[0]);
            Assert.AreEqual(n[0].inner.stuff[1], arr[0].inner.stuff[1]);
            Assert.AreEqual(n[0].inner.stuff[2], arr[0].inner.stuff[2]);
            Assert.AreEqual(n[1].inner.stuff[0], arr[1].inner.stuff[0]);
            Assert.AreEqual(n[1].inner.stuff[1], arr[1].inner.stuff[1]);
            Assert.AreEqual(n[1].inner.stuff[2], arr[1].inner.stuff[2]);
        }
        public void AJson_JsonBuilding_CreatingArraysNotList()
        {
            var twa_input = new ThingWithArray()
            {
                Names = new[]
                {
                    "Bob",
                    "Joe",
                    "McFartpantz"
                }
            };

            Json json = JsonHelper.BuildJsonForObject(twa_input);

            Assert.IsNotNull(json);
            Assert.IsFalse(json.HasErrors, "Json parse errors:\n" + String.Join("\n\t", json.Errors));

            var twa_output = JsonHelper.BuildObjectForJson <ThingWithArray>(json);

            Assert.IsNotNull(twa_output);
            Assert.IsInstanceOfType(twa_output, typeof(ThingWithArray));
            Assert.IsNotNull(twa_output.Names);

            Assert.AreEqual(3, twa_output.Names.Length);
            Assert.AreEqual("Bob", twa_output.Names[0]);
            Assert.AreEqual("Joe", twa_output.Names[1]);
            Assert.AreEqual("McFartpantz", twa_output.Names[2]);
        }