Skip to content

System.Text.Json doesn't serialize tuples correctly #68264

Closed

Description

@TDroogers

Describe the bug

System.Text.Json serializes tuples as {}

Expected Behavior

Tuples being serialized and deserialized correctly

Steps To Reproduce

Console.WriteLine(JsonSerializer.Serialize(("el0", "el1"))); // output: {}

EDIT (@krwq): simplified repro to just essentials

Activity

  1. javiercn commented on Apr 20, 2022

    @javiercn
    Member

    @TDroogers thanks for contacting us.

    I don't think this is Blazor specific, I believe it is likely an issue of serializing and deserializing tuples. I would suggest you replace the tuple with a proper type and see if you run into issues in that case.

  2. TDroogers commented on Apr 20, 2022

    @TDroogers
    Author

    It works fine with public List<KeyValuePair<string, string>> Claims { get; set; }

  3. javiercn commented on Apr 20, 2022

    @javiercn
    Member

    @TDroogers that is then very likely because of the way the tuple is serialized.

    I would suggest a couple of things.

    • Try deserializing the values from a command line application and see if that works (it likely won't).
    • Check the serialized JSON in the network tab to make sure it has the shape you expect (it likely won't have the tuple names applied).

    This is very likely a dupe of #29045 and #876

  4. javiercn commented on Apr 20, 2022

    @javiercn
    Member

    @TDroogers let us know if you get it to work in a command-line app but still have issues with Blazor webassembly.

  5. TDroogers commented on Apr 20, 2022

    @TDroogers
    Author

    Those looks indeed like the same issue, but on an eol .net version

    It fails on the serialization side, it has indeed nothing to do with blazor.

    xUnit test

    [Fact]
        public void SerializeClaimsTest()
        {
            var loginResponse = new LoginLocalUserResponse
            {
                UserName = MockPubbleApiClientResponse.UserResponse1.User.UserName,
                Roles = new List<UserRole> {UserRole.Administrateur, UserRole.hidepagesuser}
            };
            var test = AuthController.GetClaimsList(MockPubbleApiClientResponse.DataDb, loginResponse);
            var serialized = JsonSerializer.Serialize(test.Select(claim => (claim.Type, claim.Value)).ToList());
            Assert.NotNull(serialized); //output: [{},{},{},{},{}]
        }
    
  6. transferred this issue fromdotnet/aspnetcoreon Apr 20, 2022
  7. ghost added
    untriaged
    on Apr 20, 2022
  8. javiercn commented on Apr 20, 2022

    @javiercn
    Member

    @TDroogers I've transferred this issue to the runtime repo so that they can give you a definite answer here.

  9. 11 remaining items

  10. added
    untriaged
    on Jun 7, 2022
  11. deleted a comment from on Jun 7, 2022
  12. changed the title [-]blazor wasm List<(string Key, string Value)> from server to client values lost[/-] [+]System.Text.Json doesn't serialize tuples correctly[/+] on Jun 7, 2022
  13. krwq commented on Jun 7, 2022

    @krwq
    Member

    @TDroogers I went ahead and modified issue description to simplify it to the core of the issue

  14. added this to the 7.0.0 milestone on Jun 7, 2022
  15. ghost removed
    untriaged
    on Jun 7, 2022
  16. krwq commented on Jun 7, 2022

    @krwq
    Member

    Setting milestone as 7.0.0 to consider fixing in this release or providing workable workaround although we cannot guarantee this will get fixed in this release.

  17. krwq commented on Jun 7, 2022

    @krwq
    Member

    Just found out this is by design since tuples don't have any public properties. The way you can do this is:

    var o = new JsonSerializerOptions { IncludeFields = true };
    Console.WriteLine(JsonSerializer.Serialize(("el0", "el1", "fff"), o));
  18. krwq commented on Jun 7, 2022

    @krwq
    Member

    I've created #70352 to track improvements in this area for vNext

  19. TDroogers commented on Jun 7, 2022

    @TDroogers
    Author

    Thanks

  20. ghost locked as resolved and limited conversation to collaborators on Jul 7, 2022
  21. added
    in-pr
    on Oct 20, 2025
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

    area-System.Text.Jsonbugin-pr

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions