This project has moved and is read-only. For the latest updates, please go here.

Naughty strings

May 6, 2009 at 2:08 AM
I have a situation where the string content could be something like:

It's a good day

Which poses several problems using
Newtonsoft.Json.Linq.JObject jsObj = new Newtonsoft.Json.Linq.JObject();
jsObj.Add(Name, new Newtonsoft.Json.Linq.JValue(Value));
System.IO.StringWriter sw = new System.IO.StringWriter();
JsonWriter jwriter = new JsonTextWriter(sw);
jsObj.WriteTo(jwriter, new Newtonsoft.Json.Converters.JavaScriptDateTimeConverter());

The string in the StringWriter object comes out with \n in it and unescaped ' characters. This causes a nice error in javascript of Unterminated string constant when pumped into eval. Am I missing something? I would rather not do sw.Replace("\n","\\\\n").Replace("'","''")... to clean up the output.


May 6, 2009 at 11:05 AM
Your example ends up as:

"It's a good day\r\n\"sunshine\""

..which is a valid JavaScript string. What's the issue?
May 6, 2009 at 3:41 PM
Actually, my string ends up as:

"It's a good day

once it hits HTML. Now I am afraid that this is a result of a different library making a mess of this library's output. :-(