0
votes

I am working on a project which stores all data in key/value pairs. Both the key and value are strings. Changing this is beyond the current scope. Each data concept is represented by a protobuf message.

So, I must store a protobuf message as a string. Is it enough to store the message as message.toByteString().toString()? Or should I encode the byte[], with Base32 encoding for example?

edit

Using ByteString for persistency does not seem to work:

Message m = ...;
ByteString s = m.toByteString();
ByteString s_ = ByteString.copyFromUtf8(s.toStringUtf8());
Message.PARSER.parseFrom(s_);

While parsing a protocol message, the input ended unexpectedly in the middle of a field. This could mean either than the input has been truncated or that an embedded message misreported its own length.

So do you propose to use Base32 encoding on m.toByteArray()? Please note that changing the data type from string to byte[] currently is out of scope.

Thank you!

1
I added extra information. See my edit.John Deviao

1 Answers

1
votes

If you must store an encoded message as text, you should use base64 encoding. Base32 also works, but base64 is a bit more compact and better-standardized.

Using toStringUtf8() does not work because the encoded message is not UTF-8. This approach will therefore corrupt your data.