示例#1
0
        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("]");
        }
示例#2
0
        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("]");
        }
示例#3
0
        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("}");
        }
示例#4
0
        public static string ToString(object obj)
        {
            JsonStringify js = new JsonStringify();

            return(js.Stringify(obj));
        }