Skip to content

Commit 86ffc43

Browse files
Protocol Buffer TeamLogofile
authored andcommitted
This documentation change fixes merge conflicts
Change-Id: I85acb6e88d5c425b694b0aaae5873f9bee6c6487
1 parent ca2d0fe commit 86ffc43

File tree

19 files changed

+500
-402
lines changed

19 files changed

+500
-402
lines changed

content/design-decisions/nullable-getters-setters.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ can't have defaults, there's no functional problem with this.
3030
As an example, consider this `.proto` file:
3131

3232
```proto
33-
message Msg { optional Child child = 1; }
34-
message Child { optional Grandchild grandchild = 1; }
35-
message Grandchild { optional int32 foo = 1 [default = 72]; }
33+
message Msg { Child child = 1; }
34+
message Child { Grandchild grandchild = 1; }
35+
message Grandchild { int32 foo = 1 [default = 72]; }
3636
```
3737

3838
and corresponding Kotlin getters:

content/editions/features.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,15 +1052,15 @@ proto2 | `LEGACY`
10521052

10531053
This feature determines how generated code should treat string fields. This
10541054
replaces the `ctype` option from proto2 and proto3, and offers a new
1055-
`string_view` feature. In Edition 2023, you can specify either `ctype` or
1055+
`string_type` feature. In Edition 2023, you can specify either `ctype` or
10561056
`string_type` on a field, but not both. In Edition 2024, the `ctype` option is
10571057
removed.
10581058

10591059
**Values available:**
10601060

1061-
* `VIEW`: Generates `string_view` accessors for the field. This will be the
1062-
default in a future edition.
1063-
* `CORD`: Generates `Cord` accessors for the field.
1061+
* `VIEW`: Generates `string_view` accessors for the field.
1062+
* `CORD`: Generates `Cord` accessors for the field. Not supported on extension
1063+
fields.
10641064
* `STRING`: Generates `string` accessors for the field.
10651065

10661066
**Applicable to the following scopes:** File, Field

content/editions/overview.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ the default behavior for the edition you've selected. You can also override your
2222
overrides. The [section later in this topic on lexical scoping](#scoping) goes
2323
into more detail on that.
2424

25-
*The latest released edition is 2024.*
25+
*The latest released edition is 2023.*
26+
27+
The examples in this topic show edition 2024 features, but edition 2024 is
28+
currently in **pre-release review** and is not yet recommended for production
29+
code.
2630

2731
## Lifecycle of a Feature {#lifecycles}
2832

@@ -176,7 +180,7 @@ package com.example;
176180
177181
message Player {
178182
// in proto3, optional fields have explicit presence
179-
optional string name = 1 [default = "N/A"];
183+
optional string name = 1;
180184
// in proto3 no specified field rule defaults to implicit presence
181185
int32 id = 2;
182186
// in proto3 this is packed by default

content/getting-started/pythontutorial.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,31 @@ Again, see the
293293
[`Message` API reference](https://googleapis.dev/python/protobuf/latest/google/protobuf/message.html#google.protobuf.message.Message)
294294
for a complete list.
295295
296+
You can also easily serialize messages to and from JSON. The `json_format`
297+
module provides helpers for this:
298+
299+
- `MessageToJson(message)`: serializes the message to a JSON string.
300+
- `Parse(json_string, message)`: parses a JSON string into the given message.
301+
302+
For example:
303+
304+
```python
305+
from google.protobuf import json_format
306+
import addressbook_pb2
307+
308+
person = addressbook_pb2.Person()
309+
person.id = 1234
310+
person.name = "John Doe"
311+
person.email = "[email protected]"
312+
313+
# Serialize to JSON
314+
json_string = json_format.MessageToJson(person)
315+
316+
# Parse from JSON
317+
new_person = addressbook_pb2.Person()
318+
json_format.Parse(json_string, new_person)
319+
```
320+
296321
{{% alert title="Important" color="warning" %}} **Protocol Buffers and Object Oriented Design**
297322
Protocol buffer classes are basically data holders (like structs in C) that
298323
don't provide additional functionality; they don't make good first class
@@ -464,12 +489,12 @@ One key feature provided by protocol message classes is *reflection*. You can
464489
iterate over the fields of a message and manipulate their values without writing
465490
your code against any specific message type. One very useful way to use
466491
reflection is for converting protocol messages to and from other encodings, such
467-
as XML or JSON. A more advanced use of reflection might be to find differences
468-
between two messages of the same type, or to develop a sort of "regular
469-
expressions for protocol messages" in which you can write expressions that match
470-
certain message contents. If you use your imagination, it's possible to apply
471-
Protocol Buffers to a much wider range of problems than you might initially
472-
expect!
492+
as XML or JSON (see [Parsing and Serialization](#parsing-serialization) for an
493+
example). A more advanced use of reflection might be to find differences between
494+
two messages of the same type, or to develop a sort of "regular expressions for
495+
protocol messages" in which you can write expressions that match certain message
496+
contents. If you use your imagination, it's possible to apply Protocol Buffers
497+
to a much wider range of problems than you might initially expect!
473498
474499
Reflection is provided as part of the
475500
[`Message` interface](https://googleapis.dev/python/protobuf/latest/google/protobuf/message.html#google.protobuf.message.Message).

content/news/2023-04-28.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ type = "docs"
88

99
## Stricter validation for `json_name` {#json-name}
1010

11-
v24 will forbid embedded null characters in the
11+
v24 will forbid zero unicode code points (`\u0000`) in the
1212
[`json_name` field option](/programming-guides/proto3/#json).
13-
Going forward, any valid Unicode characters will be accepted, **except**
14-
`\u0000`. Null will still be allowed in field values.
13+
Going forward, any valid Unicode characters will be accepted in `json_name`,
14+
**except** `\u0000`. `\0` characters will still be allowed to be used as values.
1515

16-
Previously, the proto compiler allowed null characters, but support for this was
17-
inconsistent across languages and implementations. To fix this, we are
18-
clarifying the spec to say that null is not allowed in `json_name`, and will be
19-
rejected by the compiler.
16+
Previously, the proto compiler allowed `\0` characters in the `json_name` field
17+
option, but support for this was inconsistent across languages and
18+
implementations. To help prevent interoperability problems relating to
19+
mishandling of keys containing a `\0` character, we are clarifying the spec to
20+
say that `\0` is not allowed in `json_name`, and will be rejected by the
21+
compiler.

content/programming-guides/editions.md

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,74 +1138,67 @@ There are two main reasons to use extensions:
11381138

11391139
### Example Extension {#ext-example}
11401140

1141-
Let's look at an example extension:
1141+
Using an extension is a two-step process. First, in the message you want to
1142+
extend (the "container"), you must reserve a range of field numbers for
1143+
extensions. Then, in a separate file, you define the extension field itself.
11421144

1143-
```proto
1144-
// file kittens/video_ext.proto
1145-
1146-
import "kittens/video.proto";
1147-
import "media/user_content.proto";
1148-
1149-
package kittens;
1145+
Here is an example that shows how to add an extension for kitten videos to a
1146+
generic `UserContent` message.
11501147

1151-
// This extension allows kitten videos in a media.UserContent message.
1152-
extend media.UserContent {
1153-
// Video is a message imported from kittens/video.proto
1154-
repeated Video kitten_videos = 126;
1155-
}
1156-
```
1148+
**Step 1: Reserve an extension range in the container message.**
11571149

1158-
Note that the file defining the extension (`kittens/video_ext.proto`) imports
1159-
the container message's file (`media/user_content.proto`).
1160-
1161-
The container message must reserve a subset of its field numbers for extensions.
1150+
The container message must use the `extensions` keyword to reserve a range of
1151+
field numbers for others to use. It is a best practice to also add a
1152+
`declaration` for the specific extension you plan to add. This declaration acts
1153+
as a forward-declaration, making it easier for developers to discover extensions
1154+
and avoid reusing field numbers.
11621155

11631156
```proto
1164-
// file media/user_content.proto
1157+
// media/user_content.proto
1158+
edition = "2023";
11651159
11661160
package media;
11671161
1168-
// A container message to hold stuff that a user has created.
1169-
message UserContent {
1170-
// Set verification to `DECLARATION` to enforce extension declarations for all
1171-
// extensions in this range.
1172-
extensions 100 to 199 [verification = DECLARATION];
1173-
}
1174-
```
1175-
1176-
The container message's file (`media/user_content.proto`) defines the message
1177-
`UserContent`, which reserves field numbers [100 to 199] for extensions. It is
1178-
recommended to set `verification = DECLARATION` for the range to require
1179-
declarations for all its extensions.
1180-
1181-
When the new extension (`kittens/video_ext.proto`) is added, a corresponding
1182-
declaration should be added to `UserContent` and `verification` should be
1183-
removed.
1184-
1185-
```
1186-
// A container message to hold stuff that a user has created.
1162+
// A container for user-created content.
11871163
message UserContent {
11881164
extensions 100 to 199 [
11891165
declaration = {
11901166
number: 126,
11911167
full_name: ".kittens.kitten_videos",
11921168
type: ".kittens.Video",
11931169
repeated: true
1194-
},
1195-
// Ensures all field numbers in this extension range are declarations.
1196-
verification = DECLARATION
1170+
}
11971171
];
11981172
}
11991173
```
12001174

1201-
`UserContent` declares that field number `126` will be used by a `repeated`
1202-
extension field with the fully-qualified name `.kittens.kitten_videos` and the
1203-
fully-qualified type `.kittens.Video`. To learn more about extension
1204-
declarations see
1205-
[Extension Declarations](/programming-guides/extension_declarations).
1175+
This declaration specifies the field number, full name, type, and cardinality of
1176+
the extension that will be defined elsewhere.
1177+
1178+
**Step 2: Define the extension in a separate file.**
1179+
1180+
The extension itself is defined in a different `.proto` file, which typically
1181+
focuses on a specific feature (like kitten videos). This avoids adding a
1182+
dependency from the generic container to the specific feature.
1183+
1184+
```proto
1185+
// kittens/video_ext.proto
1186+
edition = "2023";
1187+
1188+
import "media/user_content.proto"; // Imports the container message
1189+
import "kittens/video.proto"; // Imports the extension's message type
1190+
1191+
package kittens;
1192+
1193+
// This defines the extension field.
1194+
extend media.UserContent {
1195+
repeated Video kitten_videos = 126;
1196+
}
1197+
```
12061198

1207-
Note that the container message's file (`media/user_content.proto`) **does not**
1208-
import the kitten_video extension definition (`kittens/video_ext.proto`)
1199+
The `extend` block ties the new `kitten_videos` field back to the
1200+
`media.UserContent` message, using the field number `126` that was reserved in
1201+
the container.
12091202

12101203
There is no difference in the wire-format encoding of extension fields as
12111204
compared to a standard field with the same field number, type, and cardinality.

content/programming-guides/encoding.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,15 @@ For example, `-500z` is the same as the varint `999`.
222222

223223
### Non-varint Numbers {#non-varints}
224224

225-
Non-varint numeric types are simple -- `double` and `fixed64` have wire type
226-
`I64`, which tells the parser to expect a fixed eight-byte lump of data. We can
227-
specify a `double` record by writing `5: 25.4`, or a `fixed64` record with `6:
228-
200i64`. In both cases, omitting an explicit wire type implies the `I64` wire
229-
type.
225+
Non-varint numeric types are simple. `double` and `fixed64` have wire type
226+
`I64`, which tells the parser to expect a fixed eight-byte lump of data.
227+
`double` values are encoded in IEEE 754 double-precision format. We can specify
228+
a `double` record by writing `5: 25.4`, or a `fixed64` record with `6: 200i64`.
230229

231230
Similarly `float` and `fixed32` have wire type `I32`, which tells it to expect
232-
four bytes instead. The syntax for these consists of adding an `i32` suffix.
233-
`25.4i32` will emit four bytes, as will `200i32`. Tag types are inferred as
234-
`I32`.
231+
four bytes instead. `float` values are encoded in IEEE 754 single-precision
232+
format. The syntax for these consists of adding an `i32` suffix. `25.4i32` will
233+
emit four bytes, as will `200i32`. Tag types are inferred as `I32`.
235234

236235
## Length-Delimited Records {#length-types}
237236

@@ -529,11 +528,13 @@ value := varint for wire_type == VARINT,
529528
varint := int32 | int64 | uint32 | uint64 | bool | enum | sint32 | sint64;
530529
encoded as varints (sintN are ZigZag-encoded first)
531530
i32 := sfixed32 | fixed32 | float;
532-
encoded as 4-byte little-endian;
533-
memcpy of the equivalent C types (u?int32_t, float)
531+
encoded as 4-byte little-endian (float is IEEE 754
532+
single-precision); memcpy of the equivalent C types (u?int32_t,
533+
float)
534534
i64 := sfixed64 | fixed64 | double;
535-
encoded as 8-byte little-endian;
536-
memcpy of the equivalent C types (u?int64_t, double)
535+
encoded as 8-byte little-endian (double is IEEE 754
536+
double-precision); memcpy of the equivalent C types (u?int64_t,
537+
double)
537538
538539
len-prefix := size (message | string | bytes | packed);
539540
size encoded as int32 varint

0 commit comments

Comments
 (0)