-
Notifications
You must be signed in to change notification settings - Fork 914
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce GsonGrpcJsonMarshaller and catch JsonMarshaller initializat…
…ion failure (#4033) Motivation: Currently, armeria server fails to start up while trying to create a `MessageMarshaller` for `proto2` files containing `Enum` definitions. It seems like [protobuf-jackson](curioswitch/protobuf-jackson#8) doesn't support `proto2`. However, it seems like upstream at least partially supports `proto2`. https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/util/JsonFormat > The JSON format follows Proto3 JSON specification and only proto3 features are supported. Proto2 only features (e.g., extensions and unknown fields) will be discarded in the conversion. That is, when converting proto2 messages to JSON format, extensions and unknown fields will be treated as if they do not exist. This applies to proto2 messages embedded in proto3 messages as well. Modifications: - Introduce a `GsonGrpcJsonMarshaller` which performs JSON marshalling using Gson. - Also introduce a corresponding builder class `GrpcJsonMarshallerBuilder` - Add `GrpcJsonMarshaller#ofJackson`, `GrpcJsonMarshaller#builderForJackson`, `GrpcJsonMarshaller#ofGson`, `GrpcJsonMarshaller#builderForGson` Result: - Closes #4020 - Armeria now provides an easy way to fall back to the upstream `gson` implementation
- Loading branch information
Showing
10 changed files
with
458 additions
and
58 deletions.
There are no files selected for viewing
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
58 changes: 58 additions & 0 deletions
58
grpc/src/main/java/com/linecorp/armeria/common/grpc/GsonGrpcJsonMarshaller.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2022 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.common.grpc; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStream; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import com.google.protobuf.Message; | ||
import com.google.protobuf.MessageOrBuilder; | ||
import com.google.protobuf.util.JsonFormat; | ||
|
||
import io.grpc.MethodDescriptor.Marshaller; | ||
import io.grpc.MethodDescriptor.PrototypeMarshaller; | ||
|
||
final class GsonGrpcJsonMarshaller implements GrpcJsonMarshaller { | ||
|
||
final JsonFormat.Printer printer; | ||
final JsonFormat.Parser parser; | ||
|
||
GsonGrpcJsonMarshaller(JsonFormat.Printer printer, JsonFormat.Parser parser) { | ||
this.printer = printer; | ||
this.parser = parser; | ||
} | ||
|
||
@Override | ||
public <T> void serializeMessage(Marshaller<T> marshaller, T message, OutputStream os) throws IOException { | ||
os.write(printer.print((MessageOrBuilder) message).getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
@Override | ||
public <T> T deserializeMessage(Marshaller<T> marshaller, InputStream is) throws IOException { | ||
final PrototypeMarshaller<T> prototypeMarshaller = (PrototypeMarshaller<T>) marshaller; | ||
final Message prototype = (Message) prototypeMarshaller.getMessagePrototype(); | ||
assert prototype != null; | ||
final Message.Builder builder = prototype.newBuilderForType(); | ||
parser.merge(new InputStreamReader(is, StandardCharsets.UTF_8), builder); | ||
@SuppressWarnings("unchecked") | ||
final T cast = (T) builder.build(); | ||
return cast; | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
grpc/src/main/java/com/linecorp/armeria/common/grpc/GsonGrpcJsonMarshallerBuilder.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2020 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.common.grpc; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import com.google.protobuf.Message; | ||
import com.google.protobuf.util.JsonFormat; | ||
|
||
import com.linecorp.armeria.common.annotation.Nullable; | ||
|
||
/** | ||
* A builder for creating a new {@link GrpcJsonMarshaller} that serializes and deserializes a {@link Message} | ||
* to and from JSON. | ||
*/ | ||
public final class GsonGrpcJsonMarshallerBuilder { | ||
|
||
@Nullable | ||
private Consumer<JsonFormat.Parser> jsonParserCustomizer; | ||
|
||
@Nullable | ||
private Consumer<JsonFormat.Printer> jsonPrinterCustomizer; | ||
|
||
GsonGrpcJsonMarshallerBuilder() {} | ||
|
||
/** | ||
* Adds a {@link Consumer} that can customize the {@link JsonFormat.Parser} | ||
* used when deserializing a JSON payload into a {@link Message}. | ||
*/ | ||
public GsonGrpcJsonMarshallerBuilder jsonParserCustomizer( | ||
Consumer<? super JsonFormat.Parser> jsonParserCustomizer) { | ||
requireNonNull(jsonParserCustomizer, "jsonParserCustomizer"); | ||
if (this.jsonParserCustomizer == null) { | ||
@SuppressWarnings("unchecked") | ||
final Consumer<JsonFormat.Parser> cast = (Consumer<JsonFormat.Parser>) jsonParserCustomizer; | ||
this.jsonParserCustomizer = cast; | ||
} else { | ||
this.jsonParserCustomizer = this.jsonParserCustomizer.andThen(jsonParserCustomizer); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Adds a {@link Consumer} that can customize the {@link JsonFormat.Printer} | ||
* used when serializing a {@link Message} into a JSON payload. | ||
*/ | ||
public GsonGrpcJsonMarshallerBuilder jsonPrinterCustomizer( | ||
Consumer<? super JsonFormat.Printer> jsonPrinterCustomizer) { | ||
requireNonNull(jsonPrinterCustomizer, "jsonPrinterCustomizer"); | ||
if (this.jsonPrinterCustomizer == null) { | ||
@SuppressWarnings("unchecked") | ||
final Consumer<JsonFormat.Printer> cast = (Consumer<JsonFormat.Printer>) jsonPrinterCustomizer; | ||
this.jsonPrinterCustomizer = cast; | ||
} else { | ||
this.jsonPrinterCustomizer = this.jsonPrinterCustomizer.andThen(jsonPrinterCustomizer); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Returns a newly-created {@link GrpcJsonMarshaller}. | ||
*/ | ||
public GrpcJsonMarshaller build() { | ||
final JsonFormat.Printer printer = JsonFormat.printer().omittingInsignificantWhitespace(); | ||
if (jsonPrinterCustomizer != null) { | ||
jsonPrinterCustomizer.accept(printer); | ||
} | ||
|
||
final JsonFormat.Parser parser = JsonFormat.parser().ignoringUnknownFields(); | ||
if (jsonParserCustomizer != null) { | ||
jsonParserCustomizer.accept(parser); | ||
} | ||
return new GsonGrpcJsonMarshaller(printer, parser); | ||
} | ||
} |
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
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
Oops, something went wrong.