Ejemplo n.º 1
0
        public void CreateJsonContent()
        {
            JsonContent a = JsonContent.CreateFrom(_TestStructure.CreateCompatibleDictionary());

            // roundtrip to json
            var json = a.ToJson();

            TestContext.Write(json);
            var b = IO.JsonContent.Parse(json);

            // roundtrip to a runtime object
            var x = a.Deserialize(typeof(_TestStructure));
            var c = JsonContent.Serialize(x);

            Assert.IsTrue(AreEqual(a, b));
            Assert.IsTrue(AreEqual(a, c));

            foreach (var dom in new[] { a, b, c })
            {
                Assert.AreEqual("me", dom.GetValue <string>("author"));
                Assert.AreEqual(17, dom.GetValue <int>("integer1"));
                Assert.AreEqual(15.3f, dom.GetValue <float>("single1"));
                Assert.AreEqual(3, dom.GetValue <int>("array1", 2));
                Assert.AreEqual(2, dom.GetValue <int>("dict2", "d", "a1"));
            }
        }
Ejemplo n.º 2
0
        public static bool AreEqual(JsonContent a, JsonContent b)
        {
            if (Object.ReferenceEquals(a.Content, b.Content))
            {
                return(true);
            }
            if (Object.ReferenceEquals(a.Content, null))
            {
                return(false);
            }
            if (Object.ReferenceEquals(b.Content, null))
            {
                return(false);
            }

            // A JsonContent ultimately represents a json block, so it seems fit to do the comparison that way.
            // also, there's the problem of floating point json writing, that can slightly change between
            // different frameworks.

            var ajson = a.ToJson();
            var bjson = b.ToJson();

            if (ajson != bjson)
            {
                return(false);
            }

            Assert.IsTrue(JsonContent.AreEqualByContent(a, b, 0.0001f));

            return(true);
        }
Ejemplo n.º 3
0
        public void TestFloatingPointJsonRoundtrip()
        {
            float value = 1.1f; // serialized by system.text.json as 1.1000002f

            var valueTxt = value.ToString("G9", System.Globalization.CultureInfo.InvariantCulture);

            var dict = new Dictionary <string, Object>();

            dict["value"] = value;

            JsonContent a = JsonContent.CreateFrom(dict);

            // roundtrip to json
            var json = a.ToJson();

            TestContext.Write(json);
            var b = IO.JsonContent.Parse(json);

            Assert.IsTrue(AreEqual(a, b));
        }