Exemple #1
0
        private void Write(int depth, object obj)
        {
            if (obj == null)
            {
                WriteLiteral("null");
                return;
            }

            Type objType = obj.GetType();

            switch (Type.GetTypeCode(objType))
            {
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
                WriteNumber((IConvertible)obj);
                return;

            case TypeCode.Double:
            case TypeCode.Decimal:
            case TypeCode.Single:
                WriteNumber((IFormattable)obj);
                return;

            case TypeCode.String:
                WriteQuotedString((string)obj);
                return;

            case TypeCode.Char:
                WriteQuotedString((char)obj);
                return;

            case TypeCode.Boolean:
                WriteBool((bool)obj);
                return;

            case TypeCode.DBNull:
                WriteLiteral("null");
                return;

            case TypeCode.DateTime:
                WriteDateTime((System.DateTime)obj);
                return;
            }

            if (objType.IsArray)
            {
                Type eleType = objType.GetElementType();
                switch (Type.GetTypeCode(eleType))
                {
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64:
                    WriteArray(depth, (IConvertible[])obj, WriteNumber);
                    return;

                case TypeCode.Double:
                case TypeCode.Decimal:
                case TypeCode.Single:
                    WriteArray(depth, (IFormattable[])obj, WriteNumber);
                    return;

                case TypeCode.String:
                    WriteArray(depth, (string[])obj, WriteQuotedString);
                    return;

                case TypeCode.Char:
                    WriteArray(depth, (char[])obj, WriteQuotedString);
                    return;

                case TypeCode.Boolean:
                    WriteArray(depth, (bool[])obj, WriteBool);
                    return;

                case TypeCode.DBNull:
                    WriteLiteral("null");
                    return;

                case TypeCode.DateTime:
                    WriteArray(depth, (DateTime[])obj, WriteDateTime);
                    return;
                }

                IConvertible[] arrConv = obj as IConvertible[];
                if (arrConv != null)
                {
                    WriteArray(depth, arrConv, WriteNumber);
                    return;
                }
            }

            JsonObject objDict = obj as JsonObject;

            if (objDict != null)
            {
                WriteDictionary(depth, objDict);
                return;
            }

            JsonArray objList = obj as JsonArray;

            if (objList != null)
            {
                WriteArray(depth, objList);
                return;
            }

            IJsonObject objObj = obj as IJsonObject;

            if (objObj != null)
            {
                var ocdict = objObj.SerializeJson();
                WriteDictionary(depth, ocdict);
                return;
            }

            IEnumerable objEnum = obj as IEnumerable;

            if (objEnum != null && objType.IsSerializable)
            {
                WriteEnumerable(depth, objEnum);
                return;
            }

            IJsonBinary objBinary = obj as IJsonBinary;

            if (objBinary != null)
            {
                WriteBinary(objBinary);
                return;
            }

            Bitmap objBitmap = obj as Bitmap;

            if (objBitmap != null)
            {
                WriteImage(objBitmap);
                return;
            }

            throw new JsonException(string.Format("Object of type {0} is not serializable.", objType.Name));
        }
Exemple #2
0
        private void SerializeObject(object obj, int depth)
        {
            if (obj == null)
            {
                WriteBytes("null"); return;
            }

            Type t = obj.GetType();

            switch (Type.GetTypeCode(t))
            {
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
                SerializeConvertible((IConvertible)obj); return;

            case TypeCode.Double:
            case TypeCode.Decimal:
            case TypeCode.Single:
                SerializeFloat((IFormattable)obj); return;

            case TypeCode.String:
                SerializeString((string)obj); return;

            case TypeCode.Char:
                SerializeCharacter((char)obj); return;

            case TypeCode.Boolean:
                SerializeBool((bool)obj); return;

            case TypeCode.DBNull:
                WriteBytes("null"); return;

            case TypeCode.DateTime:
                SerializeDateTime((System.DateTime)obj); return;
            }

            if (t.IsArray)
            {
                Type te = t.GetElementType();
                switch (Type.GetTypeCode(te))
                {
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64:
                    SerializeArray((IConvertible[])obj, SerializeConvertible); return;

                case TypeCode.Double:
                case TypeCode.Decimal:
                case TypeCode.Single:
                    SerializeArray((IFormattable[])obj, SerializeFloat); return;

                case TypeCode.String:
                    SerializeArray((string[])obj, SerializeString); return;

                case TypeCode.Char:
                    SerializeArray((char[])obj, SerializeCharacter); return;

                case TypeCode.Boolean:
                    SerializeArray((bool[])obj, SerializeBool); return;

                case TypeCode.DBNull:
                    WriteBytes("null"); return;

                case TypeCode.DateTime:
                    SerializeArray((DateTime[])obj, SerializeDateTime); return;
                }

                IConvertible[] arrConv = obj as IConvertible[];
                if (arrConv != null)
                {
                    SerializeArray(arrConv, SerializeConvertible); return;
                }
            }

            JsonObject objDict = obj as JsonObject;

            if (objDict != null)
            {
                SerializeDictionary(objDict, depth); return;
            }

            JsonArray objList = obj as JsonArray;

            if (objList != null)
            {
                SerializeArray(objList, depth); return;
            }

            IJsonObject objObj = obj as IJsonObject;

            if (objObj != null)
            {
                var ocdict = objObj.SerializeJson();
                SerializeDictionary(ocdict, depth); return;
            }

            IEnumerable objEnum = obj as IEnumerable;

            if (objEnum != null && t.IsSerializable)
            {
                SerializeEnumerable(objEnum, depth); return;
            }

            IJsonBinary objBinary = obj as IJsonBinary;

            if (objBinary != null)
            {
                SerializeBinary(objBinary); return;
            }

            throw new JsonException(string.Format(
                                        "Object of type {0} is not serializable\n{1}{0}"
                                        , t.Name, this.DebugValue));
        }