Automapper Not Mapping JObject To POCO Not Mapping Int

366 Views Asked by At

Example code

    JToken json = JObject.Parse(
        " {\"Url\": \"www.fakeUrl.com\",\"CallId\": 12}");

    var poco = mapper.Map<CallData>(json);

    Console.WriteLine(json);
    Console.WriteLine(poco.Url + " " + poco.CallId); 

Simple Model

   public class CallData
    {
        public int CallId { get; set; }
        public string Url { get; set; }
    }

Output

{ "Url": "www.fakeUrl.com", "CallId": 12 }

www.fakeUrl.com 0

I'm just curious to why Automapper isn't mapping the integer in this JSON object? I know there are alternatives out such as a custom extension for this but I'm wondering why AutoMapper can't do this simple map?

Automapper V7.0.1

1

There are 1 best solutions below

1
On BEST ANSWER

I resolved the issue by adding a custom mapping. I still believe this to be an issue with the underlining libraries and will investigate further as this simple primitive mapping shouldn't need extensions.

Mapper

CreateMap<dynamic ,CallData>().ConvertUsing((jo) =>
{
    var callData = new CallData();
    JsonSerializer serializer = new JsonSerializer();
    if(jo != null)
     serializer.Populate((JsonReader) jo.CreateReader(), callData);
    return callData;
});

Usage

var response =_mapper.Map<dynamic, CallData>(_callData);