Newtonsoft.json: PopulateObject: populating anonymous type instance

Created on 23 Feb 2015  路  3Comments  路  Source: JamesNK/Newtonsoft.Json

i just stumbled upon a use case where i want to fill a anonymous object with values coming from json:

var o = new { foo = "123", bar = "456" };
JsonConvert.PopulateObject(@"{'foo': 'abc'}", o);
Console.WriteLine(o.foo); // outputs "123" instead of "abc"

but it does not work as long as o is of anonymous type.

Most helpful comment

in any case, here's my workaround: https://dotnetfiddle.net/S4GPil

All 3 comments

Anonymous objects don't have setters.

i see, didn't know too much about anonymous types. there would be a way by modifying their backing fields but i guess you wouldn't want that it in the lib.

in any case, here's my workaround: https://dotnetfiddle.net/S4GPil

Was this page helpful?
0 / 5 - 0 ratings