Jeff and .NET

The .NET musings of Jeff Putz

Sponsors

News

My Sites

Archives

Any bets on whether or not JavaScriptSerializer will really be obsolete?

Anyone who does a bit AJAXy goodness knows that it's nice and easy to transport simple objects and arrays back and forth to your server as JSON pieces. And hey, if you're doing it right, trying not to be uber chatty and keep it all zippy, you probably aren't doing anything that complex in terms of the objects you're shuttling around.

So why, oh why, would Microsoft mark JavaScriptSerializer as obsolete? The suggested "replacement" is a lot more convoluted: DataContractJsonSerializer. It doesn't even have a good name. What do contracts have to do with me wanting to just turn an array of strings into, well, an array of strings?

Scott Guthrie mentioned in a comment to a blog post of his last year that he was going to try and find out why, and petition for it not to be made obsolete, but I couldn't find any resolution beyond that. All I can say is that two lines that do something productive make a lot more sense than creating stream objects and doing a bunch of other work, and I'd rather not have to add such a thing to my own library code.

Comments

Denny Ferrassoli said:

Thanks for posting on this topic. I too was curious why they marked it obsolete. I'm assuming it had something to do with WCF. I continue to use JavaScriptSerializer regardless.

# July 9, 2008 2:54 PM

LaptopHeaven said:

JavaScriptSerializer is used in the MVC Preview 3 to parse objects to JSON.

# July 9, 2008 4:05 PM

cyrilcs said:

I continue tu use JavaScriptSerializer.

DataContractJsonSerializer is the class of WCF and we can't do everything with this. JavaScriptSerializer is simpler and better ...

I hope JavaScriptSerializer will not be deleted, if so, I hope there will be a real alternative !

# July 10, 2008 6:41 AM

Aaron said:

In a comment on one of my blog posts ScottGu mentioned that it's officially un-deprecated.

www.aaronlerch.com/.../first-thoughts-on-aspnet-mvc-preview-3

# July 10, 2008 9:39 AM

Jeff said:

Hooray!

# July 10, 2008 10:15 AM

Sean Crossey said:

I find the DataContractJsonSerializer fairly easy to use. I write a static function that serializes a generic list of an object type and it works a treat...like this:

public static string Serialize<T>(T obj)

{

DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());

       MemoryStream memoryStream = new MemoryStream();

       serializer.WriteObject(memoryStream, obj);

       return (Encoding.Default.GetString(memoryStream.ToArray()));

}

Then because its marked 'static', you can call it really easily:

List<MyClass> list = new List<MyClass>();

string json = Serialize(list);

Sorted!

# September 18, 2008 9:58 AM

subcientifico said:

I agree the JavaScriptSerializer is easy to use (and it's easy to see where it cannot be used, or might be difficult to use). I don't want to see more convoluted difficult to use APIs, e.g. the XPathNavigator and its namespace hell, but based on opinions expressed in this blog, the DataContractJsonSerializer appears to be demon spawn.

# January 14, 2009 4:45 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)