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

[API Proposal]: attribute for polymorphic json serialisation (not deserialisation) #71605

Closed
AartBluestoke opened this issue Jul 4, 2022 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json

Comments

@AartBluestoke
Copy link

AartBluestoke commented Jul 4, 2022

Background and motivation

https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-polymorphism
says "You can get polymorphic serialization for lower-level objects if you define them as type object."
and provides an example of serializing a derived WeatherForecast via:

"instead of

public class WeatherForecastWithPrevious
{
    public DateTimeOffset Date { get; set; }
    public int TemperatureCelsius { get; set; }
    public string? Summary { get; set; }
    public WeatherForecast? PreviousForecast { get; set; }
}

you should use 'object' as the type of the child:

public class WeatherForecastWithPreviousAsObject
{
    public DateTimeOffset Date { get; set; }
    public int TemperatureCelsius { get; set; }
    public string? Summary { get; set; }
    public object? PreviousForecast { get; set; } //Polymorphic serialisation of some type of forecast
}

"

The above code doesn't produce an object capable of being deserialised. If we are making people write custom deseralisers for this event, can we at least make the serialisation easy?

API Proposal

a new configuration option for jsonAttribute or jsonserializeroptions to allow for serialisation of objects as the type they actually are, not the type they are referenced as.

API Usage

public class WeatherForecastWithPrevious
{
    public DateTimeOffset Date { get; set; }
    public int TemperatureCelsius { get; set; }
    public string? Summary { get; set; }
    [JsonAttribute(PolymorphicSerialisation=True)]
    public WeatherForecast? PreviousForecast { get; set; }
}

Alternative Designs

recommend custom serialisers in this situation, not just custom deserialisers.

Risks

  • a lack of action here keeps the recommendation to have objects as property types if they might be both inhereted and serialised. This is not inline with good practice.

  • poor action here will lead people to implement polymorphic serialisation without polymorphic de-serialisation, with silent data loss. (polymorphic deserialisation into object isn't a supported

@AartBluestoke AartBluestoke added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jul 4, 2022
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jul 4, 2022
@ghost
Copy link

ghost commented Jul 4, 2022

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

Background and motivation

https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-polymorphism
says "You can get polymorphic serialization for lower-level objects if you define them as type object."
and provides an example of serializing a derived WeatherForecast via:

"instead of

public class WeatherForecastWithPrevious
{
    public DateTimeOffset Date { get; set; }
    public int TemperatureCelsius { get; set; }
    public string? Summary { get; set; }
    public WeatherForecast? PreviousForecast { get; set; }
}

you should use 'object' as the type of the child:

public class WeatherForecastWithPreviousAsObject
{
    public DateTimeOffset Date { get; set; }
    public int TemperatureCelsius { get; set; }
    public string? Summary { get; set; }
    public object? PreviousForecast { get; set; } //Polymorphic serialisation of some type of forecast
}

"

The above code doesn't produce an object capable of being deserialised. If we are making people write custom deseralisers for this event, can we at least make the serialisation easy?

API Proposal

a new configuration option for jsonAttribute or jsonserializeroptions to allow for serialisation of objects as the type they actually are, not the type they are referenced as.

API Usage

public class WeatherForecastWithPrevious
{
    public DateTimeOffset Date { get; set; }
    public int TemperatureCelsius { get; set; }
    public string? Summary { get; set; }
    [JsonAttribute(PolymorphicSerialisation=True)]
    public WeatherForecast? PreviousForecast { get; set; }
}

Alternative Designs

recommend custom serialisers in this situation, not just custom deserialisers.

Risks

  • a lack of action here has recommendations for a reasonably common situation not inline with reasonable practice (let alone best practice)

  • poor action here will lead people to implement polymorphic serialisation without polymorphic de-serialisation, with silent data loss. (polymorphic deserialisation into object isn't a supported

Author: AartBluestoke
Assignees: -
Labels:

api-suggestion, area-System.Text.Json, untriaged

Milestone: -

@Symbai
Copy link

Symbai commented Jul 4, 2022

There has been a long existing issue about that #63747 and the implementation has been completed 6 days ago #71346, the article you linked to is older.

@eiriktsarpalis
Copy link
Member

Closing as duplicate of #63747. Note that the shipped API requires explicitly opting in the supported derived types.

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jul 4, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Aug 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json
Projects
None yet
Development

No branches or pull requests

3 participants