Protoc序列化例子

protoc

是一個靈活,高效,結構化折數據序列化框架,支持數據結構化一次,到處使用。

前置工作

安裝protoc ,mac比較方便,直接命令braw命令行
查看版本號
下載版本號對應的jar包

proto文件

syntax = "proto2";

package protoc;

option java_package = "com.protoc";
option java_outer_classname = "book";

message Person {
    required string name = 1;
    required int32 id = 2;
    optional string email = 3;

    enum PhoneType {
        MOBILE = 0;
        HOME = 1;
        WORK = 2;
    }

    message PhoneNumber {
        required string number = 1;
        optional PhoneType type = 2 [default = HOME];
    }

    repeated PhoneNumber phones = 4;
}

message AddressBook {
    repeated Person people = 1;

編譯生成java代碼

protoc這個命令

序列化對象

public class Write {

    static book.Person PromptForAddress() throws IOException {
        book.Person.Builder person = book.Person.newBuilder();
        person.setId(1);
        person.setName("world");
        person.setEmail("[email protected]");
        book.Person.PhoneNumber.Builder phoneNumber =
                book.Person.PhoneNumber.newBuilder().setNumber("123");
        phoneNumber.setType(book.Person.PhoneType.HOME);
        person.addPhones(phoneNumber);
        return person.build();
    }


    public static void main(String[] args) throws Exception {

        String path = "/xx/protocdemo/a.txt";

        book.AddressBook.Builder addressBook = book.AddressBook.newBuilder();

        try {
            addressBook.mergeFrom(new FileInputStream(path));
        } catch (FileNotFoundException e) {
            System.out.println(args[0] + ": File not found.  Creating a new file.");
        }

        addressBook.addPeople(PromptForAddress());
        FileOutputStream output = new FileOutputStream(path);
        addressBook.build().writeTo(output);
        output.close();
    }
}

反序列化對象

public class Read {


    static void Print(book.AddressBook addressBook) {
        for (book.Person person : addressBook.getPeopleList()) {
            System.out.println("Person ID: " + person.getId());
            System.out.println("  Name: " + person.getName());
            if (person.hasEmail()) {
                System.out.println("  E-mail address: " + person.getEmail());
            }

            for (book.Person.PhoneNumber phoneNumber : person.getPhonesList()) {
                switch (phoneNumber.getType()) {
                    case MOBILE:
                        System.out.print("  Mobile phone #: ");
                        break;
                    case HOME:
                        System.out.print("  Home phone #: ");
                        break;
                    case WORK:
                        System.out.print("  Work phone #: ");
                        break;
                }
                System.out.println(phoneNumber.getNumber());
            }
        }
    }


    public static void main(String[] args) throws Exception {

        String path = "/xx/protocdemo/a.txt";

        // Read the existing address book.
        book.AddressBook addressBook = book.AddressBook.parseFrom(new FileInputStream(path));

        Print(addressBook);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章