user1676605
user1676605

Reputation: 1447

Google Protocol Buffer error: "Encountered string containing invalid UTF-8 data while serializing protocol buffer"

I am using the following code

int lenSend = odl->ByteSize();
char* buf = (char *)malloc(lenSend);
odl->SerializeToArray(buf, lenSend);

I get this error and I can't understand why I get it (yes I get it three times):

libprotobuf ERROR google/protobuf/wire_format.cc:1059] Encountered string containing invalid UTF-8 data while serializing protocol buffer. Strings must contain only UTF-8; use the 'bytes' type for raw bytes.
libprotobuf ERROR google/protobuf/wire_format.cc:1059] Encountered string containing invalid UTF-8 data while serializing protocol buffer. Strings must contain only UTF-8; use the 'bytes' type for raw bytes.
libprotobuf ERROR google/protobuf/wire_format.cc:1059] Encountered string containing invalid UTF-8 data while serializing protocol buffer. Strings must contain only UTF-8; use the 'bytes' type for raw bytes.

Thanks.

Upvotes: 7

Views: 37363

Answers (5)

Sucas Venior
Sucas Venior

Reputation: 301

In golang, you can use strings.ToValidUTF8 to avoid temporary

Upvotes: 1

gdl_xxx
gdl_xxx

Reputation: 1

Convert []byte for translate

Example:

message Data {
    Object obj = 1;
}

If <grpc: failed to unmarshal the received message string field contains invalid UTF-8>:

# marshal object
message Data {
    bytes encodeObject = 1;
}
# unmarshal object

Upvotes: 0

Sheng Wu
Sheng Wu

Reputation: 33

Use byte[] to replace string, which encoding is not UTF-8 or ASCII.

Upvotes: 0

MYLOGOS
MYLOGOS

Reputation: 681

string A string must always contain UTF-8 encoded or 7-bit ASCII text. string String str/unicode[4]

bytes May contain any arbitrary sequence of bytes. string ByteString str

somtimes you should user bytes instead of string!

Upvotes: 0

Fraser
Fraser

Reputation: 78388

You can get rid of the warning by following the advice in the message!

You must have a field or fields in the definition of odl (in your .proto file) which are defined as string but into which you are putting non-UTF-8 characters. The docs state that you shouldn't do this. If you change these to bytes, the warnings should disappear.

Upvotes: 13

Related Questions