Home  >  Article  >  Backend Development  >  Detailed explanation of serialization and deserialization using Protobuffer in .NET

Detailed explanation of serialization and deserialization using Protobuffer in .NET

黄舟
黄舟Original
2017-03-03 13:26:192597browse

Use Protobuffer to implement serialization and deserialization in .NET

1. Go to the official website to download protobuf-net.dll, Official address: http://code.google.com/p/protobuf-net/

2. Build a console application

3. Add class library: protobuf-net.dll to the application.

Sample code:

Prepare an entity class to be tested (note that the class and method must have the protoBuffer serialization feature):

 [ProtoContract]
    public class Student
    {
        [ProtoMember(1)]
        public intStudentId { get; set; }
        [ProtoMember(2)]
        public stringName { get; set; }
        [ProtoMember(3)]
        public stringClassName { get; set; }
    }

Then test this class Serialization and Deserialization

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProtoBuf;
using ProtoBufferDemo.Entity;
using System.IO;
 
namespace ProtoBufferDemo
{
   class Program
   {
        private const string TestPath = @"D:/1.txt";
        static void Main(string[] args)
        {
            ////////////////////////序列化//////////////////////////////
 
            Student stu = new Student()
            {
                StudentId = 1,
                Name = "zhangsan",
                ClassName = "classOne"
            };
 
            if (!File.Exists(TestPath))
            {
               FileStream fs = File.Create(TestPath,1024, FileOptions.Asynchronous);
               fs.Dispose();
            }
 
            Console.WriteLine("开始序列化并导出到文件...");
            using (Stream s = new FileStream(TestPath,FileMode.Open ,FileAccess.ReadWrite))
            {
                Serializer.Serialize<Student>(s, stu);
                s.Close();
            }
            Console.WriteLine("序列化完毕");
 
            //////////////////////反序列化////////////////////////////
 
            Console.WriteLine("反序列化并输出...");
            using (Stream s = new FileStream(TestPath,FileMode.Open))
            {
                Student st = Serializer.Deserialize<Student>(s);
                Console.WriteLine("studentName:"+ stu.Name + "/r/n" +
                                  "studentId:"+ stu.StudentId + "/r/n" +
                                  "className:" + stu.ClassName);
                s.Close();
            }
 
            Console.Read();
        }
   }
}

Now consider the situation of multiple entities and test itSerializing a collection:

class Program
   {
        private const string TestPath = @"D:/1.txt";
        static void Main(string[] args)
        {
            ////////////////////////序列化//////////////////////////////
 
            List<Student> stu = new List<Student>()
            {
                new Student(){
                StudentId = 1,
                Name = "zhangsan",
                ClassName = "classOne"},
 
                new Student(){StudentId = 2,
                Name = "lisi",
                ClassName = "classTwo"}
            };
 
            if (!File.Exists(TestPath))
            {
                FileStream fs = File.Create(TestPath,1024, FileOptions.Asynchronous);
                fs.Dispose();
            }
 
            Console.WriteLine("开始序列化并导出文件...");
            using (Stream s = new FileStream(TestPath,FileMode.Open, FileAccess.ReadWrite))
            {
                Serializer.Serialize<List<Student>>(s,stu);
                s.Close();
            }
            Console.WriteLine("序列化完毕");
 
            //////////////////////反序列化////////////////////////////
 
            Console.WriteLine("反序列化并输出...");
            using (Stream s = new FileStream(TestPath,FileMode.Open))
            {
                List<Student> sl = Serializer.Deserialize<List<Student>>(s);
 
                foreach (var student in sl)
                {
                    Console.WriteLine("studentName:"+ student.Name + "/r/n" +
                                "studentId:" + student.StudentId + "/r/n" +
                                "class Name:" + student.ClassName);
                }
 
              
                s.Close();
            }
 
            Console.Read();
        }
   }

The above is the detailed explanation of serialization and deserialization using Protobuffer in .NET. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn