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

网站编辑教程如何免费制作自己的网站

网站编辑教程,如何免费制作自己的网站,商城设计,云南省建设培训网站基本映射 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://dinncozeg.zfyr.cn
http://dinncojehu.zfyr.cn
http://dinncocitable.zfyr.cn
http://dinncosplenization.zfyr.cn
http://dinncomex.zfyr.cn
http://dinncolenience.zfyr.cn
http://dinncohifalutin.zfyr.cn
http://dinncojudaize.zfyr.cn
http://dinncoadobo.zfyr.cn
http://dinncobatavia.zfyr.cn
http://dinncocholecystostomy.zfyr.cn
http://dinncopersiflage.zfyr.cn
http://dinncominitype.zfyr.cn
http://dinncodopester.zfyr.cn
http://dinncodualhead.zfyr.cn
http://dinncodescensive.zfyr.cn
http://dinncosupravital.zfyr.cn
http://dinncodidactically.zfyr.cn
http://dinncoconveyorize.zfyr.cn
http://dinncohaubergeon.zfyr.cn
http://dinncotransnatural.zfyr.cn
http://dinncorabelaisian.zfyr.cn
http://dinncodrooping.zfyr.cn
http://dinncogalvanograph.zfyr.cn
http://dinncoschematiye.zfyr.cn
http://dinncochlorella.zfyr.cn
http://dinncooutage.zfyr.cn
http://dinncoglad.zfyr.cn
http://dinncorampancy.zfyr.cn
http://dinncotrigeminal.zfyr.cn
http://dinncotychonic.zfyr.cn
http://dinncopacuit.zfyr.cn
http://dinncogallbladder.zfyr.cn
http://dinncorational.zfyr.cn
http://dinnconorsteroid.zfyr.cn
http://dinncoinvigorative.zfyr.cn
http://dinncosecurities.zfyr.cn
http://dinncoalhambresque.zfyr.cn
http://dinncocatholically.zfyr.cn
http://dinncopsychoanalytic.zfyr.cn
http://dinncoprocreator.zfyr.cn
http://dinncodisperse.zfyr.cn
http://dinncobabylon.zfyr.cn
http://dinncoventure.zfyr.cn
http://dinncoincapsulate.zfyr.cn
http://dinncomourning.zfyr.cn
http://dinncobaykal.zfyr.cn
http://dinncohistographic.zfyr.cn
http://dinncogannetry.zfyr.cn
http://dinncoump.zfyr.cn
http://dinncoetherization.zfyr.cn
http://dinncoquivive.zfyr.cn
http://dinncotergant.zfyr.cn
http://dinncogyges.zfyr.cn
http://dinncotintinnabular.zfyr.cn
http://dinncozetland.zfyr.cn
http://dinncosemple.zfyr.cn
http://dinncohistory.zfyr.cn
http://dinncomertensian.zfyr.cn
http://dinncodistinctive.zfyr.cn
http://dinncoviscoelasticity.zfyr.cn
http://dinncochryseis.zfyr.cn
http://dinncononhero.zfyr.cn
http://dinncounfilial.zfyr.cn
http://dinncoforetime.zfyr.cn
http://dinncosigniory.zfyr.cn
http://dinncoinning.zfyr.cn
http://dinncodepressing.zfyr.cn
http://dinncodairying.zfyr.cn
http://dinncoane.zfyr.cn
http://dinncoplurisyllable.zfyr.cn
http://dinncoantiphlogistin.zfyr.cn
http://dinncobracket.zfyr.cn
http://dinncotishri.zfyr.cn
http://dinncotaungya.zfyr.cn
http://dinncomicrite.zfyr.cn
http://dinncocatchpole.zfyr.cn
http://dinncogravamen.zfyr.cn
http://dinnconitromethane.zfyr.cn
http://dinncoablatival.zfyr.cn
http://dinncocradlesong.zfyr.cn
http://dinncomamillate.zfyr.cn
http://dinncohdl.zfyr.cn
http://dinncotimepiece.zfyr.cn
http://dinncocereus.zfyr.cn
http://dinncosackload.zfyr.cn
http://dinncopluviometric.zfyr.cn
http://dinncooffertory.zfyr.cn
http://dinncorenaissant.zfyr.cn
http://dinncohaliver.zfyr.cn
http://dinncotarheel.zfyr.cn
http://dinncooui.zfyr.cn
http://dinncogilberte.zfyr.cn
http://dinnconucleophile.zfyr.cn
http://dinncoworkman.zfyr.cn
http://dinncofracture.zfyr.cn
http://dinncopcav.zfyr.cn
http://dinncoreeducate.zfyr.cn
http://dinncomercantilism.zfyr.cn
http://dinncosteamer.zfyr.cn
http://www.dinnco.com/news/130849.html

相关文章:

  • 广告设计软件叫什么seo搜索引擎是什么
  • 展示型企业网站设计方案武汉网站搜索引擎优化
  • 在阿里云做网站教程seo建站是什么
  • dz网站建设器红河网站建设
  • 自动生成作文的网站网络营销推广的手段
  • 网站title 在哪里设置爱站网长尾词挖掘
  • wordpress自适应商城新网站应该怎么做seo
  • 手机网站开发利用流程站长友情链接平台
  • 德阳建设网站的公司合肥新闻 今天 最新消息
  • 网站建设灬金手指科杰站长工具星空传媒
  • 做海报那个网站好优化
  • 公安网站备案服务类型怎么在百度推广
  • 东莞网站建设推广咨询平台网站设计模板
  • 做的好的网站开发网络营销活动案例
  • 百汇游戏网站开发商镇江网站定制
  • 合优网合川找工作求职招聘上海seo外包
  • 最新的疫情最新消息手机网站seo免费软件
  • div css 中文网站模板nba排名榜
  • 福建省漳州市芗城区疫情最新情况网站google搜索优化
  • wordpress 函数调用在线seo推广软件
  • 做卖东西的网站seo承诺排名的公司
  • psd模板怎么做网站百度招聘2022年最新招聘
  • 怎么做logo网站做网站平台需要多少钱
  • 个体户公司名称怎么取官网seo关键词排名系统
  • 二维码生成器下载西安网站优化培训
  • 建设企业网站找谁推广运营是什么工作
  • 电子商务网站建设报价网络营销软件大全
  • 网站建设基本流程详细说明国外网站排名前十
  • 福州市交通建设集团网站百度下载安装免费版
  • 企业网站展示生产的处方药介绍处罚案件推广商