Source code location: protobuf-net
1. Install Nuget :
Tools--Expansion Manager
##After the installation is complete, restart Microsoft Visual Studio 2010, you can see the following picture:
Small note:
Only when the solution has opened the project, you will see the following two items:
2. Install protobuf_net (find protobuf-net in Nuget, install it, select the project to complete)
3. Encapsulate simple operation classes (introduce using ProtoBuf; into the project and you can use it directly)
<pre class="csharp"> /// <summary> /// Protobuf_net /// </summary> public class ProtobufSerializer { /// <summary> /// 序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> public static string Serialize<T>(T t) { using (MemoryStream ms = new MemoryStream()) { Serializer.Serialize<T>(ms, t); return Encoding.Unicode.GetString(ms.ToArray()); } } /// <summary> /// 反序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="content"></param> /// <returns></returns> public static T DeSerialize<T>(string content) { using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(content))) { T t = Serializer.Deserialize<T>(ms); return t; } } }
##The following error will occur when using UTF8 to deserialize:
##---------------------------
---------------------------
System.IO.EndOfStreamException: 尝试读取超出流末尾的内容。
在 ProtoBuf.ProtoReader.Ensure(Int32 count, Boolean strict) 位置 c:\Dev\protobuf-net\protobuf-net\ProtoReader.cs:行号 257
在 ProtoBuf.ProtoReader.ReadString() 位置 c:\Dev\protobuf-net\protobuf-net\ProtoReader.cs:行号 494
在 proto_2(Object , ProtoReader )
在 ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Read(Object value, ProtoReader source) 位置 c:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:行号 57
在 ProtoBuf.Meta.RuntimeTypeModel.Deserialize(Int32 key, Object value, ProtoReader source) 位置 c:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:行号 775
在 ProtoBuf.Meta.TypeModel.DeserializeCore(ProtoReader reader, Type type, Object value, Boolean noAutoCreate) 位置 c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:行号 700
在 ProtoBuf.Meta.TypeModel.Deserialize(Stream source, Object value, Type type, SerializationContext context) 位置 c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:行号 589
在 ProtoBuf.Meta.TypeModel.Deserialize(Stream source, Object value, Type type) 位置 c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:行号 566
在 ProtoBuf.Serializer.Deserialize[T](Stream source) 位置 c:\Dev\protobuf-net\protobuf-net\Serializer.cs:行号 77
在 Serialize.ProtobufSerializer.DeSerialize[T](String content) 位置 E:\WorkSpace\WorkSpaceTest\CompressTest\Serialize\ProtobufSerializer.cs:行号 40
在 Serialize.Form1.button1_Click(Object sender, EventArgs e) 位置 E:\WorkSpace\WorkSpaceTest\CompressTest\Serialize\Form1.cs:行号 44
---------------------------
确定
---------------------------
#Small note:
Reference for this article:
Serialization champion Protobuf-Net, hands-on entry record 8 ways to improve the performance of ASP.NET Web API
Compared with XML and binary serialization methods, Protobuf is more efficient and supports larger amounts of data The size of protobuf serialization is 1/10 of json, 1/20 of xml format, and 1/10 of binary serialization
The above is the content of C# Protobuf-Net serialization. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!