Skip to content

JsonSerializer should serialize tuples by default #70352

Open

Description

@krwq

Currently:

  • tuples don't serialize by default (options.IncludeFields = true is required)
  • they don't preserve names (serialize as {"Item1":"foo","Item2":"bar"}

We should improve this behavior and figure out how to not break people potentially relying on the existing behavior.

Example possibilities for fixing this:

  • we provide converter for serialization
  • we provide options switch (i.e. TupleHandling)

Possible ways of serialization:

  • array
  • object, we need to figure out if it's possible to preserve user provided names

related: #68264

Activity

  1. ghost added
    untriaged
    on Jun 7, 2022
  2. added this to the 8.0.0 milestone on Jun 7, 2022
  3. ghost removed
    untriaged
    on Jun 7, 2022
  4. eiriktsarpalis commented on Jun 7, 2022

    @eiriktsarpalis
    Member

    One issue with the current serialization of tuples is that it doesn't handle arities > 7 well:

    var tuple = (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
    string json = JsonSerializer.Serialize(tuple, new JsonSerializerOptions { IncludeFields = true, WriteIndented = true });
    Console.WriteLine(json); 
    //{
    //  "Item1": 1,
    //  "Item2": 1,
    //  "Item3": 1,
    //  "Item4": 1,
    //  "Item5": 1,
    //  "Item6": 1,
    //  "Item7": 1,
    //  "Rest": {
    //    "Item1": 1,
    //    "Item2": 1,
    //    "Item3": 1,
    //    "Item4": 1,
    //    "Item5": 1,
    //    "Item6": 1,
    //    "Item7": 1,
    //    "Rest": {
    //      "Item1": 1,
    //      "Item2": 1
    //    }
    //  }
    //}

    We might want to consider exposing an opt-in converter that treats tuples as heterogeneous arrays:

    var options = new JsonSerializerOptions { Converters = { new JsonArrayTupleConverter() } };
    var tuple = ("foo", 42, new { Data = true });
    JsonSerializer.Serialize(tuple, options); // ["foo", 42, { "Data" : true }]

    And works with large arities as expected:

    var tuple = (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
    JsonSerializer.Serialize(tuple, options); // [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
  5. huoyaoyuan commented on Jun 7, 2022

    @huoyaoyuan
    Member

    We might want to consider exposing an opt-in converter that treats tuples as heterogeneous arrays:

    Yes, please. Some apis use array to represent tuples, and would contain different types of elements within an array.

  6. added
    api-suggestion
    wishlist
    on Sep 2, 2022
  7. modified the milestones: 8.0.0, Future on Sep 2, 2022
  8. macias commented on Aug 25, 2023

    @macias

    @krwq My guess is you are talking about ValueTuples, not Tuples actually.

    IMHO it is unfortunate that JSON serializer was released in such shape (i.e. without support for value tuples), but since it is done, changing default behaviour could be risky. I think safe feature we all could agree on, would be to provide default ValueTuple converter.

    This at least would allow to easy enabling it.

  9. chillitom commented on Jan 17, 2024

    @chillitom
    Contributor

    We might want to consider exposing an opt-in converter that treats tuples as heterogeneous arrays:

    var options = new JsonSerializerOptions { Converters = { new JsonArrayTupleConverter() } };

    Anyone have an implementation of such a JsonArrayTupleConverter?

  10. gregsdennis commented on Jan 17, 2024

    @gregsdennis
    Contributor

    @chillitom ☝️

    I've just released Json.More.Net v1.10.0 which includes a JsonArrayTupleConverter.

    var options = new JsonSerializerOptions { Converters = { JsonArrayTupleConverter.Instance } };
    var json = JsonSerializer.Serialize((1, "string"), options);
    var tuple = JsonSerializer.Deserialize<(int, string)>(json, options);

    Should work with any size tuple. Let me know if you have problems.

    image

  11. Sandeep7OnShell commented on Dec 2, 2024

    @Sandeep7OnShell

    Sadly the issue was opened more than 2 years ago and still people are waiting for some meaningful solution. @ericstj @stephentoub Any way to get some help on this?

  12. colejohnson66 commented on Oct 7, 2025

    @colejohnson66
    Contributor

    If tuples are not to be supported OOTB, that's fine. But the source generator should not act like it succeeds and write empty objects. After 3.5 years, can we at least get an analyzer warning that tuples won't be serialized without IncludeFields=true?

  13. RikkiGibson commented on Oct 7, 2025

    @RikkiGibson
    Member

    It might be reasonable to report a warning when serializing a type which has fields but no properties applicable for serialization, and IncludeFields=true is not specified. Tuples seem to fall into that category, and, perhaps if a different default behavior were adopted in the future, it could be opted out of the diagnostic at that time.

  14. 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

Labels

api-suggestionarea-System.Text.Jsonin-prwishlist

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions