Skip to content

Commit 4ab768e

Browse files
authored
Encode compact JSON (#16)
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
1 parent c7f2d04 commit 4ab768e

6 files changed

Lines changed: 14 additions & 12 deletions

File tree

docs/enums.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ PhoneType.desc().open # True (proto3), False (proto2/closed)
2626
By default, enum values serialize to their string name in ProtoJSON:
2727

2828
```python
29-
msg.to_json() # {"phoneType": "PHONE_TYPE_MOBILE"}
29+
msg.to_json() # {"phoneType":"PHONE_TYPE_MOBILE"}
3030
```
3131

3232
To serialize as integers instead, pass `print_enums_as_ints=True`:
3333

3434
```python
35-
msg.to_json(print_enums_as_ints=True) # {"phoneType": 1}
35+
msg.to_json(print_enums_as_ints=True) # {"phoneType":1}
3636
```
3737

3838
See [Serialization](./serialization.md) for the full list of JSON options.

docs/faq.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Pass `always_emit_implicit=True` to include them:
6363
```python
6464
user = User(first_name="")
6565
user.to_json() # {}
66-
user.to_json(always_emit_implicit=True) # {"firstName": ""}
66+
user.to_json(always_emit_implicit=True) # {"firstName":""}
6767
```
6868

6969
For more information, see [JSON](./serialization.md#json).
@@ -76,7 +76,7 @@ The Protobuf JSON specification requires `lowerCamelCase` field names.
7676
Pass `use_proto_field_name=True` to use the original `snake_case` names instead:
7777

7878
```python
79-
user.to_json(use_proto_field_name=True) # {"first_name": "Homer"}
79+
user.to_json(use_proto_field_name=True) # {"first_name":"Homer"}
8080
```
8181

8282
Note: if you rename a field in the `.proto` file, JSON consumers will break because JSON uses names, not field numbers.

docs/getting-started/quickstart.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ Field names are converted to camelCase per the Protobuf JSON specification:
139139
```python
140140
# Serialize to a JSON string
141141
text: str = user.to_json()
142-
# {"firstName": "Homer", "lastName": "Simpson", ...}
142+
# {"firstName":"Homer","lastName":"Simpson",...}
143143
144144
# Deserialize from a JSON string
145145
user = User.from_json(text)

docs/serialization.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,23 @@ Set this to `True` to always include them:
5858
```python
5959
user = User() # first_name is "" (zero value)
6060
user.to_json() # {} (first_name omitted)
61-
user.to_json(always_emit_implicit=True) # {"firstName": ""}
61+
user.to_json(always_emit_implicit=True) # {"firstName":""}
6262
```
6363

6464
`print_enums_as_ints`: by default, enum values are serialized as string names.
6565
Set this to `True` to serialize them as integers instead:
6666

6767
```python
6868
msg.to_json(print_enums_as_ints=True)
69-
# {"status": 1} instead of {"status": "ACTIVE"}
69+
# {"status":1} instead of {"status":"ACTIVE"}
7070
```
7171

7272
`use_proto_field_name`: by default, field names are converted to `camelCase` in JSON output.
7373
Set this to `True` to use the original `snake_case` proto field names instead:
7474

7575
```python
7676
user.to_json(use_proto_field_name=True)
77-
# {"first_name": "Homer"} instead of {"firstName": "Homer"}
77+
# {"first_name":"Homer"} instead of {"firstName":"Homer"}
7878
```
7979

8080
`registry`: required when the message contains a [`google.protobuf.Any`](./well-known-types.md#any) field or extensions.

docs/tutorial.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ print(user2)
303303

304304
```shellsession
305305
$ uv run python main.py
306-
57
307-
{"firstName": "Yogi", "lastName": "Bear", "active": true}
306+
52
307+
{"firstName":"Yogi","lastName":"Bear","active":true}
308308
Yogi
309309
Bear
310310
True
@@ -466,7 +466,7 @@ print(user.to_json())
466466

467467
```shellsession
468468
$ uv run python main.py
469-
{"firstName": "Yogi", "lastName": "Bear", "displayName": "Yabba", "status": "STATUS_ACTIVE", "homeAddress": {"location": "123 Main St", "city": "Jellystone", "country": "USA"}, "workAddress": {"location": "456 Park Ave", "city": "Jellystone", "country": "USA"}, "manager": {"firstName": "Ranger", "lastName": "Smith"}, "hobbies": ["picnicking", "fishing"], "externalUsernames": {"twitter": "@yogibear", "github": "yogidev"}, "sessionTimeoutMinutes": 30, "created": "1958-09-29T18:00:00Z"}
469+
{"firstName":"Yogi","lastName":"Bear","displayName":"Yabba","status":"STATUS_ACTIVE","homeAddress":{"location":"123 Main St","city":"Jellystone","country":"USA"},"workAddress":{"location":"456 Park Ave","city":"Jellystone","country":"USA"},"manager":{"firstName":"Ranger","lastName":"Smith"},"hobbies":["picnicking","fishing"],"externalUsernames":{"twitter":"@yogibear","github":"yogidev"},"sessionTimeoutMinutes":30,"created":"1958-09-29T18:00:00Z"}
470470
```
471471

472472
We see typical Python usage for primitives, enums, submessages, lists, and dicts.

src/protobuf/_to_json.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ def _message_to_json_value(message: Message, opts: ToJsonOptions) -> JsonValue:
210210

211211

212212
def to_json(message: Message, opts: ToJsonOptions) -> str:
213-
return json.dumps(_message_to_json_value(message, opts))
213+
return json.dumps(
214+
_message_to_json_value(message, opts), separators=(",", ":"), ensure_ascii=False
215+
)
214216

215217

216218
def message_to_json_value(

0 commit comments

Comments
 (0)