Odd dynamic effect with JObject #3641
-
Source/destination typesGist: https://gist.github.com/jamescurran/fb13fa7563edf8d9e96cf04aab723f99 class Foo
{
public int A { get; set; }
public int? Bbb { get; set; }
public string C { get; set; }
public int D { get; set; }
}
var target = new Foo
{
A=123,
Bbb=456,
C = "Hello",
D = 789
};
dynamic overlay = ExpandoObject(); // new JObject(); //
overlay.A = (int) 888;
overlay.Bbb = (int) 999;
overlay.C = "Greetings"; Mapping configuration var configuration = new MapperConfiguration(cfg => { });
var mapper = new Mapper(configuration);
mapper.Map<dynamic, Foo>(overlay,target); Version: 13.0.1Expected behaviorA | 888 The value for properties Actual behaviorWe get the expected behavior when using ExpandoObject. However, if we use JObject (which is what JSON.NET will give you if you deserialize to a A | 123 The Steps to reproduce mapper.Map<dynamic, Foo>(overlay,target); Real-world scenario which led to thisSPA app for editing a user record. The update is made through a webservice API that receives a JSON object with just the changed fields, so object structure changes on every call, so it must be received as a [HttpPost("UpdateUser")]
public async Task<JsonResult> UpdateUser([FromBody] dynamic updates) {....} When using JSON.NET that dynamic object is actually a Workaround (for my situation)Force use of [HttpPost("UpdateUser")]
public async Task<JsonResult> UpdateUser([FromBody] ExpandoObject updates) {....} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Beta Was this translation helpful? Give feedback.
((object)source.D).Dump();
. Thedynamic
Newtonsoft.JSon implementation is, well, particular :)