C # protobuf-net serialization

Source: Internet
Author: User
Tags net serialization visual studio 2010
Source location: Protobuf-net

1. Install Nuget :

Tools--Expansion manager



After the installation is complete, restart Microsoft Visual Studio 2010 and you can see:

Small bet

The following two items are only visible if the solution has already opened the project:


2. Install Protobuf_net (Find protobuf-net in NuGet, install, select Project to get it done)



3, Encapsulation simple operation class (in the project to introduce using PROTOBUF; You can use it directly)

    <summary>//protobuf_net//</summary> public class Protobufserializer {//&lt  ;summary>///serialization///</summary>//<typeparam name= "T" ></typeparam>// <param name= "T" ></param>///<returns></returns> public static string serialize< T> (T-t) {using (MemoryStream ms = new MemoryStream ()) {Serializer.seriali                Ze<t> (MS, T); Return Encoding.Unicode.GetString (Ms.            ToArray ()); }}///<summary>//deserialization///</summary>//<typeparam name= "T" >&lt ;/typeparam>//<param name= "content" ></param>///<returns></returns> PU Blic 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 occurs when deserializing with UTF8:

------------------------------------------------------System.IO.EndOfStreamException: Attempt to read content beyond the end of the stream. In ProtoBuf.ProtoReader.Ensure (Int32 count, Boolean Strict) location C:\Dev\protobuf-net\protobuf-net\ProtoReader.cs: line number 257 in ProtoBuf.ProtoReader.ReadString () position C:\Dev\protobuf-net\protobuf-net\ProtoReader.cs: line number 494 in Proto_2 (Object, Protoreader) in ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Read (Object value, Protoreader source) Location C:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs: line number 57 in ProtoBuf.Meta.RuntimeTypeModel.Deserialize (Int32 key, Object value, Protoreader source) location C:\Dev\protobuf-net\ Protobuf-net\meta\runtimetypemodel.cs: Line number 775 in ProtoBuf.Meta.TypeModel.DeserializeCore (Protoreader reader, type type , Object value, Boolean noautocreate) position C:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs: line number 700 in ProtoBuf.Meta.Type Model.deserialize (Stream source, Object value, type type, Serializationcontext context) location c:\Dev\pRotobuf-net\protobuf-net\meta\typemodel.cs: Line number 589 in ProtoBuf.Meta.TypeModel.Deserialize (Stream source, Object value, Type type) Location C:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs: line number 566 in Protobuf.serializer.deserialize[t] (Stream SOURCE) Location C:\Dev\protobuf-net\protobuf-net\Serializer.cs: line number 77 in Serialize.protobufserializer.deserialize[t] ( String content) Location E:\WorkSpace\WorkSpaceTest\CompressTest\Serialize\ProtobufSerializer.cs: line number 40 in Serialize.Form1.button1_Click (Object sender, EventArgs e) Location E:\WorkSpace\WorkSpaceTest\CompressTest\Serialize\ Form1.cs: line number---------------------------determined---------------------------

The difference between Encoding.unicode and Encoding.UTF8 in C #

Small bet

This article refers to:

Serialization Titans Protobuf-net, getting started with hands-on transcripts
8 ways to improve the performance of ASP. NET Web API


Protobuf is more efficient than XML, binary serialization, and supports a large number of data
The size of the PROTOBUF serialization is 1/20 of the JSON 1/10,xml format, which is a binary serialized 1/10


The above is the C # protobuf-net serialized content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.