Does Newtonsoft.Json escape all characters?
c#, json, json.net
Solution
Does this library escape all potentially illegal characters, or should I escape the characters before attempting to serialize?
It would be a pretty rubbish library if it claimed to serialize to JSON and didn't handle doing so correctly.
Moreover, if you "escape" those characters before giving the data to the library, then the library will escape the escapes, and you won't get the result you intend. For instance, suppose I thought it wouldn't handle escaping formfeed (`\f`) characters, and so I did this to "escape" them: `mydata = mydata.replace(/\f/g, "\\f");` Now, `mydata` doesn't have a formfeed anymore, it has a backslash followed by the letter `f`. If you encode that to JSON, then decode it, it will be a backslash followed by the letter `f`, not a formfeed.
Problem
I'm saving my object data as JSON, using NewtonSoft.Json. Does this library escape all potentially illegal characters, or should I escape the characters before attempting to serialize? E.g if I have a Site object: ``` public class Site { public string SiteName { get; set; } } ... ... var site = new Site(); site.SiteName = "$$ what could I/Put 'here' %$%^*^(&*& to break this?"; var outputJson = JsonConvert.SerializeObject(site); ``` Is it possible for me to break the `SerializeObject()` method by adding unescaped illegal characters to it, or does NewtonSoft.Json do that for me?