001/*
002 * Copyright 2002-2020 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.messaging.converter;
018
019import com.google.protobuf.ExtensionRegistry;
020import com.google.protobuf.util.JsonFormat;
021
022import org.springframework.lang.Nullable;
023
024/**
025 * Subclass of {@link ProtobufMessageConverter} for use with the official
026 * {@code "com.google.protobuf:protobuf-java-util"} library for JSON support.
027 *
028 * <p>Most importantly, this class allows for custom JSON parser and printer
029 * configurations through the {@link JsonFormat} utility. If no special parser
030 * or printer configuration is given, default variants will be used instead.
031 *
032 * <p>Requires Protobuf 3.x and {@code "com.google.protobuf:protobuf-java-util"} 3.x,
033 * with 3.3 or higher recommended.
034 *
035 * @author Rossen Stoyanchev
036 * @since 5.2.2
037 */
038public class ProtobufJsonFormatMessageConverter extends ProtobufMessageConverter {
039
040        /**
041         * Constructor with default instances of {@link com.google.protobuf.util.JsonFormat.Parser
042         * JsonFormat.Parser}, {@link com.google.protobuf.util.JsonFormat.Printer
043         * JsonFormat.Printer}, and {@link ExtensionRegistry}.
044         */
045        public ProtobufJsonFormatMessageConverter(@Nullable ExtensionRegistry extensionRegistry) {
046                this(null, null);
047        }
048
049        /**
050         * Constructor with given instances of {@link com.google.protobuf.util.JsonFormat.Parser
051         * JsonFormat.Parser}, {@link com.google.protobuf.util.JsonFormat.Printer
052         * JsonFormat.Printer}, and a default instance of {@link ExtensionRegistry}.
053         */
054        public ProtobufJsonFormatMessageConverter(
055                        @Nullable JsonFormat.Parser parser, @Nullable JsonFormat.Printer printer) {
056
057                this(parser, printer, null);
058        }
059
060        /**
061         * Constructor with given instances of {@link com.google.protobuf.util.JsonFormat.Parser
062         * JsonFormat.Parser}, {@link com.google.protobuf.util.JsonFormat.Printer
063         * JsonFormat.Printer}, and {@link ExtensionRegistry}.
064         */
065        public ProtobufJsonFormatMessageConverter(@Nullable JsonFormat.Parser parser,
066                        @Nullable JsonFormat.Printer printer, @Nullable ExtensionRegistry extensionRegistry) {
067
068                super(new ProtobufJavaUtilSupport(parser, printer), extensionRegistry);
069        }
070
071}