ProtoBuf的序列化和反序列化
来源:互联网 发布:mysql终端查看数据库 编辑:程序博客网 时间:2024/05/29 09:26
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace KT.Common.ToolHelp
{
public class ProtoBufHelp
{
/// <summary>
/// 序列化对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="model"></param>
/// <returns></returns>
public static byte[] Serialize<T>(T model)
{
try
{
//涉及格式转换,需要用到流,将二进制序列化到流中
using (MemoryStream ms = new MemoryStream())
{
//使用ProtoBuf工具的序列化方法
ProtoBuf.Serializer.Serialize<T>(ms, model);
//定义二级制数组,保存序列化后的结果
byte[] result = new byte[ms.Length];
//将流的位置设为0,起始点
ms.Position = 0;
//将流中的内容读取到二进制数组中
ms.Read(result, 0, result.Length);
return result;
}
}
catch (Exception ex)
{
//LogUtils.InfoLog("序列化失败: " + ex.ToString());
return null;
}
}
// 将收到的消息反序列化成对象
// < returns>The serialize.< /returns>
// < param name="msg">收到的消息.</param>
public static T DeSerialize<T>(byte[] msg)
{
T result = default(T);
try
{
using (MemoryStream ms = new MemoryStream())
{
//将消息写入流中
ms.Write(msg, 0, msg.Length);
//将流的位置归0
ms.Position = 0;
//使用工具反序列化对象
result = ProtoBuf.Serializer.Deserialize<T>(ms);
return result;
}
}
catch (Exception ex)
{
//LogUtils.InfoLog("反序列化失败: " + ex.ToString());
return result;
}
}
}
[ProtoContract]
public class ReportEmptyParkingDate
{
[ProtoMember(1)]
public string messageId { get; set; }
[ProtoMember(2)]
public string busCode { get; set; }
[ProtoMember(3)]
public string reqDate { get; set; }
[ProtoMember(4)]
public string ver { get; set; }
[ProtoMember(5)]
public string sign { get; set; }
[ProtoMember(6)]
public string parkCode { get; set; }
[ProtoMember(7)]
public int eptCarports { get; set; }
[ProtoMember(8)]
public int fixedRemain { get; set; }
[ProtoMember(9)]
public int leasedRemain { get; set; }
[ProtoMember(10)]
public int freeRemain { get; set; }
[ProtoMember(11)]
public int bookedRemain { get; set; }
}
- 【Protocol Buffer】Protobuf的序列化和反序列化
- ProtoBuf的序列化和反序列化
- google protobuf入门(序列化和反序列化)
- 使用protoBuf进行序列化和反序列化
- Protobuf 序列化和反序列化源码分析
- protobuf 序列化 反序列接口
- protobuf 的序列化与反序列化
- protobuf的序列化与反序列化
- protobuf Message的序列化和反序列化string类型
- unity3d中ProtoBuf的序列化和反序列化c#
- protobuf Message的序列化和反序列化string类型
- 【protobuf】 3.使用protobuf-csharp-port进行序列化和反序列化
- 序列和反序列化
- 序列和反序列化
- 【转】【UNITY3D 游戏开发之五】Google-protobuf与FlatBuffers数据的序列化和反序列化
- 【UNITY3D 游戏开发之五】Google-protobuf与FlatBuffers数据的序列化和反序列化
- java中使用protobuf序列化(反序列化)
- ProtoBuf 常用序列化/反序列化API
- php mysql emoji 的存储
- 进程间通信(四)---- 共享内存
- Selenium with Python 文档
- CodeForces-835E The penguin's game(思维+二进制乱搞+交互题)
- 记录自己经常用到的Linux命令
- ProtoBuf的序列化和反序列化
- 坑爹的mongodb异步驱动(mongodb-async-driver)
- 【文章】我是文章标题
- adb monkey
- C# --- 其他信息: @xxx不是过程xxx的参数。
- 写一个简单的自定义UITabelView
- SpringBoot集成分页插件PageHelper
- Hbase的JavaApi和工具类
- Hadoop2.7.4集群的Linux安装步骤