Deserializing to Anonymous Types

Aug 3, 2009 at 9:30 PM

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.

For example:

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 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.



Aug 4, 2009 at 10:32 PM

Json.NET can't know what the structure of the JSON will be at design time. It can't automatically generate an anonymous type for you.

Is the issue databinding JSON in ASP.NET?

Aug 6, 2009 at 5:25 AM

Yes, the primary issue is simple databinding; hoping to bypass the need for creating a tons of data-only classes, which certainly works wonderfully with straight JSON serialization concepts.

There's certainly a number of possible approaches. Maybe JObject can read and parse the files. I'd want to be able to hook the resulting hydrated object directly up to DataSource properties of server controls (no ObjectDataSource) and then use standard binding markup from there.

We're using only very simple types (small classes with a few properties) and lists of them.

I was just thinking that maybe Json.Net might allow a rather elegant approach if I understood it better. I'll spend a little more time looking at connecting JObject up to datasource.

Aug 6, 2009 at 8:58 AM

Ok. I believe the proper way to do it would be to add ICustomTypeDescriptors for the various LINQ to JSON types but that is a fair bit of work if you want to do it yourself.

I plan to look at databinding sometime in the next couple of months.

Aug 6, 2009 at 8:18 PM

I think I'll stick with POCO classes and keeping the assembly around for now and then maybe slipstream your databinding in later.

Keep up the excellent work!