跳至主要內容

序列化

naijoug大约 1 分钟

序列化,将数据接口或对象转化为可读取的格式(如:文件、缓存、网络数据流),待以后恢复读取使用。

reference

XML

  • XML 解析类型

    解析类型全称读写解析速度适合
    SAXSimple API for XML只能读不能写大型文本
    DOMDocument Object Model可读可写小型文本

JSON

YAML

Protobuf

  • protobufopen in new window

  • 高效的数据压缩编码方式 Protobufopen in new window

    syntax = "proto3";  // protocol buffers 版本
    option java_package = "io.grpc.hello"; // 生成的 Java 代码包名
    
    import "google/protobuf/empty.proto"; // 导入谷歌标准空消息
    package hello; // proto 文件包名
    
    // 定义一个服务
    service HelloService {
        // 1> 单项 RPC : 客户端发送一个请求,服务端返回一个应答 (类似函数调用)
        rpc SayHello(HelloRequest) returns (HelloResponse) {}
        // 2> 服务端流式 RPC : 客户端发送一个请求,获取一个数据流用来读取一系列的消息(直到没有更多消息为止)
        rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse) {}
        // 3> 客户端流式 RPC : 客户端提供一个数据流写入一系列消息,服务端读取完毕,返回一个应答
        rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse) {}
        // 4> 双向流式 RPC : 客户端和服务端通过一个读写数据流按任意顺序读写
        rpc BidiHello(stream HelloRequest) returns (stream HelloResponse) {}
    }
    // 空服务
    service EmptyService {
        rpc EmptyRequest(Empty) returns (HelloResponse) {}
        rpc EmptyResponse(HelloRequest) return (Empty) {}
        rpc EmptyAll(Empty) returns (Empty) {}
    }
    // 请求参数消息
    message HelloRequest {
        string name = 1;
    }
    // 返回数据消息
    message HelloResponse {
        string message = 1;
    }