Skip to content

Commit 69c8384

Browse files
committed
Unify ProtobufJsonReader and ProtobufJsonWriter implementations into single classes.
Motivation: - Simplify the codebase by eliminating separate `Impl` classes that provide no additional value. - Improve clarity by making `ProtobufJsonReader` and `ProtobufJsonWriter` self-contained. Changes: - Merged `ProtobufJsonReaderImpl` into `ProtobufJsonReader`. - Merged `ProtobufJsonWriterImpl` into `ProtobufJsonWriter`. - Updated constructors to handle `JsonWireFormat` directly within unified classes. - Removed redundant `Impl` classes and adjusted usage across the codebase.
1 parent 5889fa5 commit 69c8384

4 files changed

Lines changed: 62 additions & 86 deletions

File tree

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
package io.vertx.grpc.common.impl;
22

3+
import com.google.protobuf.InvalidProtocolBufferException;
34
import com.google.protobuf.Message;
5+
import com.google.protobuf.util.JsonFormat;
46
import io.vertx.core.buffer.Buffer;
57
import io.vertx.grpc.common.CodecException;
68
import io.vertx.grpc.common.JsonWireFormat;
79

10+
import java.nio.charset.StandardCharsets;
11+
812
/**
913
* Reads JSON {@link Buffer} payloads into a protobuf {@link Message.Builder}.
1014
* <p>
11-
* Use {@link #create(JsonWireFormat)} to get an instance. The default implementation is backed
12-
* by {@code com.google.protobuf.util.JsonFormat}.
15+
* Use {@link #create(JsonWireFormat)} to get an instance, backed by
16+
* {@code com.google.protobuf.util.JsonFormat}.
1317
*/
14-
public interface ProtobufJsonReader {
18+
public final class ProtobufJsonReader {
1519

1620
/**
1721
* @return a reader configured from {@code format}
1822
*/
19-
static ProtobufJsonReader create(JsonWireFormat format) {
20-
return new ProtobufJsonReaderImpl(format);
23+
public static ProtobufJsonReader create(JsonWireFormat format) {
24+
return new ProtobufJsonReader(format);
25+
}
26+
27+
private final JsonFormat.Parser parser;
28+
29+
private ProtobufJsonReader(JsonWireFormat format) {
30+
JsonFormat.Parser parser = JsonFormat.parser();
31+
if (format.ignoringUnknownFields()) {
32+
parser = parser.ignoringUnknownFields();
33+
}
34+
this.parser = parser;
2135
}
2236

2337
/**
@@ -27,5 +41,11 @@ static ProtobufJsonReader create(JsonWireFormat format) {
2741
* @param builder the target builder
2842
* @throws CodecException when the payload cannot be parsed
2943
*/
30-
void merge(Buffer payload, Message.Builder builder) throws CodecException;
44+
public void merge(Buffer payload, Message.Builder builder) throws CodecException {
45+
try {
46+
parser.merge(payload.toString(StandardCharsets.UTF_8), builder);
47+
} catch (InvalidProtocolBufferException e) {
48+
throw new CodecException(e);
49+
}
50+
}
3151
}

vertx-grpc-common/src/main/java/io/vertx/grpc/common/impl/ProtobufJsonReaderImpl.java

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,47 @@
11
package io.vertx.grpc.common.impl;
22

3+
import com.google.protobuf.InvalidProtocolBufferException;
34
import com.google.protobuf.MessageOrBuilder;
5+
import com.google.protobuf.util.JsonFormat;
46
import io.vertx.core.buffer.Buffer;
57
import io.vertx.grpc.common.CodecException;
68
import io.vertx.grpc.common.JsonWireFormat;
79

810
/**
911
* Writes a protobuf {@link MessageOrBuilder} as a JSON {@link Buffer}.
1012
* <p>
11-
* Use {@link #create(JsonWireFormat)} to get an instance. The default implementation is backed
12-
* by {@code com.google.protobuf.util.JsonFormat}.
13+
* Use {@link #create(JsonWireFormat)} to get an instance, backed by
14+
* {@code com.google.protobuf.util.JsonFormat}.
1315
*/
14-
public interface ProtobufJsonWriter {
16+
public final class ProtobufJsonWriter {
1517

1618
/**
1719
* @return a writer configured from {@code format}
1820
*/
19-
static ProtobufJsonWriter create(JsonWireFormat format) {
20-
return new ProtobufJsonWriterImpl(format);
21+
public static ProtobufJsonWriter create(JsonWireFormat format) {
22+
return new ProtobufJsonWriter(format);
23+
}
24+
25+
private final JsonFormat.Printer printer;
26+
27+
private ProtobufJsonWriter(JsonWireFormat format) {
28+
JsonFormat.Printer printer = JsonFormat.printer();
29+
if (format.alwaysPrintFieldsWithNoPresence()) {
30+
printer = printer.alwaysPrintFieldsWithNoPresence();
31+
}
32+
if (format.omittingInsignificantWhitespace()) {
33+
printer = printer.omittingInsignificantWhitespace();
34+
}
35+
if (format.preservingProtoFieldNames()) {
36+
printer = printer.preservingProtoFieldNames();
37+
}
38+
if (format.printingEnumsAsInts()) {
39+
printer = printer.printingEnumsAsInts();
40+
}
41+
if (format.sortingMapKeys()) {
42+
printer = printer.sortingMapKeys();
43+
}
44+
this.printer = printer;
2145
}
2246

2347
/**
@@ -27,5 +51,11 @@ static ProtobufJsonWriter create(JsonWireFormat format) {
2751
* @return the encoded JSON payload
2852
* @throws CodecException when the message cannot be encoded
2953
*/
30-
Buffer write(MessageOrBuilder message) throws CodecException;
54+
public Buffer write(MessageOrBuilder message) throws CodecException {
55+
try {
56+
return Buffer.buffer(printer.print(message));
57+
} catch (InvalidProtocolBufferException e) {
58+
throw new CodecException(e);
59+
}
60+
}
3161
}

vertx-grpc-common/src/main/java/io/vertx/grpc/common/impl/ProtobufJsonWriterImpl.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)