作者:8prye孙瑞D | 来源:互联网 | 2022-12-09 11:42
使用Jackson的ObjectMapper将protobuf转换为JSON时,出现以下错误:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Direct self-reference leading to cycle (through reference chain:
MyObjectPb$MyObject["unknownFields"]->
com.google.protobuf.UnknownFieldSet["defaultInstanceForType"])
MyObjectPb具有以下字段:
protected com.google.protobuf.UnknownFieldSet unknownFields
当我在现有代码库上工作时,我有以下限制:
我无法修改MyObjectPb的源代码,因此无法在MyObjectPb中使用Jackson的ignore注释。
我也不能使用Gson的库来转换对象,因为代码库已经使用Jackson进行序列化了。不建议添加新的依赖项。
如何告诉Jackson忽略(反序列化)MyObjectPb中的UnknownFieldSet对象?
我尝试了以下方法,但是这些方法似乎无法解决问题:
a)配置ObjectMapper:
myObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
myObjectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
b)使用杰克逊混音:
@JsonIgnoreType
private abstract class UnknownFieldSetIgnoreMixIn {}
myObjectMapper.addMixIn(UnknownFieldSet.class, UnknownFieldSetIgnoreMixIn.class)
amad-person..
5
我使用了JsonFormat类(com.googlecode.protobuf.format.JsonFormat)来转换protobuf:
new JsonFormat().printToString(myObject)
这对我来说做得很完美。
1> amad-person..:
我使用了JsonFormat类(com.googlecode.protobuf.format.JsonFormat)来转换protobuf:
new JsonFormat().printToString(myObject)
这对我来说做得很完美。
2> M H Chu..:
当前的序列化protobuf的方式(Oct-2018)使用com.google.protobuf.util.JsonFormat
以下方式:
JsonFormat.printer().print(myMessageOrBuilder)
我@JsonSerialize(using = MyMessageSerializer.class)
在protobuf对象之前使用了注释,并添加了此类:
public static class MyMessageSerializer extends JsonSerializer {
@Override
public void serialize(Message message, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString(JsonFormat.printer().print(message));
}
}
这允许 new ObjectMapper().writeValueAsString(wrapperObject)
将我的protobuf正确转换为JSON。
gen.writeRawValue,如果protobuf对象只是POJO类中的成员