4

The Json.NET homepage says the project started out as :

a couple of static methods for escaping JavaScript strings

Do these methods still exist? I'm aware that JavaScriptSerializer can do this, but I'm after a one liner to do this.

SerializeObject can serialize a single string (which escapes it) but it returns the quotes

JsonConvert.SerializeObject(@"hi bud's  \no way\");

Returns: "hi bud's \\no way\\"

1 Answers1

7

I found a nice one-liner solution, but it has only been in the framework since 4.0. Would be nice to have something similar in Json.NET I can use on < 4.0 projects.

HttpUtility.JavaScriptStringEncode example:

HttpUtility.JavaScriptStringEncode(@"hi bud's  \no way\")

Returns: hi bud\u0027s \\no way\\