Skip to content
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

Closed
Acerby opened this issue Jan 22, 2024 · 2 comments
Closed

Text.Json Serialization Context and Reference Handler #97315

Acerby opened this issue Jan 22, 2024 · 2 comments

Comments

@Acerby
Copy link

Acerby commented Jan 22, 2024

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

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jan 22, 2024
@ghost
Copy link

ghost commented Jan 22, 2024

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

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 now 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

Author: Acerby
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@eiriktsarpalis
Copy link
Member

Essentially a duplicate of #51715. We won't be able to support this unless we expose methods on JsonConverter that accept serialization state objects; this is tracked by #63795

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jan 22, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Feb 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants