/* JSONSharp, a c# library for generating strings in JSON format Copyright (C) 2007 Jeff Rodenburg This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. If you have questions about the library, please contact me at jeff.rodenburg@gmail.com. */ using System; using System.Collections.Generic; using System.Text; using JSONSharp; namespace JSONSharp.Values { /// <summary> /// JSONStringValue is a collection of zero or more Unicode characters, wrapped in double quotes, /// using backslash escapes. A character is represented as a single character string. A string /// is very much like a C# string. /// </summary> public class JSONStringValue : JSONValue { private string _value; /// <summary> /// Public constructor that accepts a value of type string /// </summary> /// <param name="value">string value</param> public JSONStringValue(string value) : base() { this._value = value; } /// <summary> /// Required override of the ToString() method. /// </summary> /// <returns>contained string in JSON-compliant form</returns> public override string ToString() { return JSONStringValue.ToJSONString(this._value); } /// <summary> /// Required override of the PrettyPrint() method. /// </summary> /// <returns>this.ToString()</returns> public override string PrettyPrint() { return this.ToString(); } /// <summary> /// Evaluates all characters in a string and returns a new string, /// properly formatted for JSON compliance and bounded by double-quotes. /// </summary> /// <param name="text">string to be evaluated</param> /// <returns>new string, in JSON-compliant form</returns> public static string ToJSONString(string text) { List<string> output = new List<string>(); if (text != null) { char[] charArray = text.ToCharArray(); foreach (char c in charArray) { if (((int)c) == 8) //Backspace output.Add("\\b"); else if (((int)c) == 9) //Horizontal tab output.Add("\\t"); else if (((int)c) == 10) //Newline output.Add("\\n"); else if (((int)c) == 12) //Formfeed output.Add("\\f"); else if (((int)c) == 13) //Carriage return output.Add("\\r"); else if (((int)c) == 34) //Double-quotes (") output.Add("\\" + c.ToString()); /* else if (((int)c) == 44) //Comma (,) output.Add("\\" + c.ToString()); else if (((int)c) == 47) //Solidus (/) output.Add("\\" + c.ToString()); */ else if (((int)c) == 92) //Reverse solidus (\) output.Add("\\" + c.ToString()); else if (((int)c) > 31) output.Add(c.ToString()); //TODO: add support for hexadecimal } } return "\"" + string.Join("", output.ToArray()) + "\""; } } }