0

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

Joshua Duxbury
  • 4,892
  • 4
  • 32
  • 51
  • Code seems right. Have you tried using `JObject` instead `JToken`?. Maybe there is something odd with `Json.net`. – Cleptus Nov 09 '18 at 10:25
  • Yes, tried JToken and still nothing, I'm just opening an issue on Automappers GitHub too – Joshua Duxbury Nov 09 '18 at 10:28
  • 2
    Possible duplicate of [Using Automapper to Copy Properties from a Dynamic](https://stackoverflow.com/questions/38394587/using-automapper-to-copy-properties-from-a-dynamic) – Orel Eraki Nov 09 '18 at 10:30
  • Thanks @OrelEraki that is useful, however I still feel there is an issue here as Automapper should be able to map this primitive type without the need for extensions. It maps the string just not the integer? I'm looking for answers more than an alternative soltuion – Joshua Duxbury Nov 09 '18 at 10:59
  • Combining your code with a custom mapping [it can be done](https://stackoverflow.com/questions/38107415/automapperconverting-json-to-list-of-objects) There seems to be some code out there using TypeConverters http://codingcanvas.com/mapping-json-to-objects-using-automapper/ – Cleptus Nov 09 '18 at 11:28
  • I would advice on editing your question to state you have a problem with implementing a workaround to avoid flagging as duplicate. – Cleptus Nov 09 '18 at 11:48
  • How are you initializing `mapper`? – asherber Nov 09 '18 at 13:53
  • Nothing special Asherber, var config = new MapperConfiguration(cfg => { cfg.AddProfile(); }); return config.CreateMapper(); – Joshua Duxbury Nov 09 '18 at 14:26
  • I initialise it with just the profile containing the logic in my [answer](https://stackoverflow.com/a/53226236/3649914) – Joshua Duxbury Nov 09 '18 at 14:27
  • I tried your example, and it event didn't mapped the Url. This is due the nature of JObject which isn't composed of static property types, but it's a dynamic container for JProperties which is not a primitive type, but it's value is inside one of it's inner properties. Thus, AutoMapper can't just guess how to extract it, you should tell it how to do so. Fortunately, Json.Net did an easy way to help you with that, by using CreateReader. – Orel Eraki Nov 09 '18 at 16:37

1 Answers1

0

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);
Joshua Duxbury
  • 4,892
  • 4
  • 32
  • 51
  • AM doesn't reference Newtonsoft.Json (and no, json objects are not primitive types). But this support can be easily added outside AM. AM supports directly only built-in types. – Lucian Bargaoanu Nov 09 '18 at 13:38