0

Classes created as below:

public class Base
{
   public int Id { get; set; }
}
public class Derived:Base
{
   public string Name { get; set; }
}

Automapper mapping defined as:

CreateMap<DataRow, Base>()
.ForMember(m => m.Id, opt => opt.MapFrom(r => r["Id"]));

CreateMap<DataRow, Derived>()
.IncludeBase<DataRow, Base>()
.ForMember(m => m.Name, opt => opt.MapFrom(r => r["Name"]))

Output of this mapping is:

{
 "Name":"ABC",
 "Id":1
}

Expected result:

{
 "Id":1,
 "Name":"ABC"
}

I want to maintain the sequence of properties as defined in base and derived class.

  • 3
    Automapper maps between classes, it has nothing to do with JSON. This question makes no sense. – DavidG Mar 11 '22 at 13:08
  • DavidG - But after mapping and using Automapper the sequence of properties is getting changed. Order/Display Order DataAnnotation is also not working – Abhinav Verma Mar 11 '22 at 13:22
  • Changed where though? How are you seeing this? – DavidG Mar 11 '22 at 13:24
  • DavidG - When only 1 mapping is defined then in the postman result - Id was 1st attribute and Name as 2nd. But after using IncludeBase, then Name is 1st attribute and Id as 2nd – Abhinav Verma Mar 11 '22 at 13:32
  • Did you have the base and derived class there before you added the mapping though? That is how serialisation works and Automapper has absolutely no effect on the JSON that is output by your API. Is this .NET Core? Which version? – DavidG Mar 11 '22 at 13:36
  • Dotnet framework 4.8 WebAPI. Even Dataannotation [Column(Order = 1)] or [Display(Order=1)] is also not working – Abhinav Verma Mar 11 '22 at 13:40
  • What library is being used to serialize the class as JSON? – Oliver Mar 11 '22 at 13:46
  • With JSON why would it ever matter if the order of properties is different? – mxmissile Mar 11 '22 at 15:08

1 Answers1

2

This has nothing to do with Automapper, that library doesn't affect the JSON serialisation.

The real issue is that you have a derived class. Serialisation libraries use reflection to get the properties and that starts with the properties in the derived class and works up the hierarchy. You can see this with this code:

var props = string.Join(", ", typeof(Derived).GetProperties().Select(x => x.Name));

//props = "Name, Id"

However, you can control the serialisation with some attributes. If you are using .NET 3+, then you will likely be using the System.Text.Json namespace. That means you can change your model to be something like this:

using System.Text.Json.Serialization;

public class Base
{
    [JsonPropertyOrder(1)]
    public int Id { get; set; }
}
public class Derived : Base
{
    [JsonPropertyOrder(2)]
    public string Name { get; set; }
}

Alternatively, you may be using Newtonsoft JSON.Net which means your models will instead look like this:

using Newtonsoft.Json;

public class Base
{
    [JsonProperty(Order = 1)]
    public int Id { get; set; }
}
public class Derived : Base
{
    [JsonProperty(Order = 2)]
    public string Name { get; set; }
}
DavidG
  • 113,891
  • 12
  • 217
  • 223