0

How do I do a mapping between an object created by instantiating a class and a dynamic object created by a JSON deserialization (JsonConvert) considering I don't know the dynamic object's fields? In other words, I'd like to update the dynamic object fields matching by name.

This is my example code:

string json = {\"NDG\":7803, \"NumberOfNights\":2, \"Nome\":\"Ago\", \"Cognome\":\"Mar\", \"CognomeNome\":\"\"};
string djson = ?? //I don't know the structure coming from a call as parameter but I know there are some json string identical fields  

public class myVars
{
    public string Userid { get; set; }
    public string Nome { get; set; }
    public string Cognome { get; set; }
    public string CognomeNome { get; set; }
}

myVars object1 = JsonConvert.DeserializeObject<myVars>(json);
dynamic object2 = JObject.Parse(djson); // object2 contains a field named "CognomeNome"


myVars.CognomeNome = myVars.Cognome + myVarsNome;
MapObjects(object1 , object2);
string rjson = JsonConvert.SerializeObject(object2); //returns {"CognomeNome":""}

public static object MapObjects(object source, object target)
    {
        foreach (PropertyInfo sourceProp in source.GetType().GetProperties())
        {
            PropertyInfo targetProp = target.GetType().GetProperties().Where(p => p.Name == sourceProp.Name).FirstOrDefault();
            if (targetProp != null && targetProp.GetType().Name == sourceProp.GetType().Name)
            {
                targetProp.SetValue(target, sourceProp.GetValue(source));
            }
        }
        return target;
    }
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
AgoMar
  • 1
  • 2
  • 3
    You'll need to add more information for your question to be answerable (example, perhaps?). Seemingly [`AutoMapper` has support](https://github.com/AutoMapper/AutoMapper/wiki/Dynamic-and-ExpandoObject-Mapping) for `dynamic` so this presumably extends to `JObject` as well – StuartLC Oct 28 '17 at 12:04
  • Possible duplicate of [C# - How to create a dynamic object from a static object?](https://stackoverflow.com/questions/23156815/c-sharp-how-to-create-a-dynamic-object-from-a-static-object) – derloopkat Oct 28 '17 at 12:08
  • You can try c# reflection to retrieve the names of the members from known object and try to check whether they exist in dynamic object. If it exists you can assign using the reflection. – XPD Oct 28 '17 at 12:09
  • Hello, and welcome to stackoverflow. As explained in [ask], we're going to need to see some code and/or JSON to give a concrete answer; a [mcve] would be best. But since a *dynamic object created by `JsonConvert`* is really just a [`JToken`](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JToken.htm), you should be able to deserialize to a new concrete object using [`JToken.ToObject()`](https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JToken_ToObject__1.htm), or populate an existing object as shown [here](https://stackoverflow.com/a/30220811). – dbc Oct 28 '17 at 19:30

1 Answers1

1

The simplest way is to use one of the existing automappers (Alternatives to AutoMapper)

For example, this: https://github.com/agileobjects/AgileMapper/wiki/Performing-Updates sounds like it's what you are asking for.

Malcolm
  • 1,239
  • 1
  • 14
  • 25