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

Accessing the property information changes the order of System.Text.Json serialization. #33854

Closed
neelabo opened this issue Mar 20, 2020 · 5 comments

Comments

@neelabo
Copy link

neelabo commented Mar 20, 2020

If you access the property information before serialization, the property will be output at the top.

I hope the same serialized result is output from the same object regardless of the property access.

Sample code

public class OrderSample
{
    public int A { get; set; }
    public int B { get; set; }
    public int C { get; set; }

    public void Test()
    {
        var propertyInfo = this.GetType().GetProperty(nameof(C));

        for (int turn = 1; turn <= 3; turn++)
        {
            Console.WriteLine(JsonSerializer.Serialize(this, new JsonSerializerOptions()));
        }
    }
}

Result: .NET Core 3.1

The accessed property "C" is placed at the top.

{"C":0,"A":0,"B":0}
{"C":0,"A":0,"B":0}
{"C":0,"A":0,"B":0}

Result: .NET Framework 4.8 + System.Text.Json 4.7.1

In the .NET Framework, the order changes from the second time.

{"A":0,"B":0,"C":0}
{"C":0,"A":0,"B":0}
{"C":0,"A":0,"B":0}
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Mar 20, 2020
@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Mar 21, 2020
@layomia layomia added this to the 5.0 milestone Mar 21, 2020
@layomia
Copy link
Contributor

layomia commented Mar 21, 2020

Reflection order is not guaranteed on serialization. The serialized content will always be the same.

@layomia layomia closed this as completed Mar 21, 2020
@neelabo
Copy link
Author

neelabo commented Mar 21, 2020

It will be used to save application settings, but there is a problem if the order is not guaranteed.

  • Diff cannot be obtained, making it difficult to find differences in settings.
  • Not suitable for version control.
  • The deserialization order is changed.

Is JsonSerializer.Serialize not suitable for this purpose?

@neelabo
Copy link
Author

neelabo commented Mar 21, 2020

To fix the order, we propose the following enhancements:

  • Extend so that alphabetical sort flag or sort function (IComparer<PropertyInfo>) can be set in JsonSerializerOptions. This option only affects the output.

@layomia
Copy link
Contributor

layomia commented Mar 23, 2020

@neelabo this is a valid scenario - we have a tracking issue for being able to control the serialization order #728.

@neelabo
Copy link
Author

neelabo commented Mar 24, 2020

I will look closely at that issue.
Thank.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants