You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Even though we have covered the blocking issue of not being able to (de)serialize reference loops (#30820, #29900) by adding ReferenceHandler.Preserve to S.T.Json, there is many asks for adding an option equivalent to Json.NET's ReferenceLoopHandling.Ignore.
Motivations for doing this are:
ReferenceHandler.Preserve may be too cumbersome and increases payload size.
ReferenceHandler.Preserve creates JSON incomprehensible by serializers other than S.T.Json and Json.NET.
Proposed API
namespace System.Text.Json.Serialization
{
public abstract partial class ReferenceHandler
{
public static ReferenceHandler Preserve { get; }
+ public static ReferenceHandler IgnoreCycle { get; }
}
}
Usage Examples
classNode{publicstringDescription{get;set;}publicNodeNext{get;set;}}voidTest(){varnode=newNode{Description="Node 1"};node.Next=node;varopts=newJsonSerializerOptions{ReferenceHandler=ReferenceHandler.IgnoreCycle};stringjson=JsonSerializer.Serialize(node,opts);Console.WriteLine(json);// Prints: {"Description":"Node 1"}. // Note property "Next" being ignored due to cycle detection.}
Alternative Designs
This new API is being added to ReferenceHandler class since this can be considered as an alternative to deal with references that is more isolated to the circularity problem during serialization.
One downside is that users are uanble to implement their own ReferenceHandler and cannot make their own "Ignore Cycle" handler. The discrimination between preserve and ignore would occur with an internal flag.
Concerns of adding this feature (users must be aware of these problems when opting-in for it):
Unable to round-trip data. the JSON may differ depending on the order of serialization (e.g: object properties and dictionary elements enumeration is non-deterministic).
Background and Motivation
Even though we have covered the blocking issue of not being able to (de)serialize reference loops (#30820, #29900) by adding
ReferenceHandler.Preserveto S.T.Json, there is many asks for adding an option equivalent to Json.NET'sReferenceLoopHandling.Ignore.Motivations for doing this are:
ReferenceHandler.Preservemay be too cumbersome and increases payload size.ReferenceHandler.Preservecreates JSON incomprehensible by serializers other than S.T.Json and Json.NET.Proposed API
namespace System.Text.Json.Serialization { public abstract partial class ReferenceHandler { public static ReferenceHandler Preserve { get; } + public static ReferenceHandler IgnoreCycle { get; } } }Usage Examples
Alternative Designs
This new API is being added to
ReferenceHandlerclass since this can be considered as an alternative to deal with references that is more isolated to the circularity problem during serialization.Comparison with Newtonsoft.Json
Comparison with existing ReferenceHandler.Preserve setting in S.T.Json
Risks
One downside is that users are uanble to implement their own
ReferenceHandlerand cannot make their own "Ignore Cycle" handler. The discrimination between preserve and ignore would occur with an internal flag.Concerns of adding this feature (users must be aware of these problems when opting-in for it):