static private void SerializeArray(JsonStringify js, object obj) { IEnumerable ie = (IEnumerable)obj; IEnumerator it = ie.GetEnumerator(); it.Reset(); bool isFirst = true; js.sb.Append("["); while (it.MoveNext()) { if (isFirst) { isFirst = false; } else { js.sb.Append(","); } object o = it.Current; js.Stringify(o); } js.sb.Append("]"); }
static private void SerializeTuple(JsonStringify js, object obj) { Type objType = obj.GetType(); var props = objType.GetProperties(); bool isFirst = true; js.sb.Append("["); foreach (System.Reflection.PropertyInfo prop in props) { if (isFirst) { isFirst = false; } else { js.sb.Append(","); } object o = prop.GetValue(obj, null); js.Stringify(o); } js.sb.Append("]"); }
static private void SerializeDictionary(JsonStringify js, object obj) { IEnumerable ie = (IEnumerable)obj; IEnumerator it = ie.GetEnumerator(); it.Reset(); bool isFirst = true; js.sb.Append("{"); IDictionaryEnumerator id = (IDictionaryEnumerator)it; while (it.MoveNext()) { if (isFirst) { isFirst = false; } else { js.sb.Append(","); } object k = id.Key; if (Type.GetTypeCode(k.GetType()) != TypeCode.String) { throw UnsupportedTypeException.DictionaryKey(k); } js.Stringify(k); js.sb.Append(":"); object v = id.Value; js.Stringify(v); } js.sb.Append("}"); }
public static string ToString(object obj) { JsonStringify js = new JsonStringify(); return(js.Stringify(obj)); }