Encoding NewLines & Quotes in jSON

by Randall 7/20/2010 11:04:00 AM

Today I attempted to send a giant amount of user-entered text from a standard HTML page, via jQuery's .ajax() method - passing a jSON packet to a REST-enabled WCF service.

It was failing.

Turns out the newlines were destroying the jSON formatting, so I needed to find a way to preserve the information for the server.  I ran across a blog post dealing with this here,  and he suggested the following bit of code:

function escapeNewLineChars(valueToEscape) {
    if (valueToEscape != null && valueToEscape != "") {
        return valueToEscape.replace(/\n/g, "\\n");
    } else {
        return valueToEscape;
    }
}

It didn't work.

Turns out, after fiddling with it, the "replace with" value needed to be wrapped in single (not double) quotes. So, in order to prepare your text for transmittal via jSON, try this:

function fixMyUserEnteredData(giantBlobOfText) {

giantBlobOfText = $.trim(giantBlobOfText.replace(/\n/g, '\\n')); // converts the newlines and trims the string

return giantBlobOfText.replace(/\"/g, '\\"')); // converts all double quotes

}

That did the trick.

 

Currently rated 1.4 by 1456 people

  • Currently 1.440249/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Fixes

Getting started with the Android SDK

by Randall 11/1/2009 11:07:00 PM

After putting off Android for quite some time, I finally decided to dig in.  I must admit, however, that it was Verizon's clever marketing campaign in coordination of the upcoming launch of the Verizon Droid that motivated me enough to do it.  For the last year or so I've had friends and family memers mention on occasion that I 'should' be developing applications for the iPhone (perhaps they're all thinking that I'll strike gold and share some moola), but I never could motivate myself.  First of all, I'm not an Apple fanboy.  Secondly, I don't own a Mac so that kind of kills it.  Apple has seen to it to exclude Windows developers from getting in on the action.  Android and Google is a completely different story.

The Android SDK plugs into the Eclipse IDE quite seamlessly.  I was able to get Eclipse and the Android SDK installed, updated, configured, and ran my initial "Hello World" application within about an hour.  Since Eclipse runs on Windows (as well as Linux and Mac), I was able to dig right in.  Kudos to Google.

Let's see what it's all about.

The best place to start is right here: http://developer.android.com/sdk/index.htm

I'm not going to reproduce their work, so let's get right to how things look (this is running on a fresh install of Windows 7) ...

Running the AVD



... still booting ... 



... and still booting ... 



The whole bootup process takes far too long.  I have yet to be able to find a way to shorten it.  Unfortunately, every time you make a code change and want to run your application, you must wait for about 1 min or so for the AVD to fire up.  Unless I've just not found it, this is a step back from developing against the .Net CF as the emulators there enter a standby mode when you stop debugging.  ***EDIT*** You do not need to shut down the emulator in order to deploy new code.  Simply minimize the AVD, make your code changes, then run the application via Eclipse.  The updated application will run correctly.

"Hello, Android!"

According to Google's "Hello World" tutorial, you can simply change this:


to this ...


... and Voila! But this DOES NOT work. At first glance it (seemingly) works, but change the argument of setText to "Howdy, Android" and you will still get "Hello, Android" when you run the AVD. I tried several times recompiling my code. Nothing worked.

To resolve this, you need to understand that Android applications support two ways of building the user interface: through code and XML files1. If you browse to 'res/layout/main.xml' and "Open with XML Editor", you'll see the following:


The key here (literally) is in the @string/hello. Now open 'strings.xml' and find the Resource Element called 'hello' which is being referenced here. If you change the value here to something else, it will work.

 

Run your application again (and wait, and wait, and wait) ...

 

Nothing dramatic yet.  Just getting up and going.  Perhaps someone else had or will have the same issue and this will help.  Tell me if you know how to speed up running the apps on the AVD.

More to come on this.

Sources: 1. "Getting Started with Android Development Using Eclipse", September 15, 2008, Wei-Meng Lee, http://www.devx.com/wireless/Article/39101

 

Currently rated 3.3 by 6 people

  • Currently 3.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Internet Explorer is Driving Me Mad

by Randall 6/2/2009 3:04:00 PM

Currently I'm trying to learn jQuery and jQueryUI.  Both libraries are supposed to foster compatibilty between all browsers.

Why is it that once again IE is the bastard child?

Firefox gets it right.

 

Safari gets it right.

 

Chrome joins the club by rendering the page correctly.

 

  Here comes IE ...

 

Here are the choices for the question I pose in the IE screencap:

  1. Normal abnormal behaviour
  2. My problem
  3. Microsoft bullshi*
  4. Ignoring W3 standards
  5. All of the above

If you chose 5, you guessed it.

Unfortunately the worst part is answer #2.  At least it keeps me busy.

Currently rated 3.0 by 5 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

Mission Accomplished

by Randall 5/31/2009 8:21:00 AM

I started this blog with the sole intent of sharing things that I have found useful.  While I've not done a very good job of posting regularly, I've also purposely not posted things just to say that I 'post regularly'.

I was browsing my StatCounter statistics this morning, when I noticed something that made me smile.  Someone in a forum referred to my NUnit tutorial in their post as a good intro. 

 

(By the way, if you're unfamiliar with StatCounter, I suggest you check it out)

Currently rated 1.7 by 31 people

  • Currently 1.709677/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

jQuery & Visual Studio 2010 Beta

by Randall 5/29/2009 2:30:00 PM

Today I decided to check out the jQuery support within Visual Studio 2010.  I had been having difficulty in getting IntelliSense to work correctly in Visual Studio 2008 SP1 as described by Scott Guthrie here.  Turns out that VS wasn't allowing jQuery and jQuery UI to play along nicely.  If I removed the script include for jQuery UI, IntelliSense would begin functioning for jQuery as it was supposed to. However, I'm currently trying to learn both of them so I want it to work properly.

After firing up Visual Studio 2010 Beta, I created a new Web Application Project: 



By the way, you might be happy to see that the option for a website has been removed and you're now forced to choose a web application project.  Personally I'm glad to see this.

Getting back to it, I immediately went into my code behind and started typing up a script block - I didn't see anything any jQuery IntelliSense at all.  DOH.  jQuery is SUPPORTED, not PROVIDED.  After including the scripts for jQuery and jQuery UI, IntelliSense began working beautifully providing support for both.

For jQuery ...

 

... and for jQuery UI ... 

 

Currently rated 3.3 by 6 people

  • Currently 3.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Supressing Submit Behaviour

by Randall 3/30/2009 10:39:00 AM

Yesterday I needed to supress the submit functionality of some HtmlInputImage controls (aka <input type="submit" ... /> ) and found the answer with a bit of Googling.  I'm posting it here with what is (hopefully) a better heading for the solution.   Perhaps Google will index this page and make it easier for someone else to figure out.

In your form tag, use the following:

<form id="form1" runat="server" onsubmit="return false"

This will prevent the standard submit behaviour and allow your onclick JavaScript to run as you expected. 

Currently rated 3.0 by 5 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Hilarious

by Randall 2/3/2009 10:07:00 AM

I ran across this today while doing some reading on CSS ...

http://giveupandusetables.com/ 

Enjoy. 

Currently rated 3.0 by 5 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

This Was Probably Sheer Luck ...

by Randall 7/31/2008 1:11:00 PM

I just spent part of two days battling an exception while waiting on a response from a web service. 

    ---> System.Net.WebException: The server committed a protocol violation. Section=ResponseStatusLine at System.Web.Services.Protocols.WebClientProtocol.GetWebResponse(WebRequest     request)

   at System.Web.Services.Protocols.HttpWebClientProtocol.GetWebResponse(WebRequest request)

   at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters) 

I found all sorts of suggestions to fix the issue.  Most of them involved adding the following information to your web.config:

    <system.net>
        <settings>
            <httpWebRequest useUnsafeHeaderParsing="true" />
        </settings>
    </system.net> 

No luck.

I even found a post that suggested you override the WebRequest method within your proxy class (seems like a bad idea because if you ever regenerate your proxy class ... well, you know what'll happen to your "fix").

protected override WebRequest GetWebRequest(Uri uri)
{
     HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(uri);

     request.ProtocolVersion = HttpVersion.Version10;

     return request;
} 

Anyway, after neither of those solutions had worked, my Google resources were running low.  I kept noticing that all of the posts mentioned .Net 1.1 ...

So I changed the webservice to use 2.0 (this is a COTS product and installs to 1.1 by default) and voila.  No more nasty gram.

Perhaps this will help someone.

Currently rated 3.3 by 6 people

  • Currently 3.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Fixes

String.Format Shortcut

by Randall 6/24/2008 5:19:00 PM

Well, I feel like I have totally missed the boat on something.

I love using string.Format() in order to construct messages and other things that need to be written to the console, etc.  For example:

    string msg = string.Format("I can't believe it's {0} already!", DateTime.Now.Date.ToShortDateString());
  Console.WriteLine(msg);

Anyway.  I just learned that the line above can be written as:

    Console.WriteLine("I can't believe it's {0} already!", DateTime.Now.Date.ToShortDateString());

Seems as if the compiler can determine that you're formatting a string and will infer the string.Format() function.

Too bad it can't infer the following:

    string msg = "I can't believe it's {0} already!", DateTime.Now.Date.ToShortDateString();

Currently rated 2.8 by 11 people

  • Currently 2.818182/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Powered by BlogEngine.NET 1.3.1.0
Theme by Mads Kristensen

About the author

Name of author Randall Sexton
Currently a .Net developer for Bechtel Corporation in Oak Ridge, TN.

E-mail me Send mail

Calendar

<<  April 2014  >>
MoTuWeThFrSaSu
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

View posts in large calendar

Pages

    Recent comments

    Don't show

    Authors

    Categories


    Logo Credit

    My logo was taken from CodingHorror.
    Jeff Atwood © Copyright 2007

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2014

    Sign in