Java 格式化Protobuf消息,包括非ASCII字符串
当protobuf消息包含带有非ASCII字符的字符串时,Java 格式化Protobuf消息,包括非ASCII字符串,java,protocol-buffers,Java,Protocol Buffers,当protobuf消息包含带有非ASCII字符的字符串时,message.toString()返回类似 alarm { message: "\320\227\320\260\320\262\320\265\321\200\321\210\320\265\320\275\320\270\320\265 \321\201\320\262\321\217\320\267\320\270 \321\201 mzta1." } 我更喜欢在日志中看到包含西里尔字符的字符串。有简单的方法吗?我找到的唯一
message.toString()
返回类似
alarm {
message: "\320\227\320\260\320\262\320\265\321\200\321\210\320\265\320\275\320\270\320\265 \321\201\320\262\321\217\320\267\320\270 \321\201 mzta1."
}
我更喜欢在日志中看到包含西里尔字符的字符串。有简单的方法吗?我找到的唯一方法是复制com.google.protobuf.TextFormat的一部分并删除printFieldValue()方法中的字符串转义。类TextFormat::Printer中有方法SetUseUtf8StringEscaping:
// Set true to output UTF-8 instead of ASCII. The only difference
// is that bytes >= 0x80 in string fields will not be escaped,
// because they are assumed to be part of UTF-8 multi-byte
// sequences.
void SetUseUtf8StringEscaping(bool as_utf8)
从2.5.0开始,就有了。坦率地说,要获得protobuf的大部分好处,您最好使用二进制格式,而不是文本格式。如果您使用的是文本格式,那么您也可以使用json等。@marcGravel这只是用于日志记录,我当然在网上使用二进制格式:)
打印机
在Java库中是私有的。它不推荐使用,所以请使用TextFormat.Printer().escapingNonAscii(false)。printToString(messageHere);