using CYQ.Data.Tool;
using System;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
namespace CYQ.Data.Cache
{
internal enum SerializedType : ushort
{
None = 255,
ByteArray = 0,
Object = 1,
String = 2,
Datetime = 3,
Bool = 4,
//SByte = 5, //Makes no sense.
Byte = 6,
Short = 7,
UShort = 8,
Int = 9,
UInt = 10,
Long = 11,
ULong = 12,
Float = 13,
Double = 14,
ObjectJson = 15,
CompressedByteArray = 50,
CompressedObject = 51,
CompressedString = 52,
CompressedObjectJson = 53,
}
internal class Serializer
{
///
/// 序列化
///
/// 值
/// 返回序列化类型
/// 指定超过长度时启用压缩功能
///
public static byte[] Serialize(object value, out SerializedType type, uint compressionThreshold)
{
byte[] bytes;
if (value is byte[])
{
bytes = (byte[])value;
type = SerializedType.ByteArray;
if (bytes.Length > compressionThreshold)
{
bytes = compress(bytes);
type = SerializedType.CompressedByteArray;
}
}
else if (value is string)
{
bytes = Encoding.UTF8.GetBytes((string)value);
type = SerializedType.String;
if (bytes.Length > compressionThreshold)
{
bytes = compress(bytes);
type = SerializedType.CompressedString;
}
}
else if (value is DateTime)
{
bytes = BitConverter.GetBytes(((DateTime)value).Ticks);
type = SerializedType.Datetime;
}
else if (value is bool)
{
bytes = new byte[] { (byte)((bool)value ? 1 : 0) };
type = SerializedType.Bool;
}
else if (value is byte)
{
bytes = new byte[] { (byte)value };
type = SerializedType.Byte;
}
else if (value is short)
{
bytes = BitConverter.GetBytes((short)value);
type = SerializedType.Short;
}
else if (value is ushort)
{
bytes = BitConverter.GetBytes((ushort)value);
type = SerializedType.UShort;
}
else if (value is int)
{
bytes = BitConverter.GetBytes((int)value);
type = SerializedType.Int;
}
else if (value is uint)
{
bytes = BitConverter.GetBytes((uint)value);
type = SerializedType.UInt;
}
else if (value is long)
{
bytes = BitConverter.GetBytes((long)value);
type = SerializedType.Long;
}
else if (value is ulong)
{
bytes = BitConverter.GetBytes((ulong)value);
type = SerializedType.ULong;
}
else if (value is float)
{
bytes = BitConverter.GetBytes((float)value);
type = SerializedType.Float;
}
else if (value is double)
{
bytes = BitConverter.GetBytes((double)value);
type = SerializedType.Double;
}
else
{
type = SerializedType.ObjectJson;
Type t = value.GetType();
switch (ReflectTool.GetSystemType(ref t))
{
case SysType.Base:
case SysType.Enum:
case SysType.Custom:
if (t.GetCustomAttributes(typeof(SerializableAttribute), false).Length > 0)
{
type = SerializedType.Object;
}
break;
}
if (type == SerializedType.Object)
{
//可序列化 List