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

System.Text.Json.Serialization Order of serialized properties #1085

Closed
Jogge opened this issue Dec 20, 2019 · 4 comments
Closed

System.Text.Json.Serialization Order of serialized properties #1085

Jogge opened this issue Dec 20, 2019 · 4 comments
Labels
area-System.Text.Json enhancement Product code improvement that does NOT require public API changes/additions
Milestone

Comments

@Jogge
Copy link

Jogge commented Dec 20, 2019

I'm missing an attribute for setting the order of the element in the JSON output.

Example:

I want the @SEGMENT to be the first element in the serialized JSON output of MyClass:

public abstract class MyBase
{
    [JsonPropertyName("@SEGMENT")]
    public virtual string Segment { get; set; } = "1";

    public int ID { get; set; }
}

public class MyClass : MyBase
{
    public string Name { get; set; }
}

The result when serializing MyClass is:

{
    "MyClass": {
    	"Name": "Foo",
    	"@SEGMENT": "1",
    	"ID": 42
    }
}

I would like the serialized JSON output to be:

{
    "MyClass": {
    	"@SEGMENT": "1",
    	"Name": "Foo",
    	"ID": 42
    }
}

or

{
    "MyClass": {
    	"@SEGMENT": "1",
    	"ID": 42,
    	"Name": "Foo"
    }
}

Json.NET has an attribute for this: https://www.newtonsoft.com/json/help/html/JsonPropertyOrder.htm

Related question: .NET core 3: Order of serialization for JsonPropertyName (System.Text.Json.Serialization)

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Dec 20, 2019
@layomia
Copy link
Contributor

layomia commented Dec 21, 2019

From @Praveen-Rai in https://github.com/dotnet/corefx/issues/38669:

An attribute similar to JsonPropertyAttribute in JSON.Net or DataMemberAttribute in System.Runtime.Serialization.

@layomia
Copy link
Contributor

layomia commented Dec 21, 2019

From @ahsonkhan in https://github.com/dotnet/corefx/issues/38669:

This is an interesting feature request which we will consider for the future.

@steveharter, is it feasible for us to extend our existing attribute to enable this scenario in the future? We should make sure our current design allows for it.

@layomia layomia added enhancement Product code improvement that does NOT require public API changes/additions and removed untriaged New issue has not been triaged by the area owner labels Dec 21, 2019
@layomia layomia added this to the Future milestone Dec 21, 2019
@steveharter
Copy link
Member

This feature was discussed early on but did not make the list for v1.

is it feasible for us to extend our existing attribute to enable this scenario in the future? We should make sure our current design allows for it.

We don't have an existing attribute - we'd need to create a new one (e.g. JsonPropertyOrderAttribute).

The existing design would support this well as we currently build and cache an array of the properties to serialize, and the ordering is currently determined by reflection order (which is not deterministic) which can be changes to support this new attribute.

@layomia
Copy link
Contributor

layomia commented Mar 6, 2020

Closing - duplicate of #728.

@layomia layomia closed this as completed Mar 6, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json enhancement Product code improvement that does NOT require public API changes/additions
Projects
None yet
Development

No branches or pull requests

4 participants