Skip to content

[System.Text.Json] JsonExtensionData should allow non-JsonElement types during serialization #29774

Closed
dotnet/corefx
#38306

Description

@pranavkm

Consider the model

public class TestModel
{
    public string Name { get; set; } = "Test"

    public Dictionary<string, object> Extensions { get; set; } = new Dictionary<string, object>()
    {
       ["foo"] = 1,
     }
}

This serializes correctly. If I annotate the Extensions property with JsonExtensionDataAttribute, serialization fails with:

The data extension property 'WebApplication1.Test2Controller+TestModel.Extensions' cannot contain dictionary values of type 'System.Int32'. Dictionary values must be of type JsonElement.

It's not entirely clear why serialization requires the type be an instance of JsonElement. Additionally, it's not particularly trivial to produce JsonElement instances to put inside a dictionary.

Could this constraint be removed so that it's treated the same as a regular dictionary for the purposes of serialization?

Activity

  1. pranavkm commented on Jun 5, 2019

    @pranavkm
    ContributorAuthor

    Note that in the one use case that MVC uses this feature, it stores arbitrary content (strings or numbers) in the JsonExtensionData backed dictionary.

  2. pranavkm commented on Jun 5, 2019

    @pranavkm
    ContributorAuthor
  3. MarcoRossignoli commented on Jun 5, 2019

    @MarcoRossignoli
    Member

    Guys I'm working on this if you can take a look at dotnet/corefx#38262 and let me know what you think I could bring it to the end.

  4. steveharter commented on Jun 5, 2019

    @steveharter
    Contributor

    The request is only for polymorphic serialize, which for round-tripping on subsequent deserialize will create JsonElement.

    This means on deserialize the values should always be JsonElement, and we shouldn't try to convert to int, string, etc.

    cc @MarcoRossignoli

  5. transferred this issue fromdotnet/corefxon Feb 1, 2020
  6. added this to the 3.0 milestone on Feb 1, 2020
  7. ghost locked as resolved and limited conversation to collaborators on Dec 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-System.Text.Jsonblocking

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions