Yesterday the WCF team released a new version of the “WCF Support for jQuery” libraries on codeplex (http://wcf.codeplex.com). Besides some bug fixes, we added some new features to make the JSON API better. Here’s a short list of them:
Similarly to the XElement API, JsonValue now supports events as well, so that a user can register to be notified when a JsonValue instance is modified. Notice that JsonPrimitives are immutable, since we don’t allow the modification of the wrapped CLR object, and all types supported in JsonPrimitive are immutable themselves (numbers, boolean, char, strings, DateTime, DateTimeOffset and Uri). So the event will essentially be raised only on JsonArray and JsonObject instances.
Like the XElement type, JsonValue exposes two events: Changing and Changed. The first is raised when a change is about to be made – and that gives the user some control to prevent the change from happening, like in the example below.
The second event, Changed, is raised after the change is made. The main scenario for it is to bind a JsonValue instance to an UI control. Whenever changes are made, we can update the UI based on the new value. One of the samples included in the download package (Features / JsonValueEvents) shows one way it can be done.
Exposing the wrapped CLR type
A small feature that we didn’t have before, we now expose a get-only property in JsonPrimitive that returns the CLR object which was wrapped by that instance. We’ve had converters (casts, ReadAs and TryReadAs) before so that you could ask for a CLR object from the JsonPrimitive, but there are cases where you don’t actually know the type – for example, if you want to clone the JSON DOM. This can now be done with a simple extension method, like the one shown below.
Operations and conversions in dynamic notation
- Implicit conversions to primitive types (no need to cast)
- Operators support – a dynamic reference to a JsonPrimitive can now be used in addition, subtraction, relational operations, etc.
The code below shows some examples of that. The sample included in the download package (Features / JsonValueDynamic) also shows more instances of that.
Download the new release (at http://wcf.codeplex.com/releases/view/57702) and tell us what you think. We’ll look for addressing any issues that are raised in our next release.