views:

935

answers:

5

According to this article (http://msdn.microsoft.com/en-us/library/cc197951(VS.95).aspx) Silverlight 2 Beta 2 supports the DataContractJsonSerializer object. But, when I try to use it VS says "Type 'DataContractJsonSerializer' is not defined".

I have a method marked as ScriptableMember that gets called from JavaScript and is passed an Object. Inside this method I need to serialize the object to a string (preferably JSON) and then save it in isolated storage.

Does Silverlight 2 Beta 2 really support DataContractJsonSerializer? Or would anyone recommend a different method of saving the JavaScript created ScriptObject in the Isolated Storage?

+1  A: 

For now, the only solution to this that I have found is to use the ASP.NET AJAX JavaScriptSerializer to do the JSON serialization/deserialization in JavaScript, and then just use Silverlight to store/retrieve the resulting string.

Sys.Serialization.JavaScriptSerializer.serialize(obj);
Sys.Serialization.JavaScriptSerializer.deserialize(json);
Chris Pietschmann
A: 

I would say your own answer would be the best approach. JavaScript is dead slow at doing stuff like that, so best you leave the serialization-part to ASP.NET.

roosteronacid
I'm talking about all client-side code. The ASP.NET AJAX JavaScriptSerializer that I'm referring to is the JavaScript-based one that is part of the ASP.NET AJAX Client-Side Scripts.
Chris Pietschmann
+3  A: 

Actually the answer is, the DataContractJsonSerializer is part of Silverlight 2 Beta 2, but you need to add a reference to System.ServiceModel.Web to your Silverlight project to use it.

I didn't realize that you still needed to add dll references in Silverlight. I thought it automatically included everything in a similar way to how ASP.NET does.

Chris Pietschmann
+2  A: 

There is a Silverlight version of Json.NET that will serialize your objects to JSON. It doesn't require [DataContract] and [DataMember] attributes all over your objects.

Json.NET

James Newton-King
A: 

If you don't need JSON specifically, check out: 1. www.eggheadcafe.com/tutorials/aspnet/a09a0861-c83e-4e77-915a-5b86b0c676c9/silverlight-isolatedstorage-compressed-object-cache.aspx 2. smehrozalam.wordpress.com/2009/02/10/saving-data-objects-to-isolated-storage-in-silverlight/ 3. www.sharpserializer.com (compression is also planned, currently still forthcoming)

Dave A-W