-
Notifications
You must be signed in to change notification settings - Fork 914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce GsonGrpcJsonMarshaller and catch JsonMarshaller initialization failure #4033
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fa4b834
minimal implementation
jrhee17 858edbb
add more unit/it tests
jrhee17 965310e
add more unit tests
jrhee17 6e71b03
also handle proto2 included in proto3 file case
jrhee17 b708727
fix failing test
jrhee17 2c47eed
add whitespace option by default
jrhee17 58c0609
use protobuf-jackson by default, provide an upstream alternative
jrhee17 1bd8e83
fix javadocs, add warning log
jrhee17 9528f02
update the warning log message
jrhee17 e181b84
add documentation on performance
jrhee17 7be0eaa
let users know removing grpc serialization formats is also an option
jrhee17 a54508e
catch marshall exceptions on startup and warn
jrhee17 58acdbc
link benchmark, improve exception message, handle client side
jrhee17 c6b082d
catch exception for client side
jrhee17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about catching
Exception
likeFramedGrpcService
?