Пример #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 SerializeString(JsonStringify js, object obj)
        {
            string str = (string)obj;

            js.sb.Append("\"");
            JsonStringEscaper.Escape(js.sb, str);
            js.sb.Append("\"");
        }
Пример #4
0
        static private void SerializeTimestamp(JsonStringify js, object obj)
        {
            DateTime userDate     = (DateTime)obj;
            var      univDateTime = userDate.ToUniversalTime();

            js.sb.Append("\"");
            js.sb.Append(univDateTime.ToString("yyyy-MM-dd HH:mm:ss"));
            js.sb.Append("\"");
        }
Пример #5
0
        static private void SerializeIEnumerable(JsonStringify js, object obj)
        {
            IEnumerable ie = (IEnumerable)obj;
            IEnumerator it = ie.GetEnumerator();

            it.Reset();

            if (it is IDictionaryEnumerator)
            {
                SerializeDictionary(js, obj);
            }
            else
            {
                SerializeArray(js, obj);
            }
        }
Пример #6
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("}");
        }
Пример #7
0
        public static string ToString(object obj)
        {
            JsonStringify js = new JsonStringify();

            return(js.Stringify(obj));
        }
Пример #8
0
 static private void SerializeNull(JsonStringify js)
 {
     js.sb.Append("null");
 }
Пример #9
0
        static private void SerializeUInteger(JsonStringify js, object obj)
        {
            UInt64 value = (UInt64)Convert.ChangeType(obj, TypeCode.UInt64);

            js.sb.Append(value);
        }
Пример #10
0
        static private void SerializeFloat(JsonStringify js, object obj)
        {
            float value = (float)obj;

            js.sb.Append(value);
        }
Пример #11
0
        static private void SerializeDouble(JsonStringify js, object obj)
        {
            double value = (double)obj;

            js.sb.Append(value);
        }
Пример #12
0
        static private void SerializeDecimal(JsonStringify js, object obj)
        {
            Decimal dec = (Decimal)obj;

            js.sb.Append(dec);
        }
Пример #13
0
        static private void SerializeBoolean(JsonStringify js, object obj)
        {
            Boolean v = (bool)obj;

            js.sb.Append(v);
        }