Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort serialized fields using System.Text.Json

I would like to sort the serialized properties with System.Text.Json. Actually, I want to put one property (ID) in the first place.

Follow my console application.

class Program
{
    static void Main(string[] args)
    {            
        Student student = new Student()
        {                
            Name = "Robert",
            Id = Guid.NewGuid(),
            LastName = "Alv",
            Check = "Ok"
        };

        var resultJson =  System.Text.Json.JsonSerializer.Serialize(student);
        Console.WriteLine(resultJson);
        Console.ReadLine();
        
    }
    class BaseClass1
    {            
        public Guid Id { get; set; }
    }

    class BaseClass2 : BaseClass1
    {
        
    }

    class People : BaseClass2
    {            
        public string Name { get; set; }
        public string LastName { get; set; }
        public string Check { get; set; }
    }

    class Student : BaseClass2
    {            
        public string Name { get; set; }
        public string LastName { get; set; }
        public string Check { get; set; }
    }       
}

  

The result of the program above is {"Name":"Robert","LastName":"Alv","Check":"Ok","Id":"4bd17c0c-f610-414d-8f6c-49ca8957ef3f"}

But I want the result below

{"Id":"4bd17c0c-f610-414d-8f6c-49ca8957ef3f","Name":"Robert","LastName":"Alv","Check":"Ok"}

like image 452
Rogerio Potenza Avatar asked Apr 22 '26 12:04

Rogerio Potenza


2 Answers

After reading the thread stackoverflow.com/questions/3330989 indicated by @AlexeiLevenkov. I realized that It is easier using NewtonSoft.Json. So I set the attribute [JsonProperty(Order = -2)] to "ID" property and exchange System.Text.Json.JsonSerializer.Serialize(student) for JsonConvert.SerializeObject(student); It worked properly!

Below is the code updated.

class Program
{
    static void Main(string[] args)
    {            
        Student student = new Student()
        {                
            Name = "Robert",
            Id = Guid.NewGuid(),
            LastName = "Alv",
            Check = "Ok"
        };

        var resultJson =  JsonConvert.SerializeObject(student);
        Console.WriteLine(resultJson);
        Console.ReadLine();
        
    }
    class BaseClass1
    {
        [JsonProperty(Order = -2)]
        public Guid Id { get; set; }
    }

    class BaseClass2 : BaseClass1
    {
        
    }

    class People : BaseClass2
    {            
        public string Name { get; set; }
        public string LastName { get; set; }
        public string Check { get; set; }
    }

    class Student : BaseClass2
    {            
        public string Name { get; set; }
        public string LastName { get; set; }
        public string Check { get; set; }
    }       
}
like image 59
Rogerio Potenza Avatar answered Apr 25 '26 00:04

Rogerio Potenza


Not possible with System.Text.Json (yet). Supposedly in the works for a release "someday".

As you've found, it is possible with Newtonsoft.Json.

like image 21
jspinella Avatar answered Apr 25 '26 01:04

jspinella



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!