I get the following error while converting a protobuf to JSON using Jackson's ObjectMapper:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Direct self-reference leading to cycle (through reference chain:
MyObjectPb$MyObject["unknownFields"]->
com.google.protobuf.UnknownFieldSet["defaultInstanceForType"])
MyObjectPb has the following field:
protected com.google.protobuf.UnknownFieldSet unknownFields
As I am working on an existing codebase, I have the following constraints:
How do I tell Jackson to ignore (de)serializing the UnknownFieldSet object inside MyObjectPb?
I have tried the following, but these approaches do not seem to solve the problem:
a) Configuring the ObjectMapper:
myObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
myObjectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
b) Using a Jackson Mixin:
@JsonIgnoreType
private abstract class UnknownFieldSetIgnoreMixIn {}
myObjectMapper.addMixIn(UnknownFieldSet.class, UnknownFieldSetIgnoreMixIn.class)
Nested Class SummaryA Printer converts protobuf message to JSON format. A TypeRegistry is used to resolve Any messages in the JSON conversion.
JSON is limited to certain python objects, and it cannot serialize every python object. Protobuf supports a wider range of data types when compared to JSON. For example, enumerations and methods are supported by Protobuf and not supported by JSON. JSON supports only a subset of python data types.
Protocol buffers provide a language-neutral, platform-neutral, extensible mechanism for serializing structured data in a forward-compatible and backward-compatible way. It's like JSON, except it's smaller and faster, and it generates native language bindings.
The current way (Oct-2018) to serialize a protobuf is to use com.google.protobuf.util.JsonFormat
in the following manner:
JsonFormat.printer().print(myMessageOrBuilder)
I used the @JsonSerialize(using = MyMessageSerializer.class)
annotation right before my protobuf object and added this class:
public static class MyMessageSerializer extends JsonSerializer<Message> {
@Override
public void serialize(Message message, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeRawValue(JsonFormat.printer().print(message));
}
}
This allowed new ObjectMapper().writeValueAsString(wrapperObject)
to properly convert my protobuf to JSON.
I used the JsonFormat class (com.googlecode.protobuf.format.JsonFormat) to convert the protobuf:
new JsonFormat().printToString(myObject)
This did the job perfectly for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With