Created
December 24, 2020 06:22
-
-
Save dvsingh9/0c67ab28f2503f5d99d19a33b8d5368c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.protobuf.Descriptors; | |
import com.google.protobuf.GeneratedMessageV3; | |
import io.quarkus.jsonb.JsonbConfigCustomizer; | |
import javax.inject.Singleton; | |
import javax.json.bind.JsonbConfig; | |
import javax.json.bind.serializer.JsonbSerializer; | |
import javax.json.bind.serializer.SerializationContext; | |
import javax.json.stream.JsonGenerator; | |
import java.util.Map; | |
@Singleton | |
public class JsonbCustomizer implements JsonbConfigCustomizer { | |
public void customize(JsonbConfig config) { | |
config.withSerializers(new ProtobufMessageJsonbSerializer()); | |
} | |
public static class ProtobufMessageJsonbSerializer implements JsonbSerializer<GeneratedMessageV3> { | |
@Override | |
public void serialize(GeneratedMessageV3 message, JsonGenerator jsonGenerator, | |
SerializationContext serializationContext) { | |
jsonGenerator.writeStartObject(); | |
Map<Descriptors.FieldDescriptor, Object> fields = message.getAllFields(); | |
fields.forEach((desc, val) -> serializationContext.serialize(desc.getName(), val, jsonGenerator)); | |
jsonGenerator.writeEnd(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Protobuf Message Jsonb Serializer