5

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!

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.