/// <summary>
 /// 获取指定输入的数据
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="index">索引</param>
 /// <returns></returns>
 public T GetData <T>(int index)
 {
     if (_serializetype == SerializeType.Newtonsoft)
     {
         return(JsonConvert.DeserializeObject <T>(_listjson[index]));
     }
     else if (_serializetype == SerializeType.protobuf)
     {
         byte[] bytes = System.Text.Encoding.UTF8.GetBytes(_listjson[index]);
         using (MemoryStream ms = new MemoryStream(bytes))
         {
             if (default(T) is DataTable)
             {
                 Object obj = DataSerializer.DeserializeDataTable(ms);
                 return((T)obj);
             }
             else
             {
                 return(Serializer.Deserialize <T>(ms));
             }
         }
     }
     else if (_serializetype == SerializeType.fastJSON)
     {
         return(fastJSON.JSON.ToObject <T>(_listjson[index]));
     }
     else
     {
         return(default(T));
     }
 }
예제 #2
0
        /// <summary>
        /// 获取指定输入的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="index">索引</param>
        /// <returns></returns>
        public T GetData <T>(int index)
        {
            if (typeof(T).Name == "DataTable")
            {
                List <string> dtjson = JsonConvert.DeserializeObject <List <string> >(_listjson[index]);
                Dictionary <string, string> colnumHead = JsonConvert.DeserializeObject <Dictionary <string, string> >(dtjson[0]);
                if (colnumHead.Count == 0)
                {
                    return(default(T));
                }
                DataTable dt = new DataTable();
                List <Dictionary <string, Object> > rowData = JsonConvert.DeserializeObject <List <Dictionary <string, Object> > >(dtjson[1]);
                foreach (var item in colnumHead)
                {
                    DataColumn dc = new DataColumn(item.Key, getColumnType(item.Value));
                    dt.Columns.Add(dc);
                }

                foreach (var item in rowData)
                {
                    DataRow dr = dt.NewRow();
                    foreach (var val in item)
                    {
                        dr[val.Key] = getRowValue(colnumHead[val.Key], val.Value);
                    }
                    dt.Rows.Add(dr);
                }
                return((T)((Object)dt));
            }
            else if (_serializetype == SerializeType.Newtonsoft)
            {
                return(JsonConvert.DeserializeObject <T>(_listjson[index]));
            }
            else if (_serializetype == SerializeType.protobuf)
            {
                byte[] bytes = System.Text.Encoding.UTF8.GetBytes(_listjson[index]);
                using (MemoryStream ms = new MemoryStream(bytes))
                {
                    if (typeof(T).Name == "DataTable")
                    {
                        Object obj = DataSerializer.DeserializeDataTable(ms);
                        return((T)obj);
                    }
                    else
                    {
                        return(Serializer.Deserialize <T>(ms));
                    }
                }
            }
            else if (_serializetype == SerializeType.fastJSON)
            {
                return(fastJSON.JSON.ToObject <T>(_listjson[index]));
            }
            else
            {
                return(default(T));
            }
        }
예제 #3
0
 public static DataTable SerializeAndDeserialize(DataTable dataTable, ProtoDataWriterOptions options)
 {
     using (var stream = new MemoryStream())
     {
         DataSerializer.Serialize(stream, dataTable, options);
         stream.Seek(0, SeekOrigin.Begin);
         return(DataSerializer.DeserializeDataTable(stream));
     }
 }
            public void ShouldDeserializeDataTable()
            {
                // Arrange
                var stream = new MemoryStream();

                DataSerializer.Serialize(stream, this.CreateTable(0));

                stream.Position = 0;

                // Act
                var dataTable = DataSerializer.DeserializeDataTable(stream);

                // Assert
                Assert.Single(dataTable.Columns);
            }
예제 #5
0
        /// <summary>
        /// Deserializes the specified value.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public T Deserialize <T>(RedisValue value) where T : class
        {
            using (var memoryStream = new MemoryStream(value))
            {
                var type = typeof(T);
                var isRedisCacheItemType = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IRedisCacheItem <>);
                var valueType            = isRedisCacheItemType ? type.GetGenericArguments().FirstOrDefault() : null;

                if (valueType != null && valueType == typeof(DataTable))
                {
                    return(new ProtobufRedisCacheItem <DataTable>
                    {
                        Value = DataSerializer.DeserializeDataTable(memoryStream)
                    } as T);
                }

                if (valueType != null && valueType == typeof(DataSet))
                {
                    return(new ProtobufRedisCacheItem <DataSet>
                    {
                        Value = DataSerializer.DeserializeDataSet(memoryStream)
                    } as T);
                }

                if (valueType != null && valueType == typeof(IDataReader))
                {
                    return(new ProtobufRedisCacheItem <IDataReader>
                    {
                        Value = DataSerializer.Deserialize(memoryStream)
                    } as T);
                }

                if (configuration.BuildSerializers != null)
                {
                    configuration.BuildSerializers.Build <T>();
                }

                if (isRedisCacheItemType)
                {
                    var originalType = type;
                    type = ProtobutRedisCacheItemType;
                    type = type.MakeGenericType(originalType.GetGenericArguments());
                }

                return(Serializer.NonGeneric.Deserialize(type, memoryStream) as T);
            }
        }
예제 #6
0
        public void SerializeDataTable()
        {
            var dataTable = new DataTable();

            dataTable.Columns.Add("A", typeof(int));
            dataTable.Columns.Add("B", typeof(string));
            dataTable.Rows.Add(1, "Hello");

            using (var stream = new MemoryStream())
            {
                DataSerializer.Serialize(stream, dataTable);
                stream.Seek(0, SeekOrigin.Begin);
                var result = DataSerializer.DeserializeDataTable(stream);

                Assert.AreEqual(dataTable.Rows[0]["A"], result.Rows[0]["A"]);
                Assert.AreEqual(dataTable.Rows[0]["B"], result.Rows[0]["B"]);
            }
        }
예제 #7
0
        public static BooleanValue <DataTable> DeSerializeDataTable(byte[] buffer)
        {
            using (MemoryStream ms = new MemoryStream(buffer))
            {
                DataTable dt = null;
                BooleanValue <DataTable> bv = new BooleanValue <DataTable>(dt);
                try
                {
                    dt       = DataSerializer.DeserializeDataTable(ms);
                    bv.Value = dt;
                }
                catch (Exception ex)
                {
                    bv.ErrorText = ex.Message;
                }

                return(bv);
            }
        }