Since NetFX 3.5, WCF has support for working with JSON data. You can define your data contracts according to the JSON “schema” you expect to receive / want to return, plug them into your operation contracts, and your server is talking JSON. With this feature you can define the contract such as:
and handle requests or return responses such as the ones below:
So this feature works quite well, with only one problem. You need to know the “schema” of the data you’re receiving (or returning). There are some cases where the service doesn’t know it upfront – for example, when the definition of the schema is part of the message itself (and you want to populate a data grid from it):
Currently, unless you want to parse the JSON yourself (or let WCF parse the JSON into “XML” using the JSON/XML mapping, and parse the information from the “XML” nodes yourself), WCF doesn’t support this scenario well. You’re tied to the data contract, and if you don’t have a data contract, you’re stuck.
In comes JsonValue
In Silverlight (since SL2), there is an API which deals with JSON in an untyped way. The JsonValue classes provide a DOM in which one can load a JSON document, and work with it without linking it to any specific CLR types. I like to think of JsonValue as the equivalent to XElement in the XML world: JsonValue : DataContractJsonSerializer :: XElement : DataContractSerializer. Silverlight, however, is a client-only platform, and as such the JsonValue classes didn’t have any integration with the service model (i.e., it can’t be used in an operation contract).
We found that the JsonValue abstraction, however, is quite interesting, so we decided to port it to the desktop framework, and hook it up to WCF. So you can now consume that “schema-less” value
Now, besides porting the code from Silverlight, we also added additional features such as Linq support, better casting, among others, to make the experience of using the new APIs simpler. The code above, for example, can be rewritten simply as
I want to use it; now what?
Since the next iteration of .NET Framework (where this feature may be included) is still a long time away, we decided to publish the code to enable users to take advantage of this feature now. As of this morning, we have a new Codeplex site at http://wcf.codeplex.com which contains the binaries needed to use this feature and all its source code, so if you need it the way it is, simply use it. If you need some change, you can either open an issue and wait for the next update (which should be fairly quick), or even fix it yourself, so you don’t get blocked. The JsonValue code is on the “jQuery Support” link on the main page.
Go ahead, download it, let us know what you think!
I only touched the surface of this new API. You can find more information at the following pages:
- JsonValue walkthrough: http://hashtagfail.com/post/1432485895/jsonvalue-json-walkthrough
- JsonValue feature details: http://wcf.codeplex.com/wikipage?title=JsonValue