Package io.micronaut.protobuf.json
Class ProtobufJsonTranscoder
java.lang.Object
io.micronaut.protobuf.json.ProtobufJsonTranscoder
Utility class for handling Protobuf message serialization and deserialization to/from JSON.
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new instance ofProtobufJsonTranscoder
. -
Method Summary
-
Constructor Details
-
ProtobufJsonTranscoder
public ProtobufJsonTranscoder()Constructs a new instance ofProtobufJsonTranscoder
.
This constructor initializes the JSON printer and parser for handling serialization and deserialization of Protobuf messages to and from JSON. The JSON printer is configured to include default value fields in the resulting JSON and to sort map keys for deterministic output.
-
-
Method Details
-
toJson
Converts a Protobuf message to JSON string.- Parameters:
message
- The Protobuf message to convert- Returns:
- JSON string representation
- Throws:
ProtobufTranscodingException
- if serialization fails
-
fromJson
@NonNull public <T extends com.google.protobuf.Message> T fromJson(@NonNull @NonNull String jsonBody, @NonNull @NonNull Class<T> messageType) Creates a Protobuf message from JSON string.- Type Parameters:
T
- The type of Protobuf message- Parameters:
jsonBody
- The JSON string to parsemessageType
- The Protobuf message class- Returns:
- The constructed Protobuf message
- Throws:
ProtobufTranscodingException
- if deserialization fails
-