当前位置: 首页 > wzjs >正文

中文域名 网站兼职开发网站开发

中文域名 网站,兼职开发网站开发,企业推广软件有哪些,怎么做建设网站基本映射 1. 基本类型映射 Protobuf 类型JSON 类型示例(Protobuf → JSON)说明int32, sint32, uint32数字123直接映射为 JSON 数字,范围在 -2^31 到 2^32-1 之间。int64, sint64, uint64字符串"9223372036854775807"必须用字符串…

基本映射

1. 基本类型映射

Protobuf 类型JSON 类型示例(Protobuf → JSON)说明
int32, sint32, uint32数字123直接映射为 JSON 数字,范围在 -2^312^32-1 之间。
int64, sint64, uint64字符串"9223372036854775807"必须用字符串表示,避免 JavaScript 等语言中的精度丢失(安全范围为 ±2^53-1)。
float, double数字3.14浮点数直接映射为 JSON 数字。
booltrue/falsetrue布尔值直接映射。
string字符串"hello"UTF-8 编码的字符串。
bytes字符串(Base64)"SGVsbG8gd29ybGQ="二进制数据使用标准 Base64 编码。

2. 枚举类型(enum

枚举类型可以映射为两种形式:

2.1 数值形式(默认)
  • Protobufenum Status { ACTIVE = 0; INACTIVE = 1; }
  • JSON{ "status": 0 }{ "status": 1 }
2.2 字符串形式(需配置)

通过设置 json_format 选项,可将枚举值映射为字符串:

import "google/protobuf/descriptor.proto";extend google.protobuf.EnumValueOptions {string json_name = 50000;
}enum Status {ACTIVE = 0 [(json_name) = "active"];INACTIVE = 1 [(json_name) = "inactive"];
}
  • JSON{ "status": "active" }{ "status": "inactive" }

3. 消息类型(message

  • Protobuf
    message Person {string name = 1;int32 age = 2;
    }
    
  • JSON
    {"name": "Alice","age": 30
    }
    
  • 字段命名转换:Protobuf 的蛇形命名(如 user_name)会自动转换为 JSON 的驼峰命名(如 userName),可通过 json_name 选项自定义。

4. 重复字段(repeated

  • Protobuf
    message Person {repeated string hobbies = 1;
    }
    
  • JSON
    {"hobbies": ["reading", "coding"]
    }
    

5. 映射字段(map

  • Protobuf
    message Person {map<string, string> metadata = 1;
    }
    
  • JSON
    {"metadata": {"role": "admin","level": "expert"}
    }
    

6. 特殊类型(google.protobuf 包)

6.1 Timestamp
  • Protobuf
    import "google/protobuf/timestamp.proto";message Event {google.protobuf.Timestamp time = 1;
    }
    
  • JSON:ISO 8601 格式的字符串
    {"time": "2023-01-01T12:00:00Z"
    }
    
6.2 Duration
  • Protobuf
    import "google/protobuf/duration.proto";message Task {google.protobuf.Duration timeout = 1;
    }
    
  • JSON:带单位的字符串
    {"timeout": "3600s"  // 或 "1h"、"1.5h" 等
    }
    
6.3 Any

用于包装任意 Protobuf 消息:

  • Protobuf
    import "google/protobuf/any.proto";message Response {google.protobuf.Any data = 1;
    }
    
  • JSON
    {"data": {"@type": "type.googleapis.com/your.package.MessageType","field1": "value1","field2": "value2"}
    }
    
6.4 Struct

动态结构(类似 JSON 对象):

  • Protobuf
    import "google/protobuf/struct.proto";message Config {google.protobuf.Struct settings = 1;
    }
    
  • JSON:直接嵌套 JSON 对象
    {"settings": {"debug": true,"timeout": 3000}
    }
    

7. 空值处理

  • 默认值:未设置的字段在 JSON 中可以省略(如 int32 默认为 0string 默认为空字符串)。
  • 显式空值:通过 google.protobuf.NullValue 类型表示 null
    import "google/protobuf/wrappers.proto";message OptionalField {google.protobuf.StringValue name = 1;  // 允许 null
    }
    
    {"name": null
    }
    

特殊映射

1. 超出 JavaScript 安全范围的数值类型

Protobuf 类型JSON 类型原因
int64, sint64字符串范围为 -2^632^63-1,超过 JavaScript 的安全整数范围 ±2^53-1
uint64字符串范围为 02^64-1,同样超出 JavaScript 安全范围。
fixed64, sfixed64字符串64 位固定宽度整数,也会被转为字符串。

2. 二进制类型

Protobuf 类型JSON 类型原因
bytes字符串(Base64)JSON 无法直接表示二进制数据,需通过 Base64 编码转换为字符串。

3. Google Protobuf 标准类型

Protobuf 类型JSON 类型示例格式
google.protobuf.Timestamp字符串2023-01-01T12:00:00Z(ISO 8601 格式)
google.protobuf.Duration字符串3600s1.5h5m30s(带单位的字符串)
google.protobuf.Any对象{ "@type": "...", "field": "value" }(包含类型信息)
google.protobuf.Struct对象直接映射为 JSON 对象(用于动态结构)
google.protobuf.Value任意类型根据实际值类型(如 { "numberValue": 123 }
google.protobuf.ListValue数组JSON 数组(用于动态列表)

4. 其他特殊情况

4.1 枚举类型
  • 默认映射为 数值(如 { "status": 1 })。
  • 可通过 json_name 选项配置为 字符串(如 { "status": "ACTIVE" })。
4.2 空值(Null)
  • 使用 google.protobuf.NullValue 表示 null
    {"optionalField": null
    }
    
4.3 浮点特殊值
  • NaNInfinity-Infinity 在 JSON 中通常用字符串表示(如 "NaN"),但具体实现可能因语言而异。

为什么这些类型需要特殊处理?

  1. 跨语言兼容性:JavaScript、Python 等语言的数值类型范围与 Protobuf 不匹配,需要通过字符串避免精度丢失。
  2. 格式标准化:时间、持续时间等类型通过固定格式(如 ISO 8601)确保不同系统间正确解析。
  3. 类型信息保留Any 类型需要在 JSON 中保留原始消息的类型信息(通过 @type 字段)。

总结

gRPC 的 JSON 映射规则在保持 Protobuf 强类型特性的同时,通过特殊转换确保与 JSON 弱类型系统的兼容性。这些规则是跨平台 API 开发的基础,特别是在处理跨语言数据交换时尤为重要。


文章转载自:

http://QPV6wIYL.qzcLh.cn
http://200SImBj.qzcLh.cn
http://4L8RY2Hx.qzcLh.cn
http://FPutetCo.qzcLh.cn
http://S4KrQwPA.qzcLh.cn
http://tMj1G53K.qzcLh.cn
http://Q6RCHKYT.qzcLh.cn
http://KdbEINPX.qzcLh.cn
http://hLhWKjci.qzcLh.cn
http://8QaVebDC.qzcLh.cn
http://jN3Kq8lk.qzcLh.cn
http://Lo6cN0SF.qzcLh.cn
http://pexrinNA.qzcLh.cn
http://bc68gHJt.qzcLh.cn
http://86M2FvAi.qzcLh.cn
http://z0yGY6yi.qzcLh.cn
http://ypmM8xsq.qzcLh.cn
http://1GWdg5QB.qzcLh.cn
http://QdizFt8B.qzcLh.cn
http://zi2dpMx2.qzcLh.cn
http://aJT0NZHI.qzcLh.cn
http://a8LaXdtD.qzcLh.cn
http://cdkP4XUa.qzcLh.cn
http://7J2SfPx6.qzcLh.cn
http://i64xpW9D.qzcLh.cn
http://DQhgvd4M.qzcLh.cn
http://R2w4fyek.qzcLh.cn
http://D3etD6rF.qzcLh.cn
http://TJWOmSvO.qzcLh.cn
http://zzkURsvU.qzcLh.cn
http://www.dtcms.com/wzjs/780075.html

相关文章:

  • 宁波网站建设的价格表福州做企业网站的公司
  • 易语言编程可以做网站么延长中路上海网站建设
  • 广州建设监理协会网站做网站怎么找客户联系方式
  • 网站平台建设目标wordpress文章自定义栏目
  • 镇江公司网站建设站长推广网
  • 建设部网站安全考核证书查询创建一个自己的公司英语
  • 敦化网站建设手机上自己做网站吗
  • 小网站发布要怎么做网页设计简单教程
  • 重庆那里做网站外包好建设一个手机网站
  • 网站要实名认证吗教做面食的网站
  • 网站建设和维护公司筑云网站投诉
  • 租用域名与空间的网站并会使用深圳建设一个网站制作公司
  • 国内优秀设计网站推荐太原0元网站建设
  • 0基础网站建设模板网站建设属于哪个税目
  • ps怎么做网站特效集团网站建
  • 网站建设沙漠风中国联通业绩
  • 网页设计企业网站设计的功能沈阳免费网站制作
  • 建商城网站需要什么广州互联网项目工作室
  • 海南省建设工程质量安全检测协会网站最新合肥封闭小区名单
  • 企业网站建设相关书籍专业网站建设开发
  • 建站图标素材网页设计公司有哪些在包头的
  • 网站建设优化推广教程网页设计制作模板及代码
  • dedecms wap网站模板怎么建立图片的网站吗
  • wordpress网站前端想代理个网站建设平台
  • 商务网站开发源码建设厅教育培训网站
  • 做网站用哪个服务器宣威市网站建设
  • 点开图片跳到网站怎么做杭州正规制作网站公司吗
  • 网站建设服务费如何做会计分录贵州网站推广公司
  • 时间管理系统 网站开发wordpress+制作widget
  • 资源专业网站优化排名精美网站建设