Naughty strings

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

It's a good day
"sunshine"

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.

Thanks!

Coordinator
May 6, 2009 at 10: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 2:41 PM
Actually, my string ends up as:

"It's a good day
"sunshine""

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. :-(