34
votes

Consider these two classes:

public Class Base {
    public string Id {get; set;}
    public string Name {get; set;}
    public string LastName {get; set;}
}

And the derived class:

public Class Derived : Base {
    public string Address {get; set;}
    public DateTime DateOfBirth {get; set;}
}

When serializing the Derived class using Json.Net:

Derived record = new Derived record(); {// Initialize here...}
JsonConvert.SerializeObject(record);

By default, the properties of the Derived class appear first:

{ 
  "address": "test", 
  "date_of_birth" : "10/10/10",
  "id" : 007,
  "name" : "test name",
  "last_name": "test last name"
 }

What I need:

{ 
  "id" : 007,
  "name" : "test name",
  "last_name": "test last name"
  "address": "test", 
  "date_of_birth" : "10/10/10",      
 }

Question

Is it possible to have the base class properties come first, when serializing the derived class (without using [JsonProperty(Order=)] for each property of both classes)?

4
Is it worth asking the question "why do you need the order to be different?"Tim Barrass
@TimBarrass Just to be more organized when manual testing and debugging.A-Sharabiani
According to the JSON standard, a JSON object is an * unordered set of name/value pairs*. So my recommendation would be to not worry about this.dbc
For testing of JSON, I find JToken.DeepEquals to be very useful, it eliminates differences due purely to formatting.dbc
I don't think the 'Order' numbers have to be sequential. Maybe you could allocate 'bands' for each child (ie, base is 1-10, child A is 11-20, child B is 21-30, etc).Marc Bernier

4 Answers

42
votes

Just as a complement, another approach different than the accepted answer is using [JsonProperty(Order = -2)]; You can modify your base class as follow:

public class Base
{
    [JsonProperty(Order = -2)]
    public string Id { get; set; }

    [JsonProperty(Order = -2)]
    public string Name { get; set; }

    [JsonProperty(Order = -2)]
    public string LastName { get; set; }
}

The reason of setting Order values to -2 is that every property without an explicit Order value has a value of -1 by default. So you need to either give all child properties an Order value, or just set your base class' properties to -2.

27
votes

According to the JSON standard, a JSON object is an unordered set of name/value pairs. So my recommendation would be to not worry about property order. Nevertheless you can get the order you want by creating your own ContractResolver inheriting from one of the standard contract resolvers, and then overriding CreateProperties:

public class BaseFirstContractResolver : DefaultContractResolver
{
    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) =>
        base.CreateProperties(type, memberSerialization)
            ?.OrderBy(p => p.DeclaringType.BaseTypesAndSelf().Count()).ToList();
}

public static class TypeExtensions
{
    public static IEnumerable<Type> BaseTypesAndSelf(this Type type)
    {
        while (type != null)
        {
            yield return type;
            type = type.BaseType;
        }
    }
}

And then use it like:

// Cache an instance of the resolver for performance
static IContractResolver baseFirstResolver = new BaseFirstContractResolver { /* Set any required properties here e.g.  NamingStrategy = new CamelCaseNamingStrategy() */ };

// And use the cached instance when serializing and deserializing
var settings = new JsonSerializerSettings 
{ 
    ContractResolver = baseFirstResolver, 
    // Add your other settings here.
    TypeNameHandling = TypeNameHandling.Objects 
};
var json = JsonConvert.SerializeObject(derived, typeof(Base), Formatting.Indented, settings);

Notes:

  • This approach works especially well with multi-level type hierarchies as it automates correct ordering of properties from all levels in the hierarchy.

  • Newtonsoft recommends caching instances of contract resolvers for best performance.

Demo fiddle here.

3
votes

If you're using ASP.NET Core, don't override important contract resolver settings provided by default. Following from @dbc's answer, you can do this:

class DataContractJsonResolver : DefaultContractResolver
{
    public DataContractJsonResolver()
    {
        NamingStrategy = new CamelCaseNamingStrategy();
    }

    protected override IList<JsonProperty> CreateProperties( Type type, MemberSerialization memberSerialization )
    {
        return base.CreateProperties( type, memberSerialization )
            .OrderBy( p => BaseTypesAndSelf( p.DeclaringType ).Count() ).ToList();

        IEnumerable<Type> BaseTypesAndSelf( Type t )
        {
            while ( t != null ) {
                yield return t;
                t = t.BaseType;
            }
        }
    }
}
0
votes

I'd also consider taking 'dbc's answer, but with the replacement of the 'OrderBy()' expression, with the following (- that does not rely upon the base-class having more properties than the derived one):

                    .OrderBy(
                        p =>
                            p.DeclaringType == type
                                ? 2
                                : 1)
                    .ThenBy(
                        p =>
                            p.Order ?? -1)

And then you don't need the "TypeExtensions", but it is repeating the 'p.Order' ordering (that has already taken place within the base class); given time & thought, there might be a better/more efficient way to do this.