public static string WriteArray(ArrayContainer value, int indent, JIndentCfg cfg) { string tmp = "[\n"; if (indent != 0 && cfg.IndentOpenCurlyBrace) { tmp = "\n" + StringUtil.PrependChar(tmp, cfg.IndentString, indent); } for (int x = -1; ++x < value.Count - 1;) { tmp += StringUtil.PrependChar(WriteValue(value[x], indent + 1, cfg), cfg.IndentString, indent + 1); tmp += ",\n"; for (int i = -1; ++i < cfg.ArraySpacing;) { tmp += cfg.IndentSpacing ? StringUtil.PrependChar("\n", cfg.IndentString, indent + 1) : "\n"; } } if (1 <= value.Count) { tmp += StringUtil.PrependChar(WriteValue(value[value.Count - 1], indent + 1, cfg), cfg.IndentString, indent + 1); } return(tmp + "\n" + StringUtil.PrependChar("]", cfg.IndentString, indent)); }
public static string WriteObject(StringContainer value, int indent, JIndentCfg cfg) { string tmp = "{\n"; if (indent != 0 && cfg.IndentOpenCurlyBrace) { tmp = "\n" + StringUtil.PrependChar(tmp, cfg.IndentString, indent); } IEnumerator <string> keys = value.Keys.GetEnumerator(); for (int x = -1; ++x < value.Count - 1;) { keys.MoveNext(); tmp += StringUtil.PrependChar(WriteMember(keys.Current, value[keys.Current], indent + 1, cfg), cfg.IndentString, indent + 1); tmp += ",\n"; for (int i = -1; ++i < cfg.ObjectSpacing;) { tmp += cfg.IndentSpacing ? StringUtil.PrependChar("\n", cfg.IndentString, indent + 1) : "\n"; } } if (keys.MoveNext()) { tmp += StringUtil.PrependChar(WriteMember(keys.Current, value[keys.Current], indent + 1, cfg), cfg.IndentString, indent + 1); } return(tmp + "\n" + StringUtil.PrependChar("}", cfg.IndentString, indent)); }
public static string WriteValue(object value, int indent, JIndentCfg cfg) { if (value is StringContainer sc) { return(WriteObject(sc, indent, cfg)); } else if (value is ArrayContainer ac) { return(WriteArray(ac, indent, cfg)); } else if (value is JLeaf) { if (value is ManagedNull) { return(WriteNull()); } else if (value is ManagedNumber mn) { return(WriteNumber((BigRational)mn)); } else if (value is ManagedString ms) { return(WriteString(ms)); } else { return(WriteBoolean((ManagedBoolean)value)); } } else { throw new Exception("Value is not a proper JSON value"); } }
public static string WriteMember(string key, object value, int indent, JIndentCfg cfg) { string tmp = "\"" + key + "\"" + cfg.StuffingAfterMemberKey + ":"; if (!(value is Container && cfg.IndentOpenCurlyBrace)) { tmp += cfg.StuffingBeforeMemberValue; } return(tmp + WriteValue(value, indent, cfg)); }
public string ToJSONString(JIndentCfg cfg) { if (this is JContainer jc) { return(JSONCore.WriteValue(jc.content, 0, cfg)); } else { return(JSONCore.WriteValue(this, 0, cfg)); } }