Skip to content

S.T.J.JsonSerializer does not serialize value tuples #29045

Closed
dotnet/corefx
#36506

Description

@mazhou-msft

Using latest nightly build of .NET Core 3.0 and write:

string s = JsonSerializer.ToString((a: 1, b: 2));
Console.WriteLine(s);

You will get a string representing an empty object {}. I think the right behavior is to have { "Item1": 1, "Item2": 2 } and I expect it should also work for deserialization.

Activity

  1. YohDeadfall commented on Mar 29, 2019

    @YohDeadfall
    Contributor

    @ahsonkhan @karelz I would like to take it.

  2. karelz commented on Mar 29, 2019

    @karelz
    Member

    Great @YohDeadfall. I sent you collaborator invite to CoreFX repo. We recommend to swtich the repo to Not Watching, otherwise you will receive 500+ notifications per day (may be a bit overwhelming). The Not Watching setting will send you notifications for issues where you are mentioned, you are assigned or where you explicitly subscribed.
    Once you accept the collaborator invite, please ping me, I will assign the issue to you then (GH limitation, assigning to myself for now).

  3. self-assigned this
    on Mar 29, 2019
  4. YohDeadfall commented on Mar 29, 2019

    @YohDeadfall
    Contributor

    By the way, can you replace "value tuples" in the title by "structures"?

  5. karelz commented on Mar 29, 2019

    @karelz
    Member

    Well, the original issue was for "value tuples". Do you mean it could / should be generalized? If yes, I let @ahsonkhan comment on that.

  6. mazhou-msft commented on Mar 29, 2019

    @mazhou-msft
    Author

    It does work for some of the primitive structures such as DateTime, however it does not work for DateTimeOffset. They could be different issues to track.

  7. YohDeadfall commented on Mar 29, 2019

    @YohDeadfall
    Contributor

    @mazhou-msft DateTimeOffset is a different kind of structure which should be serialized as a single value. Such kind of types should be handled by value converters. I'm talking about value types which have serializable public properties and fields.

    @karelz Take a look at the expected output. There are Item1 and Item2 properties which corresponds to fields of ValueTuple<,>. So the issue isn't about serialization of value tuples with their field aliases which are a and b in the example.

  8. mazhou-msft commented on Mar 29, 2019

    @mazhou-msft
    Author

    Make sense.

    For the expected result, I would say a and b may be more natural, but it makes things harder (for those runtime TupleElementName checks etc.) back one step, Item1 and Item2 are still acceptable results as they are identitical to what Json.NET returns for value tuples.

    For the second issue regarding the DateTimeOffset, I expect at least get a JSON representing all public properties, if the value onverter is not ready yet; however I am receiving TargetInvocationException which seems a bug to me, we should fix it somewhere too.

  9. steveharter commented on Mar 29, 2019

    @steveharter
    Contributor

    DateTimeOffset support was just added.

    There is no converter yet for ValueTuple, so using this issue to track that.

  10. mazhou-msft commented on Mar 29, 2019

    @mazhou-msft
    Author

    Thanks for the info, sounds good.

  11. ahsonkhan commented on Apr 1, 2019

    @ahsonkhan
    Contributor

    Is this issue about providing struct support (i.e. value types), or is it about supporting ValueTuples specifically (which just happen to be a value type)?

  12. ahsonkhan commented on May 20, 2019

    @ahsonkhan
    Contributor

    Re-opening since it isn't fully fixed yet.

  13. buyaa-n commented on May 27, 2019

    @buyaa-n
    Contributor

    Adding a test case:

        [Fact]
        public void SerializeValueTuple()
        {
            ValueTuple<int, int, string> t = ValueTuple.Create(1, 2, "string");
            string json = JsonSerializer.ToString(t);
            Assert.Equal(@"{""Item1"": 1,""Item2"": 2,""Item3"": ""string""}", json);
            ValueTuple<int, int, string> t2 = JsonSerializer.Parse<ValueTuple<int, int, string>>(json);
    
            Assert.Equal(1, t2.Item1);
            Assert.Equal(2, t2.Item2);
            Assert.Equal("string", t2.Item3);
            }
  14. pranavkm commented on May 28, 2019

    @pranavkm
    Contributor

    I tried this out earlier today using 3.0.0-preview6-27728-02 and things are essentially the same as the original reported issue. Could we consider throwing if the serializer encounters a ValueTuple for 3.0 if there isn't enough runway to support the feature in this release? The experience is a bit frustrating since it seems like serialization succeeded, but all you have is empty objects.

  15. YohDeadfall commented on May 29, 2019

    @YohDeadfall
    Contributor

    ValueTuple has fields, not properties, and the current implementation doesn't support them. There is #876 to track progress (am working on it and will try to open a PR on this weekend).

  16. karelz commented on May 29, 2019

    @karelz
    Member

    Duplicate of #876

  17. ahsonkhan commented on Jan 9, 2020

    @ahsonkhan
    Contributor

    I mention a workaround for ValueTuples (up to 3) based on custom converters if folks need it.
    #1519 (comment)

  18. transferred this issue fromdotnet/corefxon Feb 1, 2020
  19. added this to the 3.0 milestone on Feb 1, 2020
  20. 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

Assignees

Labels

area-System.Text.Jsonenhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions