protected override string stringifyValue(int indentLevel, JSONStringifyOptions options) { var t = new StringBuilder(); bool hasItemsInList = false; t.Append(CHAR_START); foreach (KeyValuePair <string, object> valueItem in value) { if (hasItemsInList) { t.Append(CHAR_ITEM_SEPARATOR); } t.Append(options.lineFeed + JSONEncoder.getIndents(options.individualIndent, indentLevel + 1)); t.Append(JSONEncoder.encode(valueItem.Key, 0, options)); t.Append(options.objectAfterKey + CHAR_KEY_SEPARATOR + options.objectBeforeValue); t.Append(JSONEncoder.encode(valueItem.Value, indentLevel + 1, options)); hasItemsInList = true; } t.Append(options.lineFeed + JSONEncoder.getIndents(options.individualIndent, indentLevel)); t.Append(CHAR_END); return(t.ToString()); }
protected override string stringifyValue(int indentLevel, JSONStringifyOptions options) { var t = new StringBuilder(); bool hasItemsInList = false; t.Append(CHAR_START); foreach (object valueItem in value) { if (hasItemsInList) { t.Append(CHAR_ITEM_SEPARATOR); } if (options.lineFeedOnArrays) { t.Append(options.lineFeed + JSONEncoder.getIndents(options.individualIndent, indentLevel + 1)); } else { t.Append(options.arrayAfterSeparator); } t.Append(JSONEncoder.encode(valueItem, indentLevel + 1, options)); hasItemsInList = true; } t.Append(options.lineFeed + JSONEncoder.getIndents(options.individualIndent, indentLevel)); t.Append(CHAR_END); return(t.ToString()); }
// ================================================================================================================ // PUBLIC INTERFACE ----------------------------------------------------------------------------------------------- /** * Encodes an object into a string */ public static string encode(object value, bool prettyPrint = false) { if (prettyPrint) { return(encode(value, 0, JSONStringifyOptions.getPretty())); } else { return(encode(value, 0, JSONStringifyOptions.getCompact())); } }
public static JSONStringifyOptions getPretty() { var o = new JSONStringifyOptions(); o.individualIndent = "\t"; o.lineFeed = "\n"; o.arrayAfterSeparator = " "; o.lineFeedOnArrays = true; o.objectAfterKey = " "; o.objectBeforeValue = " "; return(o); }
protected override string stringifyValue(int indentLevel, JSONStringifyOptions options) { // Encodes a string properly, escaping chars that need escaping var t = new StringBuilder(); int i; char c; int charPos; string unicodeNumber; t.Append(CHAR_START); for (i = 0; i < value.Length; i++) { c = value[i]; //Debug.Log("CHAR => " + (int)c + " = [" + c + "]"); charPos = CHARS_STRING_NEED_ESCAPE.IndexOf(c); if (charPos >= 0) { // Special char: needs to be escaped t.Append(CHARS_STRING_ESCAPED[charPos]); } else if ((int)c < 0x20) { // Outside the range: needs to be escaped unicodeNumber = ((int)c).ToString("X"); t.Append(STRING_ESCAPE_UNICODE + unicodeNumber.PadLeft(4, '0').ToLowerInvariant()); } else if ((int)c >= 0xc200) { // Multi byte char // c2..fd means initial bytes of unicode, fe..ff means the char bytes themselves unicodeNumber = ((int)c).ToString("X"); t.Append(STRING_ESCAPE_UNICODE + unicodeNumber.PadLeft(4, '0').ToLowerInvariant()); } else { // Normal char t.Append(c); } } t.Append(CHAR_END); return(t.ToString()); }
protected override string stringifyValue(int indentLevel, JSONStringifyOptions options) { if (value == null) { // It's a null return(LITERAL_NULL); } else if (value is bool) { // It's a Boolean return((bool)value ? LITERAL_TRUE : LITERAL_FALSE); } else { Debug.LogError("Invalid literal value"); return(null); } }
/** * Encodes an object into a string, correctly identifying the type */ public static string encode(object value, int indentLevel, JSONStringifyOptions options) { // Decides what it is if (JSONArray.matchesValue(value)) { return(new JSONArray(value).ToString(indentLevel, options)); } else if (JSONString.matchesValue(value)) { return(new JSONString(value).ToString(indentLevel, options)); } else if (JSONNumber.matchesValue(value)) { return(new JSONNumber(value).ToString(indentLevel, options)); } else if (JSONLiteral.matchesValue(value)) { return(new JSONLiteral(value).ToString(indentLevel, options)); } else { return(new JSONObject(value).ToString(indentLevel, options)); } }
// ================================================================================================================ // STATIC INTERFACE ----------------------------------------------------------------------------------------------- public static JSONStringifyOptions getCompact() { var o = new JSONStringifyOptions(); return(o); }
protected override string stringifyValue(int indentLevel, JSONStringifyOptions options) { return(value.ToString()); }
protected abstract string stringifyValue(int indentLevel, JSONStringifyOptions options);
public string ToString(int indentLevel, JSONStringifyOptions options) { return(stringifyValue(indentLevel, options)); }