-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Text.Json Serialization Context and Reference Handler #97315
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsThere appears to be an fundamental issue with using JsonConvert based implementations to provide custom serialization for types, in that it there is no way to access or preserve the current serialization context. Thus if the object you are serializing refers to other objects then there is no way to ensure those child object (and their subtrees) would be serialized ( or deserialized) in the same way they would have been in you hadn't customized the intervening object. You cant for example simply fall-back to what the original implementation would have been for child references. The only option appears to be create a totally new serialization context for serializing the child objects. FYI: https://github.com/Jozkee
|
There appears to be an fundamental issue with using JsonConvert based implementations to provide custom serialization for types, in that it there is no way to access or preserve the current serialization context. Thus if the object you are serializing refers to other objects then there is no way to ensure those child object (and their subtrees) would be serialized ( or deserialized) in the same way they would have been in you hadn't customized the intervening object. You cant for example simply fall-back to what the original implementation would have been for child references. The only option appears to be create a totally new serialization context for serializing the child objects.
This limitation is particularly problematic for situations where an object is referenced in multiple places and the inbuilt reference handler logic is then used to link up these references because this mechanism is obviously context dependent, and there appears to be no way to obtain a reference to the current resolver from within the JsonConvert class. Thus this appears to fundamentally break the ability to customize serialization of mid-tree objects when using the reference handler logic.
Having some sense of the current context would also be helpful in other contexts/scenarios also. For example its not uncommon for some objects to have originally been built with references to ancestor objects, or in some cases objects outside of the immediate scope being serialized. Alternatively it might be desired to serialize an object in more than one way depending on usage scenario, again requiring a dependency on some kind of serialization context.
FYI: https://github.com/Jozkee
The text was updated successfully, but these errors were encountered: