Esempio n. 1
0
        public string Stringify(object obj)
        {
            if (obj == null)
            {
                SerializeNull(this);
                return(sb.ToString());
            }

            string typeFullName = obj.GetType().FullName;
            int    idx          = typeFullName.IndexOf('`');

            if (idx != -1)
            {
                typeFullName = typeFullName.Substring(0, idx);
            }

            if (_serializeDict.TryGetValue(typeFullName, out Serialize serializer))
            {
                serializer(this, obj);
            }
            else if (obj is IEnumerable)
            {
                SerializeIEnumerable(this, obj);
            }
            else
            {
                throw UnsupportedTypeException.Create(obj);
            }

            return(sb.ToString());
        }
Esempio n. 2
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("}");
        }