Nuno Gomes /* ASP.NET Adventures */

var myInterests = new { language : "C#", technology: "ASP.NET" };

ASP.NET – Converting C# String to JSON String

Most web applications display messages to users. Displaying messages is the most effective way to inform user about errors and warnings or to simply display info or success status.

I also believe that most of those web applications renders user messages using HTML elements such as Div, Span or Label.

The others, mainly because their complex layout or business rules, have to render those messages using a different approach: they render each display message as a Javascript function call where the message itself is a function parameter.  Such call could be simply like this:

string.Format("top.addMessage(\"{0}\");", message)

Those of you that already faced this problem won’t learn nothing new but I hope to alert the others and avoid them to face a problem that usually is only detected in advanced develop stage or even production environment.

Problem

The problem turns visible when the display message contains some special characters that prevent the function call to execute and trigger javascript errors.

Typically the characters are “ and ‘ but there are more.

Well, that is not completely correct, only the character “ is problematic, since he is the JSON string delimiter character, but, javascript engines also accept the character ‘ as a delimiter. The best way to avoid problems with ‘ is to ensure that the correct delimiter is used.

By now, you should be thinking that your applications are immune to this problem. Tell me, do you use string Resources? Most of us use, it’s a best practice and a smart choice. Also tell me, do you manage production string Resources? Most of us don’t. In fact, the application owner or sponsor can change it completely outside of your control scope.

String Resources are one of the major vectors of this problem but you can get this problem simply by trying to display an exception message:

string.Format("top.addMessage(\"{0}\");", exception.ToString())

Solution

What we are needing is an JScript.Encode method or similar that encode a C# string in such a way that it becomes a JSON encoded string.

Such method is not available on NetFX but, fortunately, Rick Strahl made a great post about this problem and made available a method that fits perfectly our needs.

Now that you know about it … use it.

Posted: May 23 2009, 12:09 AM by nmgomes | with 21 comment(s)
Filed under:

Comments

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# May 24, 2009 6:31 PM

Bruno Campagnolo de Paula weblog » Resumo do dia para 2009-05-25 said:

Pingback from  Bruno Campagnolo de Paula weblog » Resumo do dia para 2009-05-25

# May 25, 2009 11:49 PM

Cafliage said:

Very informative post. Thanks for taking the time to share your view with us.

# November 28, 2010 2:55 PM

eluchelereeva said:

You certainly have some agreeable opinions and views. Your blog provides a fresh look at the subject.

# January 24, 2011 4:45 PM

apples weight loss said:

I find myself coming to your blog more and more often to the point where my visits are almost daily now!

# February 12, 2011 3:06 PM

buyers guide car said:

I’ve been visiting your blog for a while now and I always find a gem in your new posts.  Thanks for sharing.

# March 1, 2011 8:03 PM

bahglyday said:

I find myself coming to your blog more and more often to the point where my visits are almost daily now!

# March 3, 2011 3:20 PM

KemeImmolfefe said:

I just sent this post to a bunch of my friends as I agree with most of what you’re saying here and the way you’ve presented it is awesome.

# March 18, 2011 8:34 PM

volvo v40 diesel used cars said:

I find myself coming to your blog more and more often to the point where my visits are almost daily now!

# April 5, 2011 9:56 AM

cheap new car said:

I just sent this post to a bunch of my friends as I agree with most of what you’re saying here and the way you’ve presented it is awesome.

# April 19, 2011 7:02 PM

new york auto auctions said:

I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.

# April 30, 2011 5:41 AM

new jersey auto auction said:

I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.

# May 1, 2011 8:36 PM

enganoWah said:

ipage web hosting  information

# June 17, 2011 7:28 AM

licaHypespeep said:

cheap vps server - UK

# June 24, 2011 11:43 AM

twingigma said:

Very informative post. Thanks for taking the time to share your view with us.

# July 16, 2011 4:51 PM

Allerblauntee said:

I find myself coming to your blog more and more often to the point where my visits are almost daily now!

# July 17, 2011 3:07 PM

Cheap oem software said:

hu8inw Every time I come back here again and don`t get disappointed..!

# September 24, 2011 7:39 AM

Jake Molder said:

I just book marked your weblog on Digg and Stumble Upon.I delight in reading your commentaries.

# November 2, 2011 10:29 PM

Pozycjonowanie Top said:

Hi there! Do you know if they make any plugins to safeguard against hackers? I'm kinda paranoid about losing everything I've worked hard on. Any tips?

# December 14, 2011 4:09 AM

Moogelisabeth said:

order an  for more detail   , just clicks away

# December 17, 2011 6:00 AM

Cheap VPS said:

Thanks for the article, was trying to figure out how to convert a C# string to a JSON string and this helped a ton!!

# March 1, 2012 10:42 PM