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

How to order child property in System.json.Text to come last during serialization #38199

Closed
KamranShahid opened this issue Jun 21, 2020 · 4 comments

Comments

@KamranShahid
Copy link

I have following class in my .net core 3.1 application

public class FinalsuccessResultForTransactionBase
{
[JsonPropertyName("auth_req_id")]
public string AuthReqId { get; set; }

    [JsonPropertyName("correlation_id")]
    public string CorrelationId { get; set; }

    [JsonPropertyName("expires_in")]
    public int ExpiresIn { get; set; }       
}

public class FInalsuccessResultForPolling : FinalsuccessResultForTransactionBase
{
    [JsonPropertyName("interval")]
    public int Interval { get; set; }
}

when serializing i am getting it like
{"interval":10,"auth_req_id":"LoXBka93EiBcCvbwppHA","correlation_id":null,"expires_in":1800}

I wanted it like

{"auth_req_id":"LoXBka93EiBcCvbwppHA","correlation_id":null,"expires_in":1800,"interval":10}

What should i do

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Jun 21, 2020
@Clockwork-Muse
Copy link
Contributor

... Is there a particular reason you want it ordered? JSON is explicitly unordered.

It looks like this function isn't yet implemented, this is essentially a duplicate of issue #728. The recommendation there is to create a custom serializer.

@KamranShahid
Copy link
Author

... Is there a particular reason you want it ordered? JSON is explicitly unordered.

It looks like this function isn't yet implemented, this is essentially a duplicate of issue #728. The recommendation there is to create a custom serializer.

There is no big reason for ordering. In my logs i want's certain elements to come first. But it is very strange that basic order feature like newtonsoft is not there

@Clockwork-Muse
Copy link
Contributor

But it is very strange that basic order feature like newtonsoft is not there

Because:

JSON is explicitly unordered.

System.Text.Json is a low-level base library for dealing with JSON. While it's possible to use it to de/serialize JSON with it, it was written in such a way as to be extensible by other libraries, who might do things like provide ordering.

If you want the items to be ordered to make your logs more readable, I'd actually recommend looking for a tool like Splunk, which can pull such data out, be able to search on it, etc.

@layomia
Copy link
Contributor

layomia commented Jul 7, 2020

FWIW - this is a duplicate of #728 and is a valid feature to be considered for built-in support.

@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Jul 7, 2020
@layomia layomia added this to the 5.0.0 milestone Jul 7, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 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

5 participants
@KamranShahid @Clockwork-Muse @layomia @Dotnet-GitSync-Bot and others