From 540f70e870c7ea46a30ee189f9e982e7dc326f46 Mon Sep 17 00:00:00 2001 From: dave Date: Tue, 1 Nov 2016 21:54:10 -0400 Subject: [PATCH] ProtoUtil.jsonMarshaller can be supplied a JsonFormat Parser and Printer This allows for customizing the printer and parser behaviour --- .../main/java/io/grpc/protobuf/ProtoUtils.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/protobuf/src/main/java/io/grpc/protobuf/ProtoUtils.java b/protobuf/src/main/java/io/grpc/protobuf/ProtoUtils.java index 57b245a583..ec62749a60 100644 --- a/protobuf/src/main/java/io/grpc/protobuf/ProtoUtils.java +++ b/protobuf/src/main/java/io/grpc/protobuf/ProtoUtils.java @@ -36,6 +36,7 @@ import com.google.protobuf.Message; import com.google.protobuf.Message.Builder; import com.google.protobuf.MessageLite; import com.google.protobuf.util.JsonFormat; +import com.google.protobuf.util.JsonFormat.Parser; import com.google.protobuf.util.JsonFormat.Printer; import io.grpc.ExperimentalApi; @@ -78,9 +79,20 @@ public class ProtoUtils { */ @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1786") public static Marshaller jsonMarshaller(final T defaultInstance) { + final Parser parser = JsonFormat.parser(); final Printer printer = JsonFormat.printer(); - // TODO(carl-mastrangelo): Add support for ExtensionRegistry (TypeRegistry?) - final JsonFormat.Parser parser = JsonFormat.parser(); + return jsonMarshaller(defaultInstance, parser, printer); + } + + /** + * Create a {@code Marshaller} for json protos of the same type as {@code defaultInstance}. + * + *

This is an unstable API and has not been optimized yet for performance. + */ + @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1786") + public static Marshaller jsonMarshaller( + final T defaultInstance, final Parser parser, final Printer printer) { + final Charset charset = Charset.forName("UTF-8"); return new Marshaller() {