-
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
Changes from 7 commits
fa4b834
858edbb
965310e
6e71b03
b708727
2c47eed
58c0609
1bd8e83
9528f02
e181b84
7be0eaa
a54508e
58acdbc
c6b082d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2020 LINE Corporation | ||
* 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 | ||
|
@@ -39,19 +39,54 @@ public interface GrpcJsonMarshaller { | |
|
||
/** | ||
* Returns a newly-created {@link GrpcJsonMarshaller} which serializes and deserializes a {@link Message} | ||
* served by the {@linkplain ServiceDescriptor service}. | ||
* served by the {@linkplain ServiceDescriptor service}. This implementation internally uses | ||
* {@code protobuf-jackson} to serialize and deserialize messages. | ||
*/ | ||
static GrpcJsonMarshaller of(ServiceDescriptor serviceDescriptor) { | ||
return builder().build(serviceDescriptor); | ||
return ofJackson(serviceDescriptor); | ||
} | ||
|
||
/** | ||
* Returns a new {@link GrpcJsonMarshallerBuilder}. | ||
* Returns a new {@link GrpcJsonMarshallerBuilder}. This implementation internally uses | ||
* {@code protobuf-jackson} to serialize and deserialize messages. | ||
*/ | ||
static GrpcJsonMarshallerBuilder builder() { | ||
return builderForJackson(); | ||
} | ||
|
||
/** | ||
* Returns a newly-created {@link GrpcJsonMarshaller} which serializes and deserializes a {@link Message} | ||
* served by the {@linkplain ServiceDescriptor service}. This implementation internally uses | ||
* {@code protobuf-jackson} to serialize and deserialize messages. | ||
jrhee17 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
static GrpcJsonMarshaller ofJackson(ServiceDescriptor serviceDescriptor) { | ||
jrhee17 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return builderForJackson().build(serviceDescriptor); | ||
} | ||
|
||
/** | ||
* Returns a new {@link GrpcJsonMarshallerBuilder}. This implementation internally uses | ||
* {@code protobuf-jackson} to serialize and deserialize messages. | ||
*/ | ||
static GrpcJsonMarshallerBuilder builderForJackson() { | ||
return new GrpcJsonMarshallerBuilder(); | ||
} | ||
|
||
/** | ||
* Returns a newly-created {@link GrpcJsonMarshaller} which serializes and deserializes {@link Message}. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should add notes that the performance is much lower and should only be used if the default has problems, for example enum values in proto2 files. In practical terms, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Thanks for looking though this PR! Left an additional note in the javadocs that addresses this.
I still think this PR has meaning in that users who are committed to using Having said this, if we want to avoid breaking changes and avoid user-side extra configuration, I guess the other option we have is to just swallow the exception and warn when building a |
||
* This implementation internally uses {@code gson} to serialize and deserialize messages. | ||
jrhee17 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
static GrpcJsonMarshaller ofGson() { | ||
return builderForGson().build(); | ||
} | ||
|
||
/** | ||
* Returns a new {@link GrpcJsonMarshallerBuilder}. This implementation internally uses | ||
* {@code gson} to serialize and deserialize messages. | ||
*/ | ||
static GsonGrpcJsonMarshallerBuilder builderForGson() { | ||
return new GsonGrpcJsonMarshallerBuilder(); | ||
} | ||
|
||
/** | ||
* Serializes a gRPC message into JSON. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,92 @@ | ||||||||||
/* | ||||||||||
* 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; | ||||||||||
import com.linecorp.armeria.internal.common.grpc.GsonGrpcJsonMarshaller; | ||||||||||
|
||||||||||
/** | ||||||||||
* 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() {} | ||||||||||
|
||||||||||
/** | ||||||||||
* Sets a {@link Consumer} that can customize the {@link JsonFormat.Parser} for {@link Message} | ||||||||||
* used when handling JSON payloads in the service. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
*/ | ||||||||||
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; | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* Sets a {@link Consumer} that can customize the {@link JsonFormat.Printer} for {@link Message} | ||||||||||
* used when handling JSON payloads in the service. | ||||||||||
jrhee17 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
*/ | ||||||||||
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 GsonGrpcJsonMarshaller}. | ||||||||||
*/ | ||||||||||
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); | ||||||||||
} | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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.internal.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 com.linecorp.armeria.common.grpc.GrpcJsonMarshaller; | ||
|
||
import io.grpc.MethodDescriptor.Marshaller; | ||
import io.grpc.MethodDescriptor.PrototypeMarshaller; | ||
|
||
public final class GsonGrpcJsonMarshaller implements GrpcJsonMarshaller { | ||
jrhee17 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
final JsonFormat.Printer printer; | ||
final JsonFormat.Parser parser; | ||
|
||
public 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; | ||
} | ||
} |
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.
revert?