コード例 #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]);
                    }
                }
            }
        }
コード例 #2
0
        //===============================================================================================
        // Serialize
        //===============================================================================================

        public static byte[] SerializeObject <T>(T value, SerializerSettings settings)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryTypesWriter bw = new BinaryTypesWriter(ms))
                {
                    SerializeObject(typeof(T), value, bw, settings, null, null);
                    return(ms.ToArray());
                }
            }
        }
コード例 #3
0
 public static byte[] SerializeObject <T>(T value)
 {
     using (MemoryStream ms = new MemoryStream())
     {
         using (BinaryTypesWriter bw = new BinaryTypesWriter(ms))
         {
             SerializeObject(value, bw);
             return(ms.ToArray());
         }
     }
 }
コード例 #4
0
        private static void SerializeClass <T>(T value, BinaryTypesWriter bw) where T : class
        {
            var props = value.GetType().GetProperties()
                        .OrderBy(x => x.MetadataToken)
                        .ToList();

            foreach (var prop in props)
            {
                var val = prop.GetValue(value);
                SerializeObject(val, bw);
            }
        }
コード例 #5
0
        ///////////////////////////////////////////////////////////////////////

        public static void SerializeDictionary <K, V>(
            BinaryTypesWriter bw,
            IDictionary <K, V> map,
            WriteDataDelegate <K> keyWriter,
            object keyObjParam,
            WriteDataDelegate <V> ValWriter,
            object valObjParam)
        {
            bw.Write7BitInt(map.Count);
            foreach (KeyValuePair <K, V> keyValue in map)
            {
                keyWriter(bw, keyValue.Key, keyObjParam);
                ValWriter(bw, keyValue.Value, valObjParam);
            }
        }
コード例 #6
0
        ///////////////////////////////////////////////////////////////////////

        public static void SerializeList <T>(BinaryTypesWriter bw, List <T> list, WriteDataDelegate <T> itemWriter, object objParam)
        {
            if (list != null)
            {
                bw.Write7BitInt(list.Count);
                foreach (T item in list)
                {
                    itemWriter(bw, item, objParam);
                }
            }
            else
            {
                bw.Write7BitInt((Int32)0);
            }
        }
コード例 #7
0
        internal static void SerializeObject <T>(Type type, T value, BinaryTypesWriter bw, SerializerSettings settings, ISerializer serializer, ISerializerArg serializerArg)
        {
            serializer = GetSerializer(type, serializer);
            if (serializer == null)
            {
                throw new Exception($"SerializeObject: serializer not found for type {type.FullName}");
            }

            if (type.IsClass && serializer.CommonNullHandle)
            {
                bw.Write(value != null);
                if (value == null)
                {
                    return;
                }
            }

            serializer.Serialize(bw, type, settings, serializerArg, value);
        }
コード例 #8
0
 public static void WriteBool(BinaryTypesWriter bw, bool val, object objParam)
 {
     bw.Write(val);
 }
コード例 #9
0
 public static void WriteCompactDecimal(BinaryTypesWriter bw, decimal val, object objParam)
 {
     bw.WriteCompactDecimal(val);
 }
コード例 #10
0
 public static void WriteLongsList(BinaryTypesWriter bw, List <long> val, object objParam)
 {
     SerializationUtils.SerializeList(bw, val, SerializationUtils.WriteLong, null);
 }
コード例 #11
0
 public static void WriteBytesArray(BinaryTypesWriter bw, byte[] val, object objParam)
 {
     bw.WriteBytesArray(val);
 }
コード例 #12
0
 public static void WriteLong(BinaryTypesWriter bw, long val, object objParam)
 {
     bw.Write7BitLong(val);
 }
コード例 #13
0
 public static void WriteEnum <T>(BinaryTypesWriter bw, T val, object objParam) where T : IConvertible
 {
     bw.Write7BitLong((long)Convert.ChangeType(val, typeof(long)));
 }
コード例 #14
0
 public static void WriteInt(BinaryTypesWriter bw, int val, object objParam)
 {
     bw.Write7BitInt(val);
 }
コード例 #15
0
 public static void WriteString(BinaryTypesWriter bw, string val, object objParam)
 {
     bw.Write(val);
 }
コード例 #16
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;
            }
        }