Skip to content

Commit ed4b5c1

Browse files
Add section that better clarifies the Any JSON representation, especially in the case of google.protobuf.Empty
PiperOrigin-RevId: 853355631
1 parent 180ae8a commit ed4b5c1

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed

content/programming-guides/json.md

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,7 @@ The following table shows how data is represented in JSON files.
147147
<td>Any</td>
148148
<td><code>object</code></td>
149149
<td><code>{"@type": "url", "f": v, ... }</code></td>
150-
<td>If the <code>Any</code> contains a well-known type that has a special
151-
JSON mapping in this table (for example <code>google.protobuf.Duration</code>)
152-
it will be converted as follows: <code>{"@type": xxx, "value":
153-
yyy}</code>. Otherwise, the value will be converted into a JSON object
154-
as usual, and an additional <code>"@type"</code> field will be inserted
155-
with a value of the URL indicating the message's type.
150+
<td>See <a href="#any">Any</a>
156151
</td>
157152
</tr>
158153
<tr>
@@ -220,7 +215,7 @@ The following table shows how data is represented in JSON files.
220215
<tr>
221216
<td>Empty</td>
222217
<td>object</td>
223-
<td><code>{}</code></td>
218+
<td><code>{} (not special cased)</code></td>
224219
<td>An empty JSON object</td>
225220
</tr>
226221
</tbody>
@@ -283,6 +278,55 @@ Values with nonzero fractional portions are not allowed for integer-typed
283278
fields. Zero fractional portions are accepted. For example `1.0` is valid for an
284279
int32 field, but `1.5` is not.
285280

281+
## Any {#any}
282+
283+
### Normal messages {#any-normal-messages}
284+
285+
For any message that is not a well-known type with a special JSON
286+
representation, the message contained inside the `Any` is turned into a JSON
287+
object with an additional `"@type"` field inserted that contains the `type_url`
288+
that was set on the `Any`.
289+
290+
For example, if you have this message definition:
291+
292+
```proto
293+
package x;
294+
message Child { int32 x = 1; string y = 2; }
295+
```
296+
297+
When an instance of Child is packed into an `Any`, the JSON representation is:
298+
299+
```json
300+
{
301+
"@type": "type.googleapis.com/x.Child",
302+
"x": 1,
303+
"y": "hello world"
304+
}
305+
```
306+
307+
### Special-cased well-known types {#any-special-cases}
308+
309+
If the `Any` contains a well-known type that has a special JSON mapping, the
310+
message is converted into the special representation and set as a field with key
311+
"value".
312+
313+
For example, a `google.protobuf.Duration` that represents 3.1 seconds will be
314+
represented by the string `"3.1s"` in the special case handling. When that
315+
`Duration` is packed into an `Any` it will be serialized as:
316+
317+
```
318+
{
319+
"@type": "type.googleapis.com/google.protobuf.Duration",
320+
"value": "3.1s"
321+
}
322+
```
323+
324+
Note that `google.protobuf.Empty` is not considered to have any special JSON
325+
mapping; it is simply a normal message that has zero fields. This means the
326+
expected representation of an `Empty` packed into an `Any` is `{"@type":
327+
"type.googleapis.com/google.protobuf.Empty"}` and not `{"@type":
328+
"type.googleapis.com/google.protobuf.Empty", "value": {}}`.
329+
286330
## ProtoJSON Wire Safety {#json-wire-safety}
287331

288332
When using ProtoJSON, only some schema changes are safe to make in a distributed

0 commit comments

Comments
 (0)