Skip to content

Commit c86a615

Browse files
authored
Merge pull request #191 from Logofile/sync
Documentation change
2 parents fad48a9 + 63e87b5 commit c86a615

File tree

12 files changed

+1218
-756
lines changed

12 files changed

+1218
-756
lines changed

content/programming-guides/editions.md

Lines changed: 9 additions & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,7 +1356,7 @@ and unpack `Any` values in a typesafe manner – for example, in Java, the `Any`
13561356
type will have special `pack()` and `unpack()` accessors, while in C++ there are
13571357
`PackFrom()` and `UnpackTo()` methods:
13581358

1359-
```c++
1359+
```cpp
13601360
// Storing an arbitrary message type in Any.
13611361
NetworkErrorDetails details = ...;
13621362
ErrorStatus status;
@@ -1425,7 +1425,7 @@ language in the relevant [API reference](/reference/).
14251425
oneof. So if you set several oneof fields, only the *last* field you set
14261426
will still have a value.
14271427

1428-
```c++
1428+
```cpp
14291429
SampleMessage message;
14301430
message.set_name("name");
14311431
CHECK(message.has_name());
@@ -1452,7 +1452,7 @@ language in the relevant [API reference](/reference/).
14521452
following sample code will crash because `sub_message` was already deleted
14531453
by calling the `set_name()` method.
14541454

1455-
```c++
1455+
```cpp
14561456
SampleMessage message;
14571457
SubMessage* sub_message = message.mutable_sub_message();
14581458
message.set_name("name"); // Will delete sub_message
@@ -1463,7 +1463,7 @@ language in the relevant [API reference](/reference/).
14631463
end up with the other's oneof case: in the example below, `msg1` will have a
14641464
`sub_message` and `msg2` will have a `name`.
14651465
1466-
```c++
1466+
```cpp
14671467
SampleMessage msg1;
14681468
msg1.set_name("name");
14691469
SampleMessage msg2;
@@ -1644,223 +1644,11 @@ about, see the
16441644

16451645
## JSON Mapping {#json}
16461646

1647-
Protobuf supports a canonical encoding in JSON, making it easier to share data
1648-
between systems. The encoding is described on a type-by-type basis in the table
1649-
below.
1650-
1651-
When parsing JSON-encoded data into a protocol buffer, if a value is missing or
1652-
if its value is `null`, it will be interpreted as the corresponding
1653-
[default value](#default).
1654-
1655-
When generating JSON-encoded output from a protocol buffer, if a protobuf field
1656-
has the default value and if the field doesn't support field presence, it will
1657-
be omitted from the output by default. An implementation may provide options to
1658-
include fields with default values in the output.
1659-
1660-
Singular fields that have a value set and that support field presence always
1661-
include the field value in the JSON-encoded output, even if it is the default
1662-
value.
1663-
1664-
<table>
1665-
<tbody>
1666-
<tr>
1667-
<th>Editions</th>
1668-
<th>JSON</th>
1669-
<th>JSON example</th>
1670-
<th>Notes</th>
1671-
</tr>
1672-
<tr>
1673-
<td>message</td>
1674-
<td>object</td>
1675-
<td><code>{"fooBar": v, "g": null, ...}</code></td>
1676-
<td>Generates JSON objects. Message field names are mapped to
1677-
lowerCamelCase and become JSON object keys. If the
1678-
<code>json_name</code> field option is specified, the specified value
1679-
will be used as the key instead. Parsers accept both the lowerCamelCase
1680-
name (or the one specified by the <code>json_name</code> option) and the
1681-
original proto field name. <code>null</code> is an accepted value for
1682-
all field types and treated as the default value of the corresponding
1683-
field type. However, <code>null</code> cannot be used for the
1684-
<code>json_name</code> value. For more on why, see
1685-
<a href="/news/2023-04-28#json-name">Stricter validation for json_name</a>.
1686-
</td>
1687-
</tr>
1688-
<tr>
1689-
<td>enum</td>
1690-
<td>string</td>
1691-
<td><code>"FOO_BAR"</code></td>
1692-
<td>The name of the enum value as specified in proto is used. Parsers
1693-
accept both enum names and integer values.
1694-
</td>
1695-
</tr>
1696-
<tr>
1697-
<td>map&lt;K,V&gt;</td>
1698-
<td>object</td>
1699-
<td><code>{"k": v, ...}</code></td>
1700-
<td>All keys are converted to strings.</td>
1701-
</tr>
1702-
<tr>
1703-
<td>repeated V</td>
1704-
<td>array</td>
1705-
<td><code>[v, ...]</code></td>
1706-
<td><code>null</code> is accepted as the empty list <code>[]</code>.</td>
1707-
</tr>
1708-
<tr>
1709-
<td>bool</td>
1710-
<td>true, false</td>
1711-
<td><code>true, false</code></td>
1712-
<td></td>
1713-
</tr>
1714-
<tr>
1715-
<td>string</td>
1716-
<td>string</td>
1717-
<td><code>"Hello World!"</code></td>
1718-
<td></td>
1719-
</tr>
1720-
<tr>
1721-
<td>bytes</td>
1722-
<td>base64 string</td>
1723-
<td><code>"YWJjMTIzIT8kKiYoKSctPUB+"</code></td>
1724-
<td>JSON value will be the data encoded as a string using standard base64
1725-
encoding with paddings. Either standard or URL-safe base64 encoding
1726-
with/without paddings are accepted.
1727-
</td>
1728-
</tr>
1729-
<tr>
1730-
<td>int32, fixed32, uint32</td>
1731-
<td>number</td>
1732-
<td><code>1, -10, 0</code></td>
1733-
<td>JSON value will be a decimal number. Either numbers or strings are
1734-
accepted. Empty strings are invalid.
1735-
</td>
1736-
</tr>
1737-
<tr>
1738-
<td>int64, fixed64, uint64</td>
1739-
<td>string</td>
1740-
<td><code>"1", "-10"</code></td>
1741-
<td>JSON value will be a decimal string. Either numbers or strings are
1742-
accepted. Empty strings are invalid.
1743-
</td>
1744-
</tr>
1745-
<tr>
1746-
<td>float, double</td>
1747-
<td>number</td>
1748-
<td><code>1.1, -10.0, 0, "NaN", "Infinity"</code></td>
1749-
<td>JSON value will be a number or one of the special string values "NaN",
1750-
"Infinity", and "-Infinity". Either numbers or strings are accepted.
1751-
Empty strings are invalid. Exponent notation is also accepted.
1752-
</td>
1753-
</tr>
1754-
<tr>
1755-
<td>Any</td>
1756-
<td><code>object</code></td>
1757-
<td><code>{"@type": "url", "f": v, ... }</code></td>
1758-
<td>If the <code>Any</code> contains a value that has a special JSON
1759-
mapping, it will be converted as follows: <code>{"@type": xxx, "value":
1760-
yyy}</code>. Otherwise, the value will be converted into a JSON object,
1761-
and the <code>"@type"</code> field will be inserted to indicate the
1762-
actual data type.
1763-
</td>
1764-
</tr>
1765-
<tr>
1766-
<td>Timestamp</td>
1767-
<td>string</td>
1768-
<td><code>"1972-01-01T10:00:20.021Z"</code></td>
1769-
<td>Uses RFC 3339, where generated output will always be Z-normalized
1770-
and uses 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are
1771-
also accepted.
1772-
</td>
1773-
</tr>
1774-
<tr>
1775-
<td>Duration</td>
1776-
<td>string</td>
1777-
<td><code>"1.000340012s", "1s"</code></td>
1778-
<td>Generated output always contains 0, 3, 6, or 9 fractional digits,
1779-
depending on required precision, followed by the suffix "s". Accepted
1780-
are any fractional digits (also none) as long as they fit into
1781-
nano-seconds precision and the suffix "s" is required.
1782-
</td>
1783-
</tr>
1784-
<tr>
1785-
<td>Struct</td>
1786-
<td><code>object</code></td>
1787-
<td><code>{ ... }</code></td>
1788-
<td>Any JSON object. See <code>struct.proto</code>.</td>
1789-
</tr>
1790-
<tr>
1791-
<td>Wrapper types</td>
1792-
<td>various types</td>
1793-
<td><code>2, "2", "foo", true, "true", null, 0, ...</code></td>
1794-
<td>Wrappers use the same representation in JSON as the wrapped primitive
1795-
type, except that <code>null</code> is allowed and preserved during data
1796-
conversion and transfer.
1797-
</td>
1798-
</tr>
1799-
<tr>
1800-
<td>FieldMask</td>
1801-
<td>string</td>
1802-
<td><code>"f.fooBar,h"</code></td>
1803-
<td>See <code>field_mask.proto</code>.</td>
1804-
</tr>
1805-
<tr>
1806-
<td>ListValue</td>
1807-
<td>array</td>
1808-
<td><code>[foo, bar, ...]</code></td>
1809-
<td></td>
1810-
</tr>
1811-
<tr>
1812-
<td>Value</td>
1813-
<td>value</td>
1814-
<td></td>
1815-
<td>Any JSON value. Check
1816-
<a href="/reference/protobuf/google.protobuf#value">google.protobuf.Value</a>
1817-
for details.
1818-
</td>
1819-
</tr>
1820-
<tr>
1821-
<td>NullValue</td>
1822-
<td>null</td>
1823-
<td></td>
1824-
<td>JSON null</td>
1825-
</tr>
1826-
<tr>
1827-
<td>Empty</td>
1828-
<td>object</td>
1829-
<td><code>{}</code></td>
1830-
<td>An empty JSON object</td>
1831-
</tr>
1832-
</tbody>
1833-
</table>
1834-
1835-
### JSON Options {#json-options}
1836-
1837-
A protobuf JSON implementation may provide the following options:
1838-
1839-
* **Always emit fields without presence**: Fields that don't support presence
1840-
and that have their default value are omitted by default in JSON output (for
1841-
example, an implicit presence integer with a 0 value, implicit presence
1842-
string fields that are empty strings, and empty repeated and map fields). An
1843-
implementation may provide an option to override this behavior and output
1844-
fields with their default values.
1845-
1846-
As of v25.x, the C++, Java, and Python implementations are nonconformant, as
1847-
this flag affects proto2 `optional` fields but not proto3 `optional` fields.
1848-
A fix is planned for a future release.
1849-
1850-
* **Ignore unknown fields**: The protobuf JSON parser should reject unknown
1851-
fields by default but may provide an option to ignore unknown fields in
1852-
parsing.
1853-
1854-
* **Use proto field name instead of lowerCamelCase name**: By default the
1855-
protobuf JSON printer should convert the field name to lowerCamelCase and
1856-
use that as the JSON name. An implementation may provide an option to use
1857-
proto field name as the JSON name instead. Protobuf JSON parsers are
1858-
required to accept both the converted lowerCamelCase name and the proto
1859-
field name.
1860-
1861-
* **Emit enum values as integers instead of strings**: The name of an enum
1862-
value is used by default in JSON output. An option may be provided to use
1863-
the numeric value of the enum value instead.
1647+
The standard protobuf binary wire format is the preferred serialization format
1648+
for communication between two systems that use protobufs. For communicating with
1649+
systems that use JSON rather than protobuf wire format, Protobuf supports a
1650+
canonical encoding in
1651+
[ProtoJSON](/programming-guides/json).
18641652

18651653
## Options {#options}
18661654

0 commit comments

Comments
 (0)