我正在尝试将 json 转换为原始消息对象,但是如果 json 中有一些字符,例如 $ 或 . 或 _ 我得到一个 InvalidProtocolBufferException。我的原型:
syntax = "proto3";
package messages;
option java_multiple_files = true;
message Main {
A a = 1;
}
message A {
repeated B b = 1;
}
message B {
repeated C c = 1;
}
message C {
map<string, bytes> d = 1;
}
Java代码:
String json = "{\n" +
" \"a\" : {\n" +
" \"b\" : [ {\n" +
" \"c\" : [ {\n" +
" \"d\" : {\n" +
" \"money\" : [ \"100$\" ]\n" +
" }\n" +
" } ]\n" +
" } ]\n" +
" }\n" +
"}";
Main.Builder builder = Main.newBuilder();
JsonFormat.parser().merge(json, builder);
System.out.println(builder.build());
输出:
com.google.protobuf.InvalidProtocolBufferException: com.google.common.io.BaseEncoding$DecodingException: Unrecognized character: $
at com.google.protobuf.util.JsonFormat$ParserImpl.merge(JsonFormat.java:1065)
at com.google.protobuf.util.JsonFormat$Parser.merge(JsonFormat.java:273)