using System; using System.Text; namespace sharp.json { public class JSONString : JSON { static System.Tuple[] 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'), }; string value; public JSONString() :base(JSONTypes.String) { this.value = ""; } public JSONString(String value) :base(JSONTypes.String) { this.value = value; } public override string prettyPrint(int d = 0) { return string.Format("\"{0}\"",escape(value)); } public override string ToString() { return prettyPrint(); } public static string escape(string source){ StringBuilder sb = new StringBuilder(); foreach (char ch in source){ if ((ch >= 0x20) && (ch < 128)){ sb.Append(ch); } else if (ch < 0x20){ foreach (Tuple repl in escapeCharacters){ if (repl.Item2 == ch){ sb.Append("\\"); sb.Append(repl.Item1); break; } } } else { int iv = (int)ch; sb.Append("\\u"); sb.Append("____"); Console.WriteLine("JSON WARNING: UNICODE ESCAPE SEQUENCES ARE NOT IMPLEMENTED YET"); } } return sb.ToString(); } } }