json2pb icon indicating copy to clipboard operation
json2pb copied to clipboard

Fixed json serialization for 'bytes' field - value base64 encoded with extra newline

Open mfan opened this issue 9 years ago • 0 comments

Notice that for json2pb.py, using test.proto, the value for '_bin' field is incorrectly serialized into '0a0a0a0a\n' after doing json->pb->json.

As shown in http://bugs.python.org/issue17714, python str.encode() with 'base64' as argument adds a trailing new line character to the returned string. This causes the bug in json2pb.py when handling bytes field.

  • Replaced str.encode('base64') with base64.b64encode(str).
  • Added a unit test for json2pb.py. Test case 'test_bytes_base64' shall fail without the fix.

mfan avatar Apr 21 '15 16:04 mfan