-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
System.Text.Json.JsonSerializer doesn't deserialize into existing sub-objects #31457
Comments
Looks like a duplicate of #29743 ? |
@Symbai and @layomia - I want to point out that this is not the same as internal/private setters. The desired behavior is that the existing sub-object be deserialized into (the sub-object is obtained from the getter), instead of requiring that the deserializer create a new sub-object, deserialize into it, and then pass it into the parent object setter (whether private or internal, non-existent should also work). Doing it this way allows developers to set default values before deserializing, to handle values that aren't present in the JSON. It also allows developers to create less mutable objects, and use non-nullable reference types for properties that should never be null. |
@johncrim I see. The serializer's behavior is to replace object instances. We could add an option such as Json.NET's object creation handling feature: https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_ObjectCreationHandling.htm. |
Closing as dup of #30258 which considers similar semantics for collections. |
@layomia - sub-objects are not the same as objects in collections. For consistency, both should be implemented the same way and at the same time. In other words, it would be inconsistent to support using an existing collection (and not replacing it), but not using an existing sub-object (eg it gets replaced). (IMO) this issue should be re-opened, and both #30258 and #31457 should be handled in the same fix. Separate tests should be added for both use-cases. |
I agree with @johncrim I also ran into this issue as I want it to use existing sub objects. I think the object creation handling of json.net would solve my issue. As long as it allows me to bypass the inability to deserialize interfaces. I have constructors setting the internal objects concrete type and read-only interfaces for subobjects. This technique was necessary for me to handle closed source codegen problems. |
If a parent object doesn't contain setters for all sub-object properties, the sub-objects are not deserialized. This is inconsistent with other JSON serializers, and seems like a major oversight - current behavior requires a large amount of mutability in the deserialized object model.
It's also a normal pattern to provide sub-objects with default values before deserialization.
For example, in this class, the
SubObject1
property won't be deserialized, and no exception is thrown.Full test project:
The text was updated successfully, but these errors were encountered: