From 72ec888c452a53f15ec202aa77d80d44f46ce8f7 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Fri, 30 Aug 2019 12:41:45 +0200 Subject: [PATCH] Fixed String escaping --- JSONString.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/JSONString.cs b/JSONString.cs index 9455f71..4b4726a 100644 --- a/JSONString.cs +++ b/JSONString.cs @@ -1,5 +1,7 @@ using System; using System.Text; +using System.Collections.Generic; +using System.Linq; namespace ln.json { @@ -16,6 +18,8 @@ namespace ln.json System.Tuple.Create('t','\t'), }; + static HashSet escapeSet = new HashSet(escapeCharacters.Select((x) => x.Item2)); + public string Value { get; private set; } public override object ToNative() => Value; @@ -31,11 +35,9 @@ namespace ln.json } public static string Escape(string source){ - StringBuilder sb = new StringBuilder(); + StringBuilder sb = new StringBuilder(); foreach (char ch in source){ - if ((ch >= 0x20) && (ch < 128)) { - sb.Append(ch); - } else if (ch < 0x20) { + if (escapeSet.Contains(ch)) { foreach (System.Tuple repl in escapeCharacters) { if (repl.Item2 == ch) { sb.Append("\\"); @@ -43,6 +45,10 @@ namespace ln.json break; } } + } + else if (ch < 128) + { + sb.Append(ch); } else { byte[] bytes = BitConverter.GetBytes((int)ch); sb.Append("\\u");