Abhishek B.
Abhishek B.

Reputation: 5202

How to map class entity within json with automapper?

I am storing the Meta information into String fields. but in DTO object I am mapped with separated class object..

Sample JSON Post object

{

  "name": "string",
  "directionId": 0,
  "description": "string",
  "siteId": 0,
  "zoneId": 0,
  "areaId": 0,
  "metaData": {
    "input": {
      "name": "string",
      "key": "string",
      "agentId": 0,
      "type": 0
    },
    "outPut": {
      "name": "string",
      "key": "string",
      "agentId": 0,
      "type": 0
    },
    "position": {
      "lat": "string",
      "long": "string"
    }
  }
}

Entity Class object

/// <summary>
/// SRC  
/// </summary>
public class SRC
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int? DirectionId { get; set; }
    public string Description { get; set; }
    public int? SiteId { get; set; }
    public int? ZoneId { get; set; }
    public int? AreaId { get; set; }

    /// <summary>
    /// Json view MetaData
    /// { 
    ///   "input": {
    ///      name:"my input1",
    ///       key:"43434",
    ///       agent:"1",
    ///       type:"1",
    ///    }
    ///    "output": {
    ///      name:"my output",
    ///       key:"12333",
    ///       agent:"1",
    ///       type:"1",
    ///    }
    ///    "position": {
    ///      lat:"42°21'30"N 71°03'37",
    ///       long:"42°21'30"S 71°03'37",
    ///    }
    /// }
    /// </summary>
    public string MetaData { get; set; }
}

DTO Class object

/// <summary>
/// SRC DTO
/// </summary>
public class SrcDTO
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int? DirectionId { get; set; }
    public string Description { get; set; }
    public int? SiteId { get; set; }
    public int? ZoneId { get; set; }
    public int? AreaId { get; set; }
    [JsonProperty("MetaData")]
    public SourceMetaData MetaData { get; set; }
}
#region Meta Data Class

public class SRCMetaData
{
    [JsonProperty("Input")]
    SourceInput SourceInput { get; set; }
    [JsonProperty("OutPut")]
    SourceOutput SourceOutput { get; set; }
    [JsonProperty("Position")]
    SourcePosition SourcePosition { get; set; }
}
public class SourceInput
{
    public string Name { get; set; }
    public string Key { get; set; }
    public int AgentId { get; set; }
    public int Type { get; set; }
}
public class SourceOutput
{
    public string Name { get; set; }
    public string Key { get; set; }
    public int AgentId { get; set; }
    public int Type { get; set; }
}
public class SourcePosition
{
    public string Lat { get; set; }
    public string Long { get; set; }
}
#endregion

How it will mapped with Auto Mapper profile?

   CreateMap<SrcDTO, Src>()
        .ForMember(x => x.MetaData, cfg => { cfg.MapFrom(jo => JsonConvert.SerializeObject(jo.MetaData)); })
        ;

        CreateMap<Src,  SrcDTO>()
        .ForMember(x=>x.MetaData , cfg => { cfg.MapFrom(jo => JsonConvert.DeserializeObject(jo.MetaData)); })
        ;

On post action with sending JSON it's working ok. but in the GetALL action its not working see below images for references. enter image description here enter image description here

Upvotes: 1

Views: 1938

Answers (1)

Abhishek B.
Abhishek B.

Reputation: 5202

Issue was mapping "Int" data type fields in sub class object. I have been fixed by modifying with "string" data type fields.

As serialized with JsonConvert.SerializeObject with casting then it worked for me..

Thank you.

Upvotes: 1

Related Questions