Exemple #1
0
        ///////////////////////////////////////////////////////////////////////

        public static void SerializeDataTable(BinaryTypesWriter bw, DataTable table)
        {
            Type[] columnTypes = new Type[table.Columns.Count];

            //write table name
            bw.Write(table.TableName);

            //write columns count
            bw.Write7BitInt(table.Columns.Count);

            for (int i = 0; i < columnTypes.Length; i++)
            {
                //write column name and type
                bw.Write(table.Columns[i].ColumnName);
                bw.Write(table.Columns[i].DataType.FullName);

                columnTypes[i] = table.Columns[i].DataType;
            }

            //write rows count
            bw.Write7BitInt(table.Rows.Count);

            foreach (DataRow row in table.Rows)
            {
                for (int i = 0; i < columnTypes.Length; i++)
                {
                    if (row.IsNull(i))
                    {
                        bw.Write("");
                        continue;
                    }

                    if (columnTypes[i] == typeof(System.String))
                    {
                        bw.Write((string)row[i]);
                    }
                    else if (columnTypes[i] == typeof(System.Int32))
                    {
                        bw.Write7BitInt((int)row[i]);
                    }
                    else if (columnTypes[i] == typeof(System.Int64))
                    {
                        bw.Write7BitLong((long)row[i]);
                    }
                    else if (columnTypes[i] == typeof(System.Decimal))
                    {
                        bw.WriteCompactDecimal((decimal)row[i]);
                    }
                    else if (columnTypes[i] == typeof(System.DateTime))
                    {
                        bw.WriteCompactDateTime((DateTime)row[i], TimeSpan.TicksPerMillisecond * 100);
                    }
                    else if (columnTypes[i] == typeof(bool))
                    {
                        bw.Write((bool)row[i]);
                    }
                }
            }
        }
Exemple #2
0
 public static void WriteCompactDecimal(BinaryTypesWriter bw, decimal val, object objParam)
 {
     bw.WriteCompactDecimal(val);
 }
Exemple #3
0
        private static void SerializeObject <T>(T value, BinaryTypesWriter bw)
        {
            var type = value.GetType();

            if (type.IsPrimitive)
            {
                switch (type.FullName)
                {
                case SystemTypeDefs.FullNameBoolean:
                case SystemTypeDefs.FullNameByte:
                case SystemTypeDefs.FullNameSByte:
                case SystemTypeDefs.FullNameInt16:
                case SystemTypeDefs.FullNameUInt16:
                case SystemTypeDefs.FullNameInt32:
                case SystemTypeDefs.FullNameUInt32:
                case SystemTypeDefs.FullNameInt64:
                case SystemTypeDefs.FullNameUInt64:
                case SystemTypeDefs.FullNameChar:
                {
                    var val = Convert.ChangeType(value, typeof(long));
                    bw.Write7BitLong((long)val);
                    break;
                }

                case SystemTypeDefs.FullNameSingle:     // todo: compact
                    bw.Write((float)(object)value);
                    break;

                case SystemTypeDefs.FullNameDouble:     // todo: compact
                    bw.Write((double)(object)value);
                    break;
                }
                return;
            }

            if (type.FullName == SystemTypeDefs.FullNameDecimal)
            {
                bw.WriteCompactDecimal((decimal)(object)value);
                return;
            }

            if (type.IsEnum)
            {
                bw.Write7BitLong((int)(object)value);
                return;
            }

            if (type.IsValueType)
            {
                switch (type.FullName)
                {
                case SystemTypeDefs.FullNameDateTime:
                    bw.Write7BitLong(((DateTime)(object)value).Ticks);
                    break;
                }
                return;
            }

            if (type.IsClass)
            {
                switch (type.FullName)
                {
                case SystemTypeDefs.FullNameString:
                    bw.Write((string)(object)value);
                    break;

                default:
                    SerializeClass((object)value, bw);
                    break;
                }
                return;
            }
        }