예제 #1
0
        public static void ExtensionPropertyObjectValue_Empty()
        {
            // Baseline
            ClassWithExtensionPropertyAlreadyInstantiated obj = JsonSerializer.Deserialize <ClassWithExtensionPropertyAlreadyInstantiated>(@"{}");

            Assert.Equal(@"{""MyOverflow"":{}}", JsonSerializer.Serialize(obj));
        }
예제 #2
0
        public static void ExtensionPropertyObjectValue()
        {
            // Baseline
            ClassWithExtensionPropertyAlreadyInstantiated obj = JsonSerializer.Deserialize <ClassWithExtensionPropertyAlreadyInstantiated>(@"{}");

            obj.MyOverflow.Add("test", new object());
            obj.MyOverflow.Add("test1", 1);

            Assert.Equal(@"{""MyOverflow"":{""test"":{},""test1"":1}}", JsonSerializer.Serialize(obj));
        }
예제 #3
0
        public static void ExtensionPropertyObjectValue_RoundTrip()
        {
            // Baseline
            ClassWithExtensionPropertyAlreadyInstantiated obj = JsonSerializer.Deserialize <ClassWithExtensionPropertyAlreadyInstantiated>(@"{}");

            obj.MyOverflow.Add("test", new object());
            obj.MyOverflow.Add("test1", 1);
            obj.MyOverflow.Add("test2", "text");
            obj.MyOverflow.Add("test3", new DummyObj()
            {
                Prop = "ObjectProp"
            });
            obj.MyOverflow.Add("test4", new DummyStruct()
            {
                Prop = "StructProp"
            });
            obj.MyOverflow.Add("test5", new Dictionary <string, object>()
            {
                { "Key", "Value" }, { "Key1", "Value1" },
            });

            string json = JsonSerializer.Serialize(obj);
            ClassWithExtensionPropertyAlreadyInstantiated roundTripObj = JsonSerializer.Deserialize <ClassWithExtensionPropertyAlreadyInstantiated>(json);

            Assert.Equal(6, roundTripObj.MyOverflow.Count);

            Assert.IsType <JsonElement>(roundTripObj.MyOverflow["test"]);
            Assert.IsType <JsonElement>(roundTripObj.MyOverflow["test1"]);
            Assert.IsType <JsonElement>(roundTripObj.MyOverflow["test2"]);
            Assert.IsType <JsonElement>(roundTripObj.MyOverflow["test3"]);

            Assert.Equal(JsonValueKind.Object, ((JsonElement)roundTripObj.MyOverflow["test"]).ValueKind);

            Assert.Equal(JsonValueKind.Number, ((JsonElement)roundTripObj.MyOverflow["test1"]).ValueKind);
            Assert.Equal(1, ((JsonElement)roundTripObj.MyOverflow["test1"]).GetInt32());
            Assert.Equal(1, ((JsonElement)roundTripObj.MyOverflow["test1"]).GetInt64());

            Assert.Equal(JsonValueKind.String, ((JsonElement)roundTripObj.MyOverflow["test2"]).ValueKind);
            Assert.Equal("text", ((JsonElement)roundTripObj.MyOverflow["test2"]).GetString());

            Assert.Equal(JsonValueKind.Object, ((JsonElement)roundTripObj.MyOverflow["test3"]).ValueKind);
            Assert.Equal("ObjectProp", ((JsonElement)roundTripObj.MyOverflow["test3"]).GetProperty("Prop").GetString());

            Assert.Equal(JsonValueKind.Object, ((JsonElement)roundTripObj.MyOverflow["test4"]).ValueKind);
            Assert.Equal("StructProp", ((JsonElement)roundTripObj.MyOverflow["test4"]).GetProperty("Prop").GetString());

            Assert.Equal(JsonValueKind.Object, ((JsonElement)roundTripObj.MyOverflow["test5"]).ValueKind);
            Assert.Equal("Value", ((JsonElement)roundTripObj.MyOverflow["test5"]).GetProperty("Key").GetString());
            Assert.Equal("Value1", ((JsonElement)roundTripObj.MyOverflow["test5"]).GetProperty("Key1").GetString());
        }
예제 #4
0
        public static void InvalidExtensionValue()
        {
            // Baseline
            ClassWithExtensionPropertyAlreadyInstantiated obj = JsonSerializer.Parse <ClassWithExtensionPropertyAlreadyInstantiated>(@"{}");

            obj.MyOverflow.Add("test", new object());

            try
            {
                JsonSerializer.ToString(obj);
                Assert.True(false, "InvalidOperationException should have thrown.");
            }
            catch (InvalidOperationException e)
            {
                // Verify the exception contains the property name and invalid type.
                Assert.Contains("ClassWithExtensionPropertyAlreadyInstantiated.MyOverflow", e.Message);
                Assert.Contains("System.Object", e.Message);
            }
        }