io.bitsensor.lib.jackson.protobuf.ProtobufSerializer Maven / Gradle / Ivy
package io.bitsensor.lib.jackson.protobuf;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import io.bitsensor.proto.shaded.com.google.protobuf.MessageOrBuilder;
import io.bitsensor.proto.shaded.com.google.protobuf.util.JsonFormat.Printer;
import java.io.IOException;
public class ProtobufSerializer extends StdSerializer {
private final Printer printer;
public ProtobufSerializer(Printer printer) {
super(MessageOrBuilder.class);
this.printer = printer;
}
@Override
public void serialize(MessageOrBuilder message, JsonGenerator generator, SerializerProvider serializerProvider)
throws IOException {
String json = printer.print(message);
ObjectCodec codec = generator.getCodec();
JsonParser parser = codec.getFactory().createParser(json);
generator.writeTree(codec.readTree(parser));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy