Skip to content

[API Proposal]: System.Text.Json serialization callback context #59892

Closed

Description

@IdkGoodName

Background and motivation

Newtonsoft.Json allows you to pass context to its JSON serializer via serializer settings that can later be retrieved in the callbacks. This works well enough for objects that need a specific property (like parent clients, status, etc.) to be set as passed value and it works at any depth. Unfortunately, there isn't a way to do that with System.Text.Json apart from using custom converters, that can't have serializer settings to be passed down (and you can't pass the property value to child types that require same property set if settings aren't passed). This can make it harder to move certain projects over to System.Text.Json and if not make it worse in some instances.

API Proposal

namespace System.Text.Json.Serialization
{
    public interface IJsonOnDeserialized<T>
    {
        void OnDeserialized(SomeContextType<T> context);
    }
}

or with the breaking change:

namespace System.Text.Json.Serialization
{
    public interface IJsonOnDeserialized
    {
        void OnDeserialized(StreamingContext context);
    }
}

API Usage

JsonSerializerOptions options = new JsonSerializerOptions
{
    // ...
    Context = new SomeContextType<int>(10),
    // ... 
};
public class SomeObject : IJsonOnDeserialized<int>
{
    public int Status { get; set; }
 
    public void IJsonOnDeserialized.OnDeserialized(SomeContextType<int> context) => Status = context.Value;
    
    // ... Now we can set up methods or anything else with the received context ...
}

Risks

This could lead to breaking changes if callback interfaces wouldn't use generic types.

Activity

  1. added
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on Oct 2, 2021
  2. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Oct 2, 2021
  3. eiriktsarpalis commented on Oct 5, 2021

    @eiriktsarpalis
    Member

    This was discussed in the serialization callback API proposal, see #54528 (comment). I understand passing contexts was cut since it would be difficult to support in the current iteration of source generation.

    cc @steveharter

  4. added
    api-needs-workAPI needs work before it is approved, it is NOT ready for implementation
    and removed
    untriagedNew issue has not been triaged by the area owner
    on Oct 11, 2021
  5. added this to the 7.0.0 milestone on Oct 11, 2021
  6. eiriktsarpalis commented on Oct 11, 2021

    @eiriktsarpalis
    Member

    This would likely need to be considered in combination with #36785.

  7. removed
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on Nov 16, 2021
  8. eiriktsarpalis commented on Jul 19, 2022

    @eiriktsarpalis
    Member

    JsonSerializer doesn't support passing user state for the moment. This is something we might consider in #71718 and will be implemented in tandem with #63795. Closing as duplicate of #71718.

  9. ghost locked as resolved and limited conversation to collaborators on Aug 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-System.Text.Json

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions