39

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)?

7
  • Is it worth asking the question "why do you need the order to be different?" Sep 14, 2015 at 18:43
  • 1
    @TimBarrass Just to be more organized when manual testing and debugging. Sep 14, 2015 at 18:44
  • 4
    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
    Sep 14, 2015 at 19:28
  • For testing of JSON, I find JToken.DeepEquals to be very useful, it eliminates differences due purely to formatting.
    – dbc
    Sep 14, 2015 at 19:49
  • 2
    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). Sep 14, 2015 at 20:57

5 Answers 5

54

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.

4
  • 3
    I think [JsonProperty(Order = int.MinValue)] is self explanatory (-2 appears to be an arbitrary value) Nov 24, 2019 at 1:09
  • Yes but in the question, I mentioned without using [JsonProperty(Order=)] for each property of both classes. The reason is I was working on a multiplatform project that the [JsonProperty] attribute was not available to use. Feb 19, 2020 at 18:44
  • 2
    You're right. As I mentioned, it was just a complementary answer for those who may find this question merely by its title, which is more general. So both approaches can be find in one place.
    – Moshtaf
    Feb 25, 2020 at 7:27
  • Alternately, my derived class added just one property to the base class. I found easier and cleaner to add [JsonProperty(Order = 1)] to that property in the derived class Dec 31, 2020 at 15:40
31

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.

1
  • 1
    it probably makes sense to cache type level since your are going to calculate it for each property in the same class Feb 11, 2018 at 11:27
3

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

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.

3
  • I'm not ordering by number of properties I am ordering by depth of the DeclaringType in the type hierarchy.
    – dbc
    Feb 3, 2021 at 14:12
  • But you've got a (final) call to '.Count()' in the 'OrderBy()' call. Feb 11, 2021 at 19:21
  • DeclaringType.BaseTypesAndSelf().Count() counts the number of base types in the inheritance hierarchy of the declaring type. A smaller count means that the property was declared higher up in the inheritance hierarchy (i.e. is from a base type), while a larger count means the property is from a more derived type.
    – dbc
    Feb 11, 2021 at 19:44
0

You can get the ordered properties "as is" from the base classes first like so:

public List<PropertyInfo> GetOrderedProperties(Type type)
{
    // Only get the properties for the current type and not its base type
    List<PropertyInfo> results = (type as TypeInfo)
            .DeclaredProperties
            .Where(propertyInfo => propertyInfo.CanRead && propertyInfo.CanWrite)
            .ToList();

    // Modify the above to your liking, but this presumes you want to add
    // every property that can be read and written to.

    // Insert the base type properties before the current type's properties
    if (type.BaseType != null && type.BaseType != typeof(object))
    {
        // Recursive method call
        results.InsertRange(0, GetOrderedProperties(type.BaseType));
    }

    return results;
}

Then create a custom JsonConverter (Custom JsonConverter example here):

public class OrderedPropertiesConverter : JsonConverter
{
    //...
    public override void WriteJson(
        JsonWriter writer,
        object value,
        JsonSerializer serializer
    )
    {
        if (value is null) {
            writer.WriteNullValue();
            return;
        }

        writer.WriteStartObject();
        foreach (var property in GetOrderedProperties(typeof(value)))
        {
            writer.WritePropertyName(property.Name);
            writer.WriteValue(property.GetValue(value));
        }
        writer.WriteEndObject();
    }

    public List<PropertyInfo> GetOrderedProperties(Type type)
    {
        List<PropertyInfo> results = (type as TypeInfo)
                .DeclaredProperties
                .Where(propertyInfo =>
                    propertyInfo.CanRead
                    && propertyInfo.CanWrite)
                .ToList();

        if (type.BaseType != null && type.BaseType != typeof(object))
        {
            results.InsertRange(0, GetOrderedProperties(type.BaseType));
        }
    
        return results;
    }
    //...
}

Then use your custom OrderedPropertiesConverter converter:

var json = JsonConvert.SerializeObject(contacts,new OrderedPropertiesConverter());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.