2017-11-03 13:13:09 +01:00
|
|
|
using System;
|
2017-10-26 16:41:14 +02:00
|
|
|
using System.Text;
|
2019-08-30 12:41:45 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2017-10-26 16:41:14 +02:00
|
|
|
|
2019-08-07 23:02:00 +02:00
|
|
|
namespace ln.json
|
2017-10-26 16:41:14 +02:00
|
|
|
{
|
2019-08-07 23:02:00 +02:00
|
|
|
public class JSONString : JSONValue
|
2017-10-26 16:41:14 +02:00
|
|
|
{
|
|
|
|
static System.Tuple<char,char>[] escapeCharacters = {
|
|
|
|
System.Tuple.Create('\\','\\'),
|
|
|
|
System.Tuple.Create('/','/'),
|
|
|
|
System.Tuple.Create('"','"'),
|
|
|
|
System.Tuple.Create('b','\b'),
|
|
|
|
System.Tuple.Create('f','\f'),
|
|
|
|
System.Tuple.Create('n','\n'),
|
|
|
|
System.Tuple.Create('r','\r'),
|
|
|
|
System.Tuple.Create('t','\t'),
|
|
|
|
};
|
|
|
|
|
2019-08-30 12:41:45 +02:00
|
|
|
static HashSet<char> escapeSet = new HashSet<char>(escapeCharacters.Select((x) => x.Item2));
|
|
|
|
|
2019-08-07 23:02:00 +02:00
|
|
|
public string Value { get; private set; }
|
2019-08-19 14:14:15 +02:00
|
|
|
public override object ToNative() => Value;
|
2017-10-26 16:41:14 +02:00
|
|
|
|
2019-08-07 23:02:00 +02:00
|
|
|
public JSONString(String value)
|
|
|
|
:base(JSONValueType.STRING)
|
2017-10-26 16:41:14 +02:00
|
|
|
{
|
2019-11-15 13:55:10 +01:00
|
|
|
Value = value == null ? String.Empty : value;
|
2017-10-26 16:41:14 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 23:02:00 +02:00
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return String.Format("\"{0}\"", Escape(Value));
|
|
|
|
}
|
2017-11-03 13:13:09 +01:00
|
|
|
|
2019-08-07 23:02:00 +02:00
|
|
|
public static string Escape(string source){
|
2019-08-30 12:41:45 +02:00
|
|
|
StringBuilder sb = new StringBuilder();
|
2017-10-26 16:41:14 +02:00
|
|
|
foreach (char ch in source){
|
2019-08-30 12:41:45 +02:00
|
|
|
if (escapeSet.Contains(ch)) {
|
2019-08-07 23:02:00 +02:00
|
|
|
foreach (System.Tuple<char, char> repl in escapeCharacters) {
|
|
|
|
if (repl.Item2 == ch) {
|
|
|
|
sb.Append("\\");
|
|
|
|
sb.Append(repl.Item1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-08-30 12:41:45 +02:00
|
|
|
}
|
|
|
|
else if (ch < 128)
|
|
|
|
{
|
|
|
|
sb.Append(ch);
|
2019-08-07 23:02:00 +02:00
|
|
|
} else {
|
|
|
|
byte[] bytes = BitConverter.GetBytes((int)ch);
|
|
|
|
sb.Append("\\u");
|
|
|
|
sb.AppendFormat("{0:x2}{1:x2}", bytes[1], bytes[0]);
|
|
|
|
}
|
2017-10-26 16:41:14 +02:00
|
|
|
}
|
|
|
|
return sb.ToString();
|
|
|
|
}
|
2017-11-03 13:13:09 +01:00
|
|
|
|
2019-08-07 23:02:00 +02:00
|
|
|
public static string Unescape(string jsonString)
|
2017-11-03 13:13:09 +01:00
|
|
|
{
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2019-08-07 23:02:00 +02:00
|
|
|
CharEnumerator ce = jsonString.GetEnumerator();
|
2017-11-03 13:13:09 +01:00
|
|
|
char[] hex = new char[4];
|
|
|
|
|
|
|
|
while (ce.MoveNext()){
|
|
|
|
|
|
|
|
if (ce.Current == '\\'){
|
|
|
|
ce.MoveNext();
|
|
|
|
|
2019-08-07 23:02:00 +02:00
|
|
|
if (ce.Current == 'u')
|
|
|
|
{
|
|
|
|
for (int n = 0; n < 4; n++)
|
|
|
|
{
|
|
|
|
ce.MoveNext();
|
|
|
|
hex[3 - n] = ce.Current;
|
|
|
|
}
|
|
|
|
sb.Append((char)Convert.ToInt16(new String(hex),16));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach (System.Tuple<char, char> repl in escapeCharacters)
|
|
|
|
{
|
|
|
|
if (repl.Item1 == ce.Current)
|
|
|
|
{
|
|
|
|
sb.Append(repl.Item2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-03 13:13:09 +01:00
|
|
|
} else {
|
|
|
|
sb.Append(ce.Current);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sb.ToString();
|
|
|
|
}
|
2017-10-26 16:41:14 +02:00
|
|
|
}
|
|
|
|
}
|