找回密码
 立即注册
查看: 221|回复: 0

Proto3: JSON Mapping - JSON映射

[复制链接]
发表于 2022-11-21 20:17 | 显示全部楼层 |阅读模式
Proto3 supports a canonical encoding in JSON, making it easier to share data between systems. The encoding is described on a type-by-type basis in the table below.
Proto3支持JSON中的规范编码,使得在系统间分享数据变得简单。下表中描述了基于逐个类型的编码。

If a value is missing in the JSON-encoded data or if its value is null, it will be interpreted as the appropriate default value when parsed into a protocol buffer. If a field has the default value in the protocol buffer, it will be omitted in the JSON-encoded data by default to save space. An implementation may provide options to emit fields with default values in the JSON-encoded output.
如果JSON编码数据中缺少一个值或者其值是null,在解析为protocol buffer时这会被解析为合适的默认值。protocol buffer中如果一个字段有默认值,为了节省空间JSON编码数据中会默认将其省略掉。实现可以提供选项以在 JSON 编码的输出中发出具有默认值的字段。
proto3JSONJSON exampleNotes
messageobject<code translate="no" dir="ltr">{"fooBar": v,<wbr> "g": null,<wbr> …}</code>Generates JSON objects. Message field names are mapped to lowerCamelCase and become JSON object keys. If the <code translate="no" dir="ltr">json_<wbr>name</code> field option is specified, the specified value will be used as the key instead. Parsers accept both the lowerCamelCase name (or the one specified by the <code translate="no" dir="ltr">json_<wbr>name</code> option) and the original proto field name. <code translate="no" dir="ltr">null</code> is an accepted value for all field types and treated as the default value of the corresponding field type.
enumstring<code translate="no" dir="ltr">"FOO_<wbr>BAR"</code>The name of the enum value as specified in proto is used. Parsers accept both enum names and integer values.
map<K,V>object<code translate="no" dir="ltr">{"k": v,<wbr> …}</code>All keys are converted to strings.
repeated Varray<code translate="no" dir="ltr">[v,<wbr> …]</code><code translate="no" dir="ltr">null</code> is accepted as the empty list <code translate="no" dir="ltr">[]</code>.
booltrue, false<code translate="no" dir="ltr">true,<wbr> false</code>
stringstring<code translate="no" dir="ltr">"Hello World!"</code>
bytesbase64 string<code translate="no" dir="ltr">"YWJjMTIzIT8kKiYoKSctPUB+"</code>JSON value will be the data encoded as a string using standard base64 encoding with paddings. Either standard or URL-safe base64 encoding with/without paddings are accepted.
int32, fixed32, uint32number<code translate="no" dir="ltr">1,<wbr> -10,<wbr> 0</code>JSON value will be a decimal number. Either numbers or strings are accepted.
int64, fixed64, uint64string<code translate="no" dir="ltr">"1",<wbr> "-10"</code>JSON value will be a decimal string. Either numbers or strings are accepted.
float, doublenumber<code translate="no" dir="ltr">1.<wbr>1,<wbr> -10.<wbr>0,<wbr> 0,<wbr> "NaN",<wbr> "Infinity"</code>JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity". Either numbers or strings are accepted. Exponent notation is also accepted.  -0 is considered equivalent to 0.
Any<code translate="no" dir="ltr">object</code><code translate="no" dir="ltr">{"@type": "url",<wbr> "f": v,<wbr> … }</code>If the <code translate="no" dir="ltr">Any</code> contains a value that has a special JSON mapping, it will be converted as follows: <code translate="no" dir="ltr">{"@type": xxx,<wbr> "value": yyy}</code>. Otherwise, the value will be converted into a JSON object, and the <code translate="no" dir="ltr">"@type"</code> field will be inserted to indicate the actual data type.
Timestampstring<code translate="no" dir="ltr">"1972-01-01T10:00:20.<wbr>021Z"</code>Uses RFC 3339, where generated output will always be Z-normalized and uses 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted.
Durationstring<code translate="no" dir="ltr">"1.<wbr>000340012s",<wbr> "1s"</code>Generated output always contains 0, 3, 6, or 9 fractional digits, depending on required precision, followed by the suffix "s". Accepted are any fractional digits (also none) as long as they fit into nano-seconds precision and the suffix "s" is required.
Struct<code translate="no" dir="ltr">object</code><code translate="no" dir="ltr">{ … }</code>Any JSON object. See <code translate="no" dir="ltr">struct.<wbr>proto</code>.
Wrapper typesvarious types<code translate="no" dir="ltr">2,<wbr> "2",<wbr> "foo",<wbr> true,<wbr> "true",<wbr> null,<wbr> 0,<wbr> …</code>Wrappers use the same representation in JSON as the wrapped primitive type, except that <code translate="no" dir="ltr">null</code> is allowed and preserved during data conversion and transfer.
FieldMaskstring<code translate="no" dir="ltr">"f.<wbr>fooBar,<wbr>h"</code>See <code translate="no" dir="ltr">field_<wbr>mask.<wbr>proto</code>.
ListValuearray<code translate="no" dir="ltr">[foo,<wbr> bar,<wbr> …]</code>
ValuevalueAny JSON value. Check <a href="/protocol-buffers/docs/reference/google.protobuf#google.protobuf.Value">google.protobuf.Value</a> for details.
NullValuenullJSON null
Emptyobject<code translate="no" dir="ltr">{}</code>An empty JSON object
JSON options - JSON选项


A proto3 JSON implementation may provide the following options:
一个proto3 JSON实现可以提供如下选项:


  • Emit fields with default values: Fields with default values are omitted by default in proto3 JSON output. An implementation may provide an option to override this behavior and output fields with their default values.
    发出有默认值的字段:在proto3  JSON输出中,默认值的字段会默认被省略掉。实现可以提供一个选项来覆盖这种表现并使用默认值来输出该字段。

  • Ignore unknown fields: Proto3 JSON parser should reject unknown fields by default but may provide an option to ignore unknown fields in parsing.
    忽略未知字段:Proto3 JSON解析器默认拒绝未知字段,但是可以提供一个选项在解析是来忽略未知字段。

  • Use proto field name instead of lowerCamelCase name: By default proto3 JSON printer should convert the field name to lowerCamelCase and use that as the JSON name. An implementation may provide an option to use proto field name as the JSON name instead. Proto3 JSON parsers are required to accept both the converted lowerCamelCase name and the proto field name.
    使用proto 字段命名而不是驼峰命令:proto3 JSON打印器应该默认将字段名称转换为驼峰命名来用作JSON命名。实现可以提供一个选项使用proto 字段作为JSON命名。要求Proto3 JSON解析器可接受驼峰和proto 字段两种命名。

  • Emit enum values as integers instead of strings: The name of an enum value is used by default in JSON output. An option may be provided to use the numeric value of the enum value instead.
    将枚举值作为整数而不是字符串发出:JSON输出中默认使用枚举值的名称。可以提供一个选项来代替使用枚举值的数值。
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-24 20:15 , Processed in 0.063916 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表