首页  

jackson 直接把protobuf 数据对象 序列化成 json string     所属分类 jackson 浏览量 238
protobuf 数据对象 使用 jackson 序列化成 json string 时报错

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Direct self-reference leading to cycle (through reference chain: dyyx.PersonProto$Person["unknownFields"]->com.google.protobuf.UnknownFieldSet["defaultInstanceForType"])
	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
	at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1300)
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter._handleSelfReference(BeanPropertyWriter.java:944)
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:722)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:774)
	at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:774)
	at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
	at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1518)
	at com.fasterxml.jackson.databind.ObjectWriter._writeValueAndClose(ObjectWriter.java:1219)
	at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsString(ObjectWriter.java:1086)
	at dyyx.JacksonUtil.toString(JacksonUtil.java:62)
	at dyyx.JsonTest.main(JsonTest.java:30)
	
jackson-datatype-protobuf
Jackson module that adds support for serializing and deserializing Google's Protocol Buffers to and from JSON.

https://github.com/HubSpot/jackson-datatype-protobuf

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new ProtobufModule());


com.hubspot.jackson:jackson-datatype-protobuf:0.9.12

com.fasterxml.jackson.core:jackson-annotations:2.13.1
com.fasterxml.jackson.core:jackson-core:2.13.1
com.fasterxml.jackson.core:jackson-databind:2.13.1

注意 jackson 版本要配套


完整代码
https://gitee.com/dyyx/hellocode/blob/master/demo/protobufdemo/src/main/java/dyyx/JsonTest.java



quarkus 中需要 配置下 ,实现 ObjectMapperCustomizer import javax.inject.Singleton; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import com.hubspot.jackson.datatype.protobuf.ProtobufModule; import io.quarkus.jackson.ObjectMapperCustomizer; @Singleton public class TemporalObjectMapperCustomizer implements ObjectMapperCustomizer{ @Override public void customize(ObjectMapper mapper) { // 解决 protobuf 数据对象 json 序列化报错问题 mapper.registerModule(new ProtobufModule()); } } quarkus Jackon 配置
protobuf 对象转 json string protobuf 简介 及 java实例

上一篇     下一篇
graalvm hello 实例

jar 命令使用

protobuf 对象转 json string

quarkus Jackson 配置

quarkus 配置读取

Spring 和 quarkus 依赖注入相关注解对比