I've been using Json.Net for a while - LOVE IT!
My new requirement is to be able to create anonymous types from LINQ queries, persist to SQL, read them back later, deserialize, and make them availble to ASP.NET form/repeater controls.
ASP.NET is very happy working with anonymous types, especially when just simple property-only classes and or lists of them -- so not having the original type info around nor the original assembly which contains it is not a problem.
var anonymous =
Bird = "BlueJay",
Color = "Orange",
Team = "Rangers",
json = JsonConvert.SerializeObject(anonymous, Newtonsoft.Json.Formatting.Indented);
Persist the above to SQL.
Then, at some later time, from a different assembly, use a syntax somethinig like this:
var myData = JsonConvert.DeserializeObject(json); // any kind of simple type constructed of properties/arrays would be just fine
And be able to pass this result to an ASP.NET form control which knows the names of the properties, and does not care about class name. The control uses reflection to get at the property names. I have already tested passing in an anonymous
type - worked great.
The purpose of all this is to be able to quicky spin out hundreds of one-time-use objects....and be able to get them back and use them with the convenience of anonymous types, rather than to resort to XML serialization and then use XPATH in ASP.NET -- which
of course would work just fine, but not as nice as being able to use Json.Net here.
The example in Json.net for anonymous types requires the very same anon type to be passed in to deserialize -- which seems to me sort of defeats much of the purpose, and is not a method that would work in my scenario.
Any suggestions of how to accomplish this using Json.Net would be greatly appreciated.