Skip to content

Commit cd224ff

Browse files
authored
feat: add convenience methods to convert to/from json (#39)
1 parent 81e2971 commit cd224ff

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

packages/proto-plus/proto/message.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from google.protobuf import descriptor_pb2
2222
from google.protobuf import message
2323
from google.protobuf import symbol_database
24+
from google.protobuf.json_format import MessageToJson, Parse
2425

2526
from proto import _file_info
2627
from proto import _package_info
@@ -325,6 +326,31 @@ def deserialize(cls, payload: bytes) -> 'Message':
325326
"""
326327
return cls(cls.pb().FromString(payload))
327328

329+
def to_json(cls, instance) -> str:
330+
"""Given a message instance, serialize it to json
331+
332+
Args:
333+
instance: An instance of this message type, or something
334+
compatible (accepted by the type's constructor).
335+
336+
Returns:
337+
str: The json string representation of the protocol buffer.
338+
"""
339+
return MessageToJson(cls.pb(instance))
340+
341+
def from_json(cls, payload) -> 'Message':
342+
"""Given a json string representing an instance,
343+
parse it into a message.
344+
345+
Args:
346+
paylod: A json string representing a message.
347+
348+
Returns:
349+
~.Message: An instance of the message class against which this
350+
method was called.
351+
"""
352+
return cls(Parse(payload, cls()._pb))
353+
328354

329355
class Message(metaclass=MessageMeta):
330356
"""The abstract base class for a message.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright (C) 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
17+
import proto
18+
from google.protobuf.json_format import MessageToJson, Parse
19+
20+
21+
def test_message_to_json():
22+
class Squid(proto.Message):
23+
mass_kg = proto.Field(proto.INT32, number=1)
24+
25+
s = Squid(mass_kg=100)
26+
json = Squid.to_json(s)
27+
json = json.replace(" ", "").replace("\n", "")
28+
assert json == '{"massKg":100}'
29+
30+
31+
def test_message_from_json():
32+
class Squid(proto.Message):
33+
mass_kg = proto.Field(proto.INT32, number=1)
34+
35+
json = '''{
36+
"massKg": 100
37+
}
38+
'''
39+
40+
s = Squid.from_json(json)
41+
assert s == Squid(mass_kg=100)
42+
43+
44+
def test_message_json_round_trip():
45+
class Squid(proto.Message):
46+
mass_kg = proto.Field(proto.INT32, number=1)
47+
48+
s = Squid(mass_kg=100)
49+
json = Squid.to_json(s)
50+
s2 = Squid.from_json(json)
51+
52+
assert s == s2

0 commit comments

Comments
 (0)