1

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

Abhishek B.
  • 5,112
  • 14
  • 51
  • 90
  • I assume you need to write a custom mapper as seen in this Possible duplicate of [Automapper:Converting JSON to list of objects](https://stackoverflow.com/questions/38107415/automapperconverting-json-to-list-of-objects) – Nope Jan 03 '18 at 11:28
  • While Auto mapping you can create a mapping, which Json deserialize string into relevant class object – Mrinal Kamboj Jan 03 '18 at 11:29
  • @Fran I tried it. in GET action it returning NULL value – Abhishek B. Jan 03 '18 at 11:37
  • 1
    That would only happen when there's a mismatch between Json and object schema, post the code please – Mrinal Kamboj Jan 03 '18 at 11:38
  • @MrinalKamboj I tried in POST action it json and mapped is ok now. but in GET it returning SourceInput , SourceOutput , SourcePosition coming null – Abhishek B. Jan 03 '18 at 11:38
  • @Fran why you put it duplicate there is different issue on mapping. sub class mapping problem with json in GET Action. pls check – Abhishek B. Jan 03 '18 at 12:30
  • @AbhishekB. Before your question was updated you had no custom mappings and asked how to map it. The duplicate should contain the code examples you need to derive your own mappings from. If you now have mappings and they don't work, then that is a different question. – Nope Jan 03 '18 at 15:19
  • @Fran you can update question subject feel free.. – Abhishek B. Jan 03 '18 at 16:26
  • @AbhishekB.Yu If your question keeps changing you are suppose to ask separate questions. If you don't want to that's fine, you might just not get any answers as it confuses everyone what it is you are looking for. It's up to you not us. – Nope Jan 03 '18 at 16:41

1 Answers1

1

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.

Abhishek B.
  • 5,112
  • 14
  • 51
  • 90