Skip to content

Commit 495ba7b

Browse files
anandoleecopybara-github
authored andcommitted
Nextgen Proto Pythonic API: “Add-on” proto for serialize/parse
- add google.protobuf.proto module - wrap generated SerializeToString and ParseFromString to the new module: def serialize(message: Message, deterministic: bool=None) -> bytes: """Return the serialized proto.""" def parse(message_class: typing.Type[Message], payload: bytes) -> Message: """Given a serialized proto, deserialize it into a Message.""" PiperOrigin-RevId: 632223409
1 parent 82e83dd commit 495ba7b

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# -*- coding: utf-8 -*-
2+
# Protocol Buffers - Google's data interchange format
3+
# Copyright 2008 Google Inc. All rights reserved.
4+
#
5+
# Use of this source code is governed by a BSD-style
6+
# license that can be found in the LICENSE file or at
7+
# https://developers.google.com/open-source/licenses/bsd
8+
9+
"""Tests Nextgen Pythonic protobuf APIs."""
10+
11+
import unittest
12+
13+
from google.protobuf import proto
14+
15+
from google.protobuf.internal import test_util
16+
from google.protobuf.internal import testing_refleaks
17+
from google.protobuf.internal import _parameterized
18+
from google.protobuf import unittest_pb2
19+
from google.protobuf import unittest_proto3_arena_pb2
20+
21+
@_parameterized.named_parameters(('_proto2', unittest_pb2),
22+
('_proto3', unittest_proto3_arena_pb2))
23+
@testing_refleaks.TestCase
24+
class ProtoTest(unittest.TestCase):
25+
26+
def testSerializeParse(self, message_module):
27+
msg = message_module.TestAllTypes()
28+
test_util.SetAllFields(msg)
29+
serialized_data = proto.serialize(msg)
30+
parsed_msg = proto.parse(message_module.TestAllTypes, serialized_data)
31+
self.assertEqual(msg, parsed_msg)
32+
33+
if __name__ == '__main__':
34+
unittest.main()

python/google/protobuf/proto.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Protocol Buffers - Google's data interchange format
2+
# Copyright 2008 Google Inc. All rights reserved.
3+
#
4+
# Use of this source code is governed by a BSD-style
5+
# license that can be found in the LICENSE file or at
6+
# https://developers.google.com/open-source/licenses/bsd
7+
8+
"""Contains the Nextgen Pythonic protobuf APIs."""
9+
10+
import typing
11+
12+
from google.protobuf.message import Message
13+
14+
def serialize(message: Message, deterministic: bool=None) -> bytes:
15+
"""Return the serialized proto.
16+
17+
Args:
18+
message: The proto message to be serialized.
19+
deterministic: If true, requests deterministic serialization
20+
of the protobuf, with predictable ordering of map keys.
21+
22+
Returns:
23+
A binary bytes representation of the message.
24+
"""
25+
return message.SerializeToString(deterministic=deterministic)
26+
27+
def parse(message_class: typing.Type[Message], payload: bytes) -> Message:
28+
"""Given a serialized data in binary form, deserialize it into a Message.
29+
30+
Args:
31+
message_class: The message meta class.
32+
payload: A serialized bytes in binary form.
33+
34+
Returns:
35+
A new message deserialized from payload.
36+
"""
37+
new_message = message_class()
38+
new_message.ParseFromString(payload)
39+
return new_message

0 commit comments

Comments
 (0)