Possible Duplicate:
Using StringEscapeHandling.EscapeNonAscii with Json.NET
The latest release of Json.NET (4.5.11) contains StringEscapeHandling.EscapeNonAscii
, which escapes all non ASCII chars. But, I can't figure out how to use it. The docs don't seem to have any examples that I can find (only a release note announcing it).
Could someone show an example of serializing an object into a JSON string, using EscapeNonAscii
?
Maybe this example can help
string obj = "abcn\n\rüö&/<>";
Console.WriteLine(Serialize(obj, StringEscapeHandling.Default));
Console.WriteLine(Serialize(obj, StringEscapeHandling.EscapeHtml));
Console.WriteLine(Serialize(obj, StringEscapeHandling.EscapeNonAscii));
public static string Serialize(object o,StringEscapeHandling stringEscapeHandling)
{
StringWriter wr = new StringWriter();
var jsonWriter = new JsonTextWriter(wr);
jsonWriter.StringEscapeHandling = stringEscapeHandling;
new JsonSerializer().Serialize(jsonWriter,o);
return wr.ToString();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With