I have tried the JavaScriptSerializer, the DataContractJsonSerializer and even JSON.Net but I always keep coming back to the JavaScriptSerializer because it is the simplest solution that doesn’t impose any 3rd party dependencies. The problem with all of these solutions is that I am constantly having to remember how to use them. The API, even for the JavaScriptSerializer, still requires several lines of code whenever I want to use it. I wanted something that was drop-dead simple, yet still powerful enough to handle 95% of my serialization/deserialization needs (for the really tough stuff I can still use one off code with an appropriate serializer)
Well, at some point I finally broke down and did what any good technologist would do – I turned to an expert who had already solved the problem in a simple, yet elegant way, and I copied him. I have known Rick Strahl for several years and he is one of the few bloggers that I still find myself following on a regular basis. I know that if I found the code useful, that it would also be pretty useful to many other DotNetNuke module developers as well. Starting with DotNetNuke 5.4.3 we included a VB version of the JsonExtensionsWeb class from Rick’s original post (with Rick’s permission of course).
The JsonExtensionsWeb is simple extension class that adds several useful extension methods to any object. Specifically it adds ToJson to any object and FromJson to any string. There are a couple of variations, depending on whether you are ok working with Generics (where the type must be known at compile time) or if you need a little more dynamic behavior (which is not supported natively by the JavaScriptSerializer). Lets look at a few examples to get a feeling for the power and simplicity that this adds to our code.
There are occasions where you want to serialize an anonymous type without creating a concrete class to hold the type. When working with web-services, I want to be able to keep my payload as small as possible and not send down a large serialized class if my client app only requires a small subset of properties from that class. With these extensions I can create a simple anonymous class that only contains my needed data and then call the ToJson method to get a JSON string:
Compare the JSON from my anonymous class versus if I had serialized the entire UserInfo object :
Anonymous = {"UserID":1,
"UserName":"host",
"Email":"host@change.me"}
UserInfo = {"AffiliateID":-1, "DisplayName":"SuperUser Account", "Email":"host@change.me", "FirstName":"SuperUser", "IsDeleted":false, "IsSuperUser":true, "LastIPAddress":null, "LastName":"Account", "Membership":{"Approved":true, "CreatedDate":"\/Date(1287064692000)\/", "IsOnLine":false, "LastActivityDate":"\/Date(1287240881730)\/", "LastLockoutDate":"\/Date(-6816268800000)\/", "LastLoginDate":"\/Date(1287240881687)\/", "LastPasswordChangeDate":"\/Date(1287065731593)\/", "LockedOut":false, "Password":null, "PasswordAnswer":null, "PasswordQuestion":null, "UpdatePassword":false, "Email":"host@change.me", "ObjectHydrated":false, "Username":"host"}, "PortalID":0, "Profile":{"Cell":null, "City":null, "Country":null, "Fax":null, "FirstName":"SuperUser", "FullName":"SuperUser Account", "IM":null, "IsDirty":false, "LastName":"Account", "Photo":null, "PhotoURL":"/dnnmobile/images/no_avatar.gif", "PostalCode":null, "PreferredLocale":"en-US", "ProfileProperties":[{"DataType":349, "DefaultValue":"", "IsDirty":false, "Length":50, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Name", "PropertyDefinitionId":1, "PropertyName":"Prefix", "PropertyValue":null, "Required":false, "ValidationExpression":"", "ViewOrder":1, "Visible":true, "DefaultVisibility":0, "Visibility":0, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}, {"DataType":349, "DefaultValue":"", "IsDirty":false, "Length":50, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Name", "PropertyDefinitionId":2, "PropertyName":"FirstName", "PropertyValue":"SuperUser", "Required":false, "ValidationExpression":"", "ViewOrder":3, "Visible":true, "DefaultVisibility":0, "Visibility":0, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}, {"DataType":349, "DefaultValue":"", "IsDirty":false, "Length":50, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Name", "PropertyDefinitionId":3, "PropertyName":"MiddleName", "PropertyValue":null, "Required":false, "ValidationExpression":"", "ViewOrder":5, "Visible":true, "DefaultVisibility":0, "Visibility":0, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}, {"DataType":349, "DefaultValue":"", "IsDirty":false, "Length":50, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Name", "PropertyDefinitionId":4, "PropertyName":"LastName", "PropertyValue":"Account", "Required":false, "ValidationExpression":"", "ViewOrder":7, "Visible":true, "DefaultVisibility":0, "Visibility":0, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}, {"DataType":349, "DefaultValue":"", "IsDirty":false, "Length":50, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Name", "PropertyDefinitionId":5, "PropertyName":"Suffix", "PropertyValue":null, "Required":false, "ValidationExpression":"", "ViewOrder":9, "Visible":true, "DefaultVisibility":0, "Visibility":0, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}, {"DataType":349, "DefaultValue":"", "IsDirty":false, "Length":50, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Address", "PropertyDefinitionId":6, "PropertyName":"Unit", "PropertyValue":null, "Required":false, "ValidationExpression":"", "ViewOrder":11, "Visible":true, "DefaultVisibility":2, "Visibility":2, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}, {"DataType":349, "DefaultValue":"", "IsDirty":false, "Length":50, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Address", "PropertyDefinitionId":7, "PropertyName":"Street", "PropertyValue":null, "Required":false, "ValidationExpression":"", "ViewOrder":13, "Visible":true, "DefaultVisibility":2, "Visibility":2, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}, {"DataType":349, "DefaultValue":"", "IsDirty":false, "Length":50, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Address", "PropertyDefinitionId":8, "PropertyName":"City", "PropertyValue":null, "Required":false, "ValidationExpression":"", "ViewOrder":15, "Visible":true, "DefaultVisibility":2, "Visibility":2, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}, {"DataType":357, "DefaultValue":"", "IsDirty":false, "Length":0, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Address", "PropertyDefinitionId":9, "PropertyName":"Region", "PropertyValue":null, "Required":false, "ValidationExpression":"", "ViewOrder":17, "Visible":true, "DefaultVisibility":2, "Visibility":2, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}, {"DataType":356, "DefaultValue":"", "IsDirty":false, "Length":0, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Address", "PropertyDefinitionId":10, "PropertyName":"Country", "PropertyValue":null, "Required":false, "ValidationExpression":"", "ViewOrder":19, "Visible":true, "DefaultVisibility":2, "Visibility":2, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}, {"DataType":349, "DefaultValue":"", "IsDirty":false, "Length":50, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Address", "PropertyDefinitionId":11, "PropertyName":"PostalCode", "PropertyValue":null, "Required":false, "ValidationExpression":"", "ViewOrder":21, "Visible":true, "DefaultVisibility":2, "Visibility":2, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}, {"DataType":349, "DefaultValue":"", "IsDirty":false, "Length":50, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Contact Info", "PropertyDefinitionId":12, "PropertyName":"Telephone", "PropertyValue":null, "Required":false, "ValidationExpression":"", "ViewOrder":23, "Visible":true, "DefaultVisibility":2, "Visibility":2, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}, {"DataType":349, "DefaultValue":"", "IsDirty":false, "Length":50, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Contact Info", "PropertyDefinitionId":13, "PropertyName":"Cell", "PropertyValue":null, "Required":false, "ValidationExpression":"", "ViewOrder":25, "Visible":true, "DefaultVisibility":2, "Visibility":2, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}, {"DataType":349, "DefaultValue":"", "IsDirty":false, "Length":50, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Contact Info", "PropertyDefinitionId":14, "PropertyName":"Fax", "PropertyValue":null, "Required":false, "ValidationExpression":"", "ViewOrder":27, "Visible":true, "DefaultVisibility":2, "Visibility":2, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}, {"DataType":349, "DefaultValue":"", "IsDirty":false, "Length":50, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Contact Info", "PropertyDefinitionId":15, "PropertyName":"Website", "PropertyValue":null, "Required":false, "ValidationExpression":"", "ViewOrder":29, "Visible":true, "DefaultVisibility":2, "Visibility":2, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}, {"DataType":349, "DefaultValue":"", "IsDirty":false, "Length":50, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Contact Info", "PropertyDefinitionId":16, "PropertyName":"IM", "PropertyValue":null, "Required":false, "ValidationExpression":"", "ViewOrder":31, "Visible":true, "DefaultVisibility":2, "Visibility":2, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}, {"DataType":355, "DefaultValue":"", "IsDirty":false, "Length":0, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Preferences", "PropertyDefinitionId":17, "PropertyName":"Biography", "PropertyValue":null, "Required":false, "ValidationExpression":"", "ViewOrder":33, "Visible":true, "DefaultVisibility":2, "Visibility":2, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}, {"DataType":352, "DefaultValue":"", "IsDirty":false, "Length":0, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Preferences", "PropertyDefinitionId":18, "PropertyName":"TimeZone", "PropertyValue":"0", "Required":false, "ValidationExpression":"", "ViewOrder":35, "Visible":true, "DefaultVisibility":2, "Visibility":2, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}, {"DataType":353, "DefaultValue":"", "IsDirty":false, "Length":0, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Preferences", "PropertyDefinitionId":19, "PropertyName":"PreferredLocale", "PropertyValue":"en-US", "Required":false, "ValidationExpression":"", "ViewOrder":37, "Visible":true, "DefaultVisibility":2, "Visibility":2, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}, {"DataType":361, "DefaultValue":"", "IsDirty":false, "Length":0, "ModuleDefId":-1, "PortalId":-1, "PropertyCategory":"Preferences", "PropertyDefinitionId":20, "PropertyName":"Photo", "PropertyValue":null, "Required":false, "ValidationExpression":"", "ViewOrder":40, "Visible":true, "DefaultVisibility":0, "Visibility":0, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}], "Region":null, "Street":null, "Telephone":null, "TimeZone":0, "Unit":null, "Website":null, "ObjectHydrated":false}, "RefreshRoles":false, "Roles":[""], "UserID":1, "Username":"host", "FullName":"SuperUser Account", "Cacheability":0, "CreatedByUserID":-1, "CreatedOnDate":"\/Date(-62135578800000)\/", "LastModifiedByUserID":-1, "LastModifiedOnDate":"\/Date(-62135578800000)\/"}
I think it is quite obvious which is the better choice for using with web services. In the past I would have created a ViewModel class just for serializing, but because of the power of the JavaScriptSerializer, I can skip that step.
Just as ToJson makes it easy to save my objects as a JSON string, I can also take a string and de-serialize it into my original object. In this case, I can’t create an anonymous object but have to de-serialize to a known type. This is often not an issue as I rarely want to work with anonymous types directly, I use them strictly as a convenience to avoid creating viewmodels.
All of the usual limitations still exist – it doesn’t deal well with circular references and you can only deserialize to concrete classes with a default constructor. Even with these limitations, this is a great feature that I find myself using more and more in my module development and I hope that now that you know about it, that you will start using it as well.