diff --git a/content/programming-guides/editions.md b/content/programming-guides/editions.md index 1831a75c0..ff4e7d2c7 100644 --- a/content/programming-guides/editions.md +++ b/content/programming-guides/editions.md @@ -1356,7 +1356,7 @@ and unpack `Any` values in a typesafe manner – for example, in Java, the `Any` type will have special `pack()` and `unpack()` accessors, while in C++ there are `PackFrom()` and `UnpackTo()` methods: -```c++ +```cpp // Storing an arbitrary message type in Any. NetworkErrorDetails details = ...; ErrorStatus status; @@ -1425,7 +1425,7 @@ language in the relevant [API reference](/reference/). oneof. So if you set several oneof fields, only the *last* field you set will still have a value. - ```c++ + ```cpp SampleMessage message; message.set_name("name"); CHECK(message.has_name()); @@ -1452,7 +1452,7 @@ language in the relevant [API reference](/reference/). following sample code will crash because `sub_message` was already deleted by calling the `set_name()` method. - ```c++ + ```cpp SampleMessage message; SubMessage* sub_message = message.mutable_sub_message(); message.set_name("name"); // Will delete sub_message @@ -1463,7 +1463,7 @@ language in the relevant [API reference](/reference/). end up with the other's oneof case: in the example below, `msg1` will have a `sub_message` and `msg2` will have a `name`. - ```c++ + ```cpp SampleMessage msg1; msg1.set_name("name"); SampleMessage msg2; @@ -1644,223 +1644,11 @@ about, see the ## JSON Mapping {#json} -Protobuf 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. - -When parsing JSON-encoded data into a protocol buffer, if a value is missing or -if its value is `null`, it will be interpreted as the corresponding -[default value](#default). - -When generating JSON-encoded output from a protocol buffer, if a protobuf field -has the default value and if the field doesn't support field presence, it will -be omitted from the output by default. An implementation may provide options to -include fields with default values in the output. - -Singular fields that have a value set and that support field presence always -include the field value in the JSON-encoded output, even if it is the default -value. - -
| Editions | -JSON | -JSON example | -Notes | -
|---|---|---|---|
| message | -object | -{"fooBar": v, "g": null, ...} |
- Generates JSON objects. Message field names are mapped to
- lowerCamelCase and become JSON object keys. If the
- json_name 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 json_name option) and the
- original proto field name. null is an accepted value for
- all field types and treated as the default value of the corresponding
- field type. However, null cannot be used for the
- json_name value. For more on why, see
- Stricter validation for json_name.
- |
-
| enum | -string | -"FOO_BAR" |
- The name of the enum value as specified in proto is used. Parsers - accept both enum names and integer values. - | -
| map<K,V> | -object | -{"k": v, ...} |
- All keys are converted to strings. | -
| repeated V | -array | -[v, ...] |
- null is accepted as the empty list []. |
-
| bool | -true, false | -true, false |
- - |
| string | -string | -"Hello World!" |
- - |
| bytes | -base64 string | -"YWJjMTIzIT8kKiYoKSctPUB+" |
- 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, uint32 | -number | -1, -10, 0 |
- JSON value will be a decimal number. Either numbers or strings are - accepted. Empty strings are invalid. - | -
| int64, fixed64, uint64 | -string | -"1", "-10" |
- JSON value will be a decimal string. Either numbers or strings are - accepted. Empty strings are invalid. - | -
| float, double | -number | -1.1, -10.0, 0, "NaN", "Infinity" |
- JSON value will be a number or one of the special string values "NaN", - "Infinity", and "-Infinity". Either numbers or strings are accepted. - Empty strings are invalid. Exponent notation is also accepted. - | -
| Any | -object |
- {"@type": "url", "f": v, ... } |
- If the Any contains a value that has a special JSON
- mapping, it will be converted as follows: {"@type": xxx, "value":
- yyy}. Otherwise, the value will be converted into a JSON object,
- and the "@type" field will be inserted to indicate the
- actual data type.
- |
-
| Timestamp | -string | -"1972-01-01T10:00:20.021Z" |
- 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. - | -
| Duration | -string | -"1.000340012s", "1s" |
- 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 | -object |
- { ... } |
- Any JSON object. See struct.proto. |
-
| Wrapper types | -various types | -2, "2", "foo", true, "true", null, 0, ... |
- Wrappers use the same representation in JSON as the wrapped primitive
- type, except that null is allowed and preserved during data
- conversion and transfer.
- |
-
| FieldMask | -string | -"f.fooBar,h" |
- See field_mask.proto. |
-
| ListValue | -array | -[foo, bar, ...] |
- - |
| Value | -value | -- | Any JSON value. Check - google.protobuf.Value - for details. - | -
| NullValue | -null | -- | JSON null | -
| Empty | -object | -{} |
- An empty JSON object | -
| Protobuf | +JSON | +JSON example | +Notes | +
|---|---|---|---|
| message | +object | +{"fooBar": v, "g": null, ...} |
+ Generates JSON objects. Message field names are mapped to
+ lowerCamelCase and become JSON object keys. If the
+ json_name 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 json_name option) and the
+ original proto field name. null is an accepted value for
+ all field types and treated as the default value of the corresponding
+ field type. However, null cannot be used for the
+ json_name value. For more on why, see
+ Stricter validation for json_name.
+ |
+
| enum | +string | +"FOO_BAR" |
+ The name of the enum value as specified in proto is used. Parsers + accept both enum names and integer values. + | +
| map<K,V> | +object | +{"k": v, ...} |
+ All keys are converted to strings. | +
| repeated V | +array | +[v, ...] |
+ null is accepted as the empty list []. |
+
| bool | +true, false | +true, false |
+ + |
| string | +string | +"Hello World!" |
+ + |
| bytes | +base64 string | +"YWJjMTIzIT8kKiYoKSctPUB+" |
+ 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, uint32 | +number | +1, -10, 0 |
+ JSON value will be a decimal number. Either numbers or strings are + accepted. Empty strings are invalid. + | +
| int64, fixed64, uint64 | +string | +"1", "-10" |
+ JSON value will be a decimal string. Either numbers or strings are + accepted. Empty strings are invalid. + | +
| float, double | +number | +1.1, -10.0, 0, "NaN", "Infinity" |
+ JSON value will be a number or one of the special string values "NaN", + "Infinity", and "-Infinity". Either numbers or strings are accepted. + Empty strings are invalid. Exponent notation is also accepted. + | +
| Any | +object |
+ {"@type": "url", "f": v, ... } |
+ If the Any contains a value that has a special JSON
+ mapping, it will be converted as follows: {"@type": xxx, "value":
+ yyy}. Otherwise, the value will be converted into a JSON object,
+ and the "@type" field will be inserted to indicate the
+ actual data type.
+ |
+
| Timestamp | +string | +"1972-01-01T10:00:20.021Z" |
+ 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. + | +
| Duration | +string | +"1.000340012s", "1s" |
+ 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 | +object |
+ { ... } |
+ Any JSON object. See struct.proto. |
+
| Wrapper types | +various types | +2, "2", "foo", true, "true", null, 0, ... |
+ Wrappers use the same representation in JSON as the wrapped primitive
+ type, except that null is allowed and preserved during data
+ conversion and transfer.
+ |
+
| FieldMask | +string | +"f.fooBar,h" |
+ See field_mask.proto. |
+
| ListValue | +array | +[foo, bar, ...] |
+ + |
| Value | +value | ++ | Any JSON value. Check + google.protobuf.Value + for details. + | +
| NullValue | +null | ++ | JSON null | +
| Empty | +object | +{} |
+ An empty JSON object | +
| proto2 | -JSON | -JSON example | -Notes | -
|---|---|---|---|
| message | -object | -{"fooBar": v, "g": null, ...} |
- Generates JSON objects. Message field names are mapped to
- lowerCamelCase and become JSON object keys. If the
- json_name 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 json_name option) and the
- original proto field name. null is an accepted value for
- all field types and treated as the default value of the corresponding
- field type. However, null cannot be used for the
- json_name value. For more on why, see
- Stricter validation for json_name.
- |
-
| enum | -string | -"FOO_BAR" |
- The name of the enum value as specified in proto is used. Parsers - accept both enum names and integer values. - | -
| map<K,V> | -object | -{"k": v, ...} |
- All keys are converted to strings. | -
| repeated V | -array | -[v, ...] |
- null is accepted as the empty list []. |
-
| bool | -true, false | -true, false |
- - |
| string | -string | -"Hello World!" |
- - |
| bytes | -base64 string | -"YWJjMTIzIT8kKiYoKSctPUB+" |
- 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, uint32 | -number | -1, -10, 0 |
- JSON value will be a decimal number. Either numbers or strings are - accepted. - | -
| int64, fixed64, uint64 | -string | -"1", "-10" |
- JSON value will be a decimal string. Either numbers or strings are - accepted. - | -
| float, double | -number | -1.1, -10.0, 0, "NaN", "Infinity" |
- 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 | -object |
- {"@type": "url", "f": v, ... } |
- If the Any contains a value that has a special JSON
- mapping, it will be converted as follows: {"@type": xxx, "value":
- yyy}. Otherwise, the value will be converted into a JSON object,
- and the "@type" field will be inserted to indicate the
- actual data type.
- |
-
| Timestamp | -string | -"1972-01-01T10:00:20.021Z" |
- 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. - | -
| Duration | -string | -"1.000340012s", "1s" |
- 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 | -object |
- { ... } |
- Any JSON object. See struct.proto. |
-
| Wrapper types | -various types | -2, "2", "foo", true, "true", null, 0, ... |
- Wrappers use the same representation in JSON as the wrapped scalar
- type, except that null is allowed and preserved during data
- conversion and transfer.
- |
-
| FieldMask | -string | -"f.fooBar,h" |
- See field_mask.proto. |
-
| ListValue | -array | -[foo, bar, ...] |
- - |
| Value | -value | -- | Any JSON value. Check
- google.protobuf.Value
- for details.
- |
-
| NullValue | -null | -- | JSON null | -
| Empty | -object | -{} |
- An empty JSON object | -
| proto3 | -JSON | -JSON example | -Notes | -
|---|---|---|---|
| message | -object | -{"fooBar": v, "g": null, ...} |
- Generates JSON objects. Message field names are mapped to
- lowerCamelCase and become JSON object keys. If the
- json_name 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 json_name option) and the
- original proto field name. null is an accepted value for
- all field types and treated as the default value of the corresponding
- field type. However, null cannot be used for the
- json_name value. For more on why, see
- Stricter validation for json_name.
- |
-
| enum | -string | -"FOO_BAR" |
- The name of the enum value as specified in proto is used. Parsers - accept both enum names and integer values. - | -
| map<K,V> | -object | -{"k": v, ...} |
- All keys are converted to strings. | -
| repeated V | -array | -[v, ...] |
- null is accepted as the empty list []. |
-
| bool | -true, false | -true, false |
- - |
| string | -string | -"Hello World!" |
- - |
| bytes | -base64 string | -"YWJjMTIzIT8kKiYoKSctPUB+" |
- 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, uint32 | -number | -1, -10, 0 |
- JSON value will be a decimal number. Either numbers or strings are - accepted. Empty strings are invalid. - | -
| int64, fixed64, uint64 | -string | -"1", "-10" |
- JSON value will be a decimal string. Either numbers or strings are - accepted. Empty strings are invalid. - | -
| float, double | -number | -1.1, -10.0, 0, "NaN", "Infinity" |
- JSON value will be a number or one of the special string values "NaN", - "Infinity", and "-Infinity". Either numbers or strings are accepted. - Empty strings are invalid. Exponent notation is also accepted. - | -
| Any | -object |
- {"@type": "url", "f": v, ... } |
- If the Any contains a value that has a special JSON
- mapping, it will be converted as follows: {"@type": xxx, "value":
- yyy}. Otherwise, the value will be converted into a JSON object,
- and the "@type" field will be inserted to indicate the
- actual data type.
- |
-
| Timestamp | -string | -"1972-01-01T10:00:20.021Z" |
- 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. - | -
| Duration | -string | -"1.000340012s", "1s" |
- 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 | -object |
- { ... } |
- Any JSON object. See struct.proto. |
-
| Wrapper types | -various types | -2, "2", "foo", true, "true", null, 0, ... |
- Wrappers use the same representation in JSON as the wrapped primitive
- type, except that null is allowed and preserved during data
- conversion and transfer.
- |
-
| FieldMask | -string | -"f.fooBar,h" |
- See field_mask.proto. |
-
| ListValue | -array | -[foo, bar, ...] |
- - |
| Value | -value | -- | Any JSON value. Check - google.protobuf.Value - for details. - | -
| NullValue | -null | -- | JSON null | -
| Empty | -object | -{} |
- An empty JSON object | -