>  기사  >  백엔드 개발  >  C# Protobuf-Net 직렬화

C# Protobuf-Net 직렬화

黄舟
黄舟원래의
2017-02-13 11:49:072529검색

소스 코드 위치: protobuf-net

1. Nuget 설치:

도구--확장 관리자



설치가 완료된 후 Microsoft Visual Studio 2010을 다시 시작하면 다음 그림을 볼 수 있습니다.

참고 사항:

솔루션이 프로젝트를 열 때만 다음 두 항목이 표시됩니다.


2. protobuf_net 설치(Nuget에서 protobuf-net을 찾아 설치하고 완료할 프로젝트 선택)



3. 간단한 작업 클래스를 캡슐화합니다. (ProtoBuf를 프로젝트에 사용하도록 도입합니다. 직접)

<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;
            }
        }
    }

UTF8 역직렬화를 사용할 때 다음 오류가 발생합니다.

---------------------------

---------------------------
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
---------------------------
确定   
---------------------------

C#에서 Encoding.Unicode와 Encoding.UTF8의 차이점

작은 메모:

이 기사에 대한 참고 자료:

직렬화의 대가 Protobuf-Net, 실습 기록
ASP.NET 성능을 향상시키는 8가지 방법 Web API


Protobuf는 XML 및 바이너리 직렬화 방법에 비해 더 효율적이고 더 많은 양의 데이터를 지원합니다.
protobuf 직렬화 크기는 json의 1/10, xml 형식의 1/20, 바이너리 직렬화의 1/10


위 내용은 C# Protobuf-Net 직렬화 내용입니다. 더 많은 관련 내용은 PHP 중국어 홈페이지(www.php.cn)를 참고해주세요!



성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.