Skip to content

Commit

Permalink
use protobuf-jackson by default, provide an upstream alternative
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhee17 committed Jan 18, 2022
1 parent 2c47eed commit 58c0609
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 270 deletions.

This file was deleted.

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
Expand Down Expand Up @@ -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.
*/
static GrpcJsonMarshaller ofJackson(ServiceDescriptor serviceDescriptor) {
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}.
* This implementation internally uses {@code gson} to serialize and deserialize messages.
*/
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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
import org.curioswitch.common.protobuf.json.MessageMarshaller;
import org.curioswitch.common.protobuf.json.MessageMarshaller.Builder;

import com.google.common.collect.ImmutableList;
import com.google.protobuf.Message;

import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.internal.common.grpc.DefaultJsonMarshaller;
import com.linecorp.armeria.internal.common.grpc.GrpcJsonUtil;
import com.linecorp.armeria.internal.common.grpc.UpstreamJsonMarshaller;

import io.grpc.ServiceDescriptor;

Expand All @@ -41,8 +40,6 @@ public final class GrpcJsonMarshallerBuilder {
@Nullable
private Consumer<MessageMarshaller.Builder> jsonMarshallerCustomizer;

private GrpcJsonMarshallType grpcJsonMarshallType = GrpcJsonMarshallType.DEFAULT;

GrpcJsonMarshallerBuilder() {}

/**
Expand All @@ -64,27 +61,13 @@ public GrpcJsonMarshallerBuilder jsonMarshallerCustomizer(
return this;
}

/**
* Specifies the {@link GrpcJsonMarshallType} that should be created.
*/
public GrpcJsonMarshallerBuilder grpcJsonMarshallType(GrpcJsonMarshallType grpcJsonMarshallType) {
this.grpcJsonMarshallType = requireNonNull(grpcJsonMarshallType);
return this;
}

/**
* Returns a newly-created {@link GrpcJsonMarshaller} with the specified {@link ServiceDescriptor}.
*/
public GrpcJsonMarshaller build(ServiceDescriptor serviceDescriptor) {
requireNonNull(serviceDescriptor, "serviceDescriptor");
if (grpcJsonMarshallType == GrpcJsonMarshallType.DEFAULT) {
return new DefaultJsonMarshaller(serviceDescriptor, jsonMarshallerCustomizer);
} else if (grpcJsonMarshallType == GrpcJsonMarshallType.PROTOBUF_JACKSON) {
return GrpcJsonUtil.protobufJacksonJsonMarshaller(serviceDescriptor, jsonMarshallerCustomizer);
} else if (grpcJsonMarshallType == GrpcJsonMarshallType.UPSTREAM) {
return UpstreamJsonMarshaller.INSTANCE;
} else {
throw new Error();
}
return new DefaultJsonMarshaller(
GrpcJsonUtil.jsonMarshaller(ImmutableList.copyOf(serviceDescriptor.getMethods()),
jsonMarshallerCustomizer));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 LINE Corporation
* Copyright 2017 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
Expand All @@ -14,34 +14,31 @@
* under the License.
*/

package com.linecorp.armeria.internal.common.grpc;
package com.linecorp.armeria.common.grpc;

import java.util.List;
import java.util.function.Consumer;

import org.curioswitch.common.protobuf.json.MessageMarshaller;
import org.curioswitch.common.protobuf.json.MessageMarshaller.Builder;

import com.google.common.collect.ImmutableList;
import com.google.protobuf.Message;

import com.linecorp.armeria.common.annotation.Nullable;

import io.grpc.MethodDescriptor;
import io.grpc.MethodDescriptor.Marshaller;
import io.grpc.MethodDescriptor.PrototypeMarshaller;
import io.grpc.ServiceDescriptor;

/**
* Utilities for dealing with JSON marshalling in server/client.
*/
public final class GrpcJsonUtil {
final class GrpcJsonUtil {

/**
* Returns a {@link MessageMarshaller} with the request/response {@link Message}s of all the {@code methods}
* registered.
*/
private static MessageMarshaller jsonMarshaller(
public static MessageMarshaller jsonMarshaller(
List<MethodDescriptor<?, ?>> methods,
@Nullable Consumer<MessageMarshaller.Builder> jsonMarshallerCustomizer) {
final MessageMarshaller.Builder builder = MessageMarshaller.builder()
Expand Down Expand Up @@ -76,12 +73,5 @@ private static Message marshallerPrototype(Marshaller<?> marshaller) {
return null;
}

public static ProtobufJacksonJsonMarshaller protobufJacksonJsonMarshaller(
ServiceDescriptor serviceDescriptor, @Nullable Consumer<Builder> jsonMarshallerCustomizer) {
return new ProtobufJacksonJsonMarshaller(
jsonMarshaller(ImmutableList.copyOf(serviceDescriptor.getMethods()),
jsonMarshallerCustomizer));
}

private GrpcJsonUtil() {}
}
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.
*/
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.
*/
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
Expand Up @@ -19,62 +19,37 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.function.Consumer;

import org.curioswitch.common.protobuf.json.MessageMarshaller.Builder;
import org.curioswitch.common.protobuf.json.MessageMarshaller;

import com.google.common.annotations.VisibleForTesting;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Descriptors.FileDescriptor.Syntax;
import com.google.protobuf.Message;

import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.grpc.GrpcJsonMarshaller;

import io.grpc.MethodDescriptor.Marshaller;
import io.grpc.ServiceDescriptor;
import io.grpc.protobuf.ProtoFileDescriptorSupplier;
import io.grpc.MethodDescriptor.PrototypeMarshaller;

public final class DefaultJsonMarshaller implements GrpcJsonMarshaller {

/**
* Recursively goes through dependencies and look if any files are defined with proto2.
* It seems like protobuf doesn't allow circular imports for now.
*/
private static boolean hasProto2(FileDescriptor fileDescriptor) {
if (fileDescriptor.getSyntax() == Syntax.PROTO2) {
return true;
}
return fileDescriptor.getDependencies().stream().anyMatch(DefaultJsonMarshaller::hasProto2);
}

private final GrpcJsonMarshaller delegate;
private final MessageMarshaller delegate;

public DefaultJsonMarshaller(ServiceDescriptor serviceDescriptor,
@Nullable Consumer<Builder> jsonMarshallerCustomizer) {
final Object schemaDescriptor = serviceDescriptor.getSchemaDescriptor();
if (schemaDescriptor instanceof ProtoFileDescriptorSupplier) {
final FileDescriptor fileDescriptor =
((ProtoFileDescriptorSupplier) schemaDescriptor).getFileDescriptor();
if (hasProto2(fileDescriptor)) {
delegate = UpstreamJsonMarshaller.INSTANCE;
return;
}
}
delegate = GrpcJsonUtil.protobufJacksonJsonMarshaller(serviceDescriptor, jsonMarshallerCustomizer);
public DefaultJsonMarshaller(MessageMarshaller delegate) {
this.delegate = delegate;
}

@Override
public <T> void serializeMessage(Marshaller<T> marshaller, T message, OutputStream os) throws IOException {
delegate.serializeMessage(marshaller, message, os);
delegate.writeValue((Message) message, os);
}

@Override
public <T> T deserializeMessage(Marshaller<T> marshaller, InputStream is) throws IOException {
return delegate.deserializeMessage(marshaller, is);
}

@VisibleForTesting
GrpcJsonMarshaller delegate() {
return delegate;
final PrototypeMarshaller<T> prototypeMarshaller = (PrototypeMarshaller<T>) marshaller;
final Message prototype = (Message) prototypeMarshaller.getMessagePrototype();
final Message.Builder builder = prototype.newBuilderForType();
delegate.mergeValue(is, builder);
@SuppressWarnings("unchecked")
final T cast = (T) builder.build();
return cast;
}
}
Loading

0 comments on commit 58c0609

Please sign in to comment.