1919import static java .util .Objects .requireNonNull ;
2020
2121import java .util .function .Consumer ;
22+ import java .util .function .Function ;
23+
24+ import org .slf4j .Logger ;
25+ import org .slf4j .LoggerFactory ;
2226
2327import com .google .protobuf .Message ;
2428import com .google .protobuf .util .JsonFormat ;
3034 * to and from JSON.
3135 */
3236public final class GsonGrpcJsonMarshallerBuilder {
37+ private static final Logger logger = LoggerFactory .getLogger (GsonGrpcJsonMarshallerBuilder .class );
38+
39+ private static boolean loggedJsonParserCustomizerWarning ;
40+ private static boolean loggedJsonPrinterCustomizerWarning ;
3341
3442 @ Nullable
35- private Consumer < JsonFormat .Parser > jsonParserCustomizer ;
43+ private Function < JsonFormat . Parser , JsonFormat .Parser > jsonParserCustomizer ;
3644
3745 @ Nullable
38- private Consumer < JsonFormat .Printer > jsonPrinterCustomizer ;
46+ private Function < JsonFormat . Printer , JsonFormat .Printer > jsonPrinterCustomizer ;
3947
4048 GsonGrpcJsonMarshallerBuilder () {}
4149
50+ /**
51+ * Adds a {@link Function} that returns customized the {@link JsonFormat.Parser}
52+ * used when deserializing a JSON payload into a {@link Message}.
53+ */
54+ public GsonGrpcJsonMarshallerBuilder jsonParserCustomizer (
55+ Function <? super JsonFormat .Parser , JsonFormat .Parser > jsonParserCustomizer ) {
56+ requireNonNull (jsonParserCustomizer , "jsonParserCustomizer" );
57+ if (this .jsonParserCustomizer == null ) {
58+ @ SuppressWarnings ("unchecked" )
59+ final Function <JsonFormat .Parser , JsonFormat .Parser > cast =
60+ (Function <JsonFormat .Parser , JsonFormat .Parser >) jsonParserCustomizer ;
61+ this .jsonParserCustomizer = cast ;
62+ } else {
63+ this .jsonParserCustomizer = this .jsonParserCustomizer .andThen (jsonParserCustomizer );
64+ }
65+ return this ;
66+ }
67+
4268 /**
4369 * Adds a {@link Consumer} that can customize the {@link JsonFormat.Parser}
4470 * used when deserializing a JSON payload into a {@link Message}.
71+ *
72+ * @deprecated {@link JsonFormat.Parser} is immutable so all changes applied in the {@link Consumer}
73+ * will be lost. Please use the {@link #jsonParserCustomizer(Function) jsonParserCustomizer}
74+ * which accepts {@link Function} parameter instead.
4575 */
76+ @ Deprecated
4677 public GsonGrpcJsonMarshallerBuilder jsonParserCustomizer (
4778 Consumer <? super JsonFormat .Parser > jsonParserCustomizer ) {
79+ if (!loggedJsonParserCustomizerWarning ) {
80+ logger .warn ("{}.jsonParserCustomizer(Consumer) does not work as expected, " +
81+ "use jsonParserCustomizer(Function)." ,
82+ getClass ().getSimpleName ());
83+ loggedJsonParserCustomizerWarning = true ;
84+ }
4885 requireNonNull (jsonParserCustomizer , "jsonParserCustomizer" );
4986 if (this .jsonParserCustomizer == null ) {
5087 @ SuppressWarnings ("unchecked" )
5188 final Consumer <JsonFormat .Parser > cast = (Consumer <JsonFormat .Parser >) jsonParserCustomizer ;
52- this .jsonParserCustomizer = cast ;
89+ this .jsonParserCustomizer = parser -> {
90+ cast .accept (parser );
91+ return parser ;
92+ };
5393 } else {
54- this .jsonParserCustomizer = this .jsonParserCustomizer .andThen (jsonParserCustomizer );
94+ this .jsonParserCustomizer = this .jsonParserCustomizer .andThen (parser -> {
95+ jsonParserCustomizer .accept (parser );
96+ return parser ;
97+ });
5598 }
5699 return this ;
57100 }
58101
59102 /**
60103 * Adds a {@link Consumer} that can customize the {@link JsonFormat.Printer}
61104 * used when serializing a {@link Message} into a JSON payload.
105+ *
106+ * @deprecated {@link JsonFormat.Printer} is immutable so all changes applied in the {@link Consumer}
107+ * will be lost. Please use the {@link #jsonPrinterCustomizer(Function) jsonParserCustomizer}
108+ * which accepts {@link Function} parameter instead.
62109 */
110+ @ Deprecated
63111 public GsonGrpcJsonMarshallerBuilder jsonPrinterCustomizer (
64112 Consumer <? super JsonFormat .Printer > jsonPrinterCustomizer ) {
113+ if (!loggedJsonPrinterCustomizerWarning ) {
114+ logger .warn ("{}.jsonPrinterCustomizer(Consumer) does not work as expected; " +
115+ "use jsonPrinterCustomizer(Function)." ,
116+ getClass ().getSimpleName ());
117+ loggedJsonPrinterCustomizerWarning = true ;
118+ }
119+
65120 requireNonNull (jsonPrinterCustomizer , "jsonPrinterCustomizer" );
66121 if (this .jsonPrinterCustomizer == null ) {
67122 @ SuppressWarnings ("unchecked" )
68123 final Consumer <JsonFormat .Printer > cast = (Consumer <JsonFormat .Printer >) jsonPrinterCustomizer ;
124+ this .jsonPrinterCustomizer = printer -> {
125+ cast .accept (printer );
126+ return printer ;
127+ };
128+ } else {
129+ this .jsonPrinterCustomizer = this .jsonPrinterCustomizer .andThen (printer -> {
130+ jsonPrinterCustomizer .accept (printer );
131+ return printer ;
132+ });
133+ }
134+ return this ;
135+ }
136+
137+ /**
138+ * Adds a {@link Function} that returns customized the {@link JsonFormat.Printer}
139+ * used when serializing a {@link Message} into a JSON payload.
140+ */
141+ public GsonGrpcJsonMarshallerBuilder jsonPrinterCustomizer (
142+ Function <? super JsonFormat .Printer , JsonFormat .Printer > jsonPrinterCustomizer ) {
143+ requireNonNull (jsonPrinterCustomizer , "jsonPrinterCustomizer" );
144+ if (this .jsonPrinterCustomizer == null ) {
145+ @ SuppressWarnings ("unchecked" )
146+ final Function <JsonFormat .Printer , JsonFormat .Printer > cast =
147+ (Function <JsonFormat .Printer , JsonFormat .Printer >) jsonPrinterCustomizer ;
69148 this .jsonPrinterCustomizer = cast ;
70149 } else {
71150 this .jsonPrinterCustomizer = this .jsonPrinterCustomizer .andThen (jsonPrinterCustomizer );
@@ -77,14 +156,14 @@ public GsonGrpcJsonMarshallerBuilder jsonPrinterCustomizer(
77156 * Returns a newly-created {@link GrpcJsonMarshaller}.
78157 */
79158 public GrpcJsonMarshaller build () {
80- final JsonFormat .Printer printer = JsonFormat .printer ().omittingInsignificantWhitespace ();
159+ JsonFormat .Printer printer = JsonFormat .printer ().omittingInsignificantWhitespace ();
81160 if (jsonPrinterCustomizer != null ) {
82- jsonPrinterCustomizer .accept (printer );
161+ printer = jsonPrinterCustomizer .apply (printer );
83162 }
84163
85- final JsonFormat .Parser parser = JsonFormat .parser ().ignoringUnknownFields ();
164+ JsonFormat .Parser parser = JsonFormat .parser ().ignoringUnknownFields ();
86165 if (jsonParserCustomizer != null ) {
87- jsonParserCustomizer .accept (parser );
166+ parser = jsonParserCustomizer .apply (parser );
88167 }
89168 return new GsonGrpcJsonMarshaller (printer , parser );
90169 }
0 commit comments