Beispiel #1
0
        public static void CustomJsonElementConverterInExtensionProperty()
        {
            const string Json = "{\"hello\": \"world\"}";

            var options = new JsonSerializerOptions();

            options.Converters.Add(new JsonElementConverter());

            ClassWithExtensionPropertyAsJsonElement obj = JsonSerializer.Deserialize <ClassWithExtensionPropertyAsJsonElement>(Json, options);
            JsonElement overflowProp = obj.MyOverflow["hello"];

            Assert.Equal(JsonValueKind.Undefined, overflowProp.ValueKind);

            string newJson = JsonSerializer.Serialize(obj, options);

            Assert.Equal("{\"hello\":{\"Hi\":\"There\"}}", newJson);
        }
Beispiel #2
0
        public static void NullAsNullObjectOrJsonValueKindNull()
        {
            const string json = @"{""MissingProperty"":null}";

            {
                ClassWithExtensionPropertyAsObject obj = JsonSerializer.Deserialize <ClassWithExtensionPropertyAsObject>(json);

                // A null value maps to <object>, so the value is null.
                object elem = obj.MyOverflow["MissingProperty"];
                Assert.Null(elem);
            }

            {
                ClassWithExtensionPropertyAsJsonElement obj = JsonSerializer.Deserialize <ClassWithExtensionPropertyAsJsonElement>(json);

                // Since JsonElement is a struct, it treats null as JsonValueKind.Null.
                object elem = obj.MyOverflow["MissingProperty"];
                Assert.IsType <JsonElement>(elem);
                Assert.Equal(JsonValueKind.Null, ((JsonElement)elem).ValueKind);
            }
        }