public static void ExtensionPropertyAsObject() { string json = @"{""MyIntMissing"":2}"; ClassWithExtensionPropertyAsObject obj = JsonSerializer.Deserialize <ClassWithExtensionPropertyAsObject>(json); Assert.IsType <JsonElement>(obj.MyOverflow["MyIntMissing"]); Assert.Equal(2, ((JsonElement)obj.MyOverflow["MyIntMissing"]).GetInt32()); }
public static void ExtensionPropertyObjectValue_SameAsExtensionPropertyName() { const string json = @"{""MyOverflow"":{""Key1"":""V""}}"; // Deserializing directly into the overflow is not supported by design. ClassWithExtensionPropertyAsObject obj = JsonSerializer.Deserialize <ClassWithExtensionPropertyAsObject>(json); // The JSON is treated as normal overflow. Assert.NotNull(obj.MyOverflow["MyOverflow"]); Assert.Equal(json, JsonSerializer.Serialize(obj)); }
public static void CustomObjectConverterInExtensionProperty() { const string Json = "{\"hello\": \"world\"}"; var options = new JsonSerializerOptions(); options.Converters.Add(new JsonObjectConverter()); ClassWithExtensionPropertyAsObject obj = JsonSerializer.Deserialize <ClassWithExtensionPropertyAsObject>(Json, options); object overflowProp = obj.MyOverflow["hello"]; Assert.IsType <string>(overflowProp); Assert.Equal("world!!!", ((string)overflowProp)); string newJson = JsonSerializer.Serialize(obj, options); Assert.Equal("{\"hello\":\"world!!!\"}", newJson); }
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); } }