Reputation: 229
I am experimenting with protobuf serialization to JSON. I made a simple proto file with the following messages:
syntax = "proto3";
message Bool {
bool data = 1;
}
message BoolArray {
repeated Bool bools = 1;
}
I then run some basic code to build the message, push to Json, then read it back in:
pb_bool_array = pb_bool.BoolArray()
b = pb_bool_array.bools.add()
b.data = True
bools_as_json = MessageToJson( pb_bool_array )
Parse(bools_as_json, proto.bool_pb2.BoolArray )
but the Parse function throws a TypeError
with the following message:
google.protobuf.json_format.ParseError: Failed to parse bools field: unbound method ClearField() must be called with BoolArray instance as first argument (got str instance instead).
I traced the Parse function and this error fires off on line 519 in Google's json_format
code. Why would this TypeError occur? Am I missing something in my proto spec and/or abusing the python API?
Thanks!
Upvotes: 5
Views: 7420
Reputation: 229
After further analysis of the json_format.Parse()
function, I realized that I was abusing the API.
Parse(bools_as_json, proto.bool_pb2.BoolArray )
should really be:
Parse(bools_as_json, proto.bool_pb2.BoolArray() )
The API expects a message instance to fill, not the type of message. Everything works as expected.
Upvotes: 9