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.http.codec.json;
018
019import java.nio.charset.Charset;
020import java.nio.charset.StandardCharsets;
021import java.util.Arrays;
022import java.util.Map;
023
024import com.fasterxml.jackson.databind.ObjectMapper;
025import org.reactivestreams.Publisher;
026import reactor.core.publisher.Flux;
027
028import org.springframework.core.ResolvableType;
029import org.springframework.core.codec.StringDecoder;
030import org.springframework.core.io.buffer.DataBuffer;
031import org.springframework.core.io.buffer.DataBufferFactory;
032import org.springframework.core.io.buffer.DefaultDataBufferFactory;
033import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
034import org.springframework.lang.Nullable;
035import org.springframework.util.MimeType;
036import org.springframework.util.MimeTypeUtils;
037
038/**
039 * Decode a byte stream into JSON and convert to Object's with Jackson 2.9,
040 * leveraging non-blocking parsing.
041 *
042 * @author Sebastien Deleuze
043 * @author Rossen Stoyanchev
044 * @since 5.0
045 * @see Jackson2JsonEncoder
046 */
047public class Jackson2JsonDecoder extends AbstractJackson2Decoder {
048
049        private static final StringDecoder STRING_DECODER = StringDecoder.textPlainOnly(Arrays.asList(",", "\n"), false);
050
051        private static final ResolvableType STRING_TYPE = ResolvableType.forClass(String.class);
052
053
054        public Jackson2JsonDecoder() {
055                super(Jackson2ObjectMapperBuilder.json().build());
056        }
057
058        public Jackson2JsonDecoder(ObjectMapper mapper, MimeType... mimeTypes) {
059                super(mapper, mimeTypes);
060        }
061
062        @Override
063        protected Flux<DataBuffer> processInput(Publisher<DataBuffer> input, ResolvableType elementType,
064                        @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
065
066                Flux<DataBuffer> flux = Flux.from(input);
067                if (mimeType == null) {
068                        return flux;
069                }
070
071                // Jackson asynchronous parser only supports UTF-8
072                Charset charset = mimeType.getCharset();
073                if (charset == null || StandardCharsets.UTF_8.equals(charset) || StandardCharsets.US_ASCII.equals(charset)) {
074                        return flux;
075                }
076
077                // Potentially, the memory consumption of this conversion could be improved by using CharBuffers instead
078                // of allocating Strings, but that would require refactoring the buffer tokenization code from StringDecoder
079
080                MimeType textMimeType = new MimeType(MimeTypeUtils.TEXT_PLAIN, charset);
081                Flux<String> decoded = STRING_DECODER.decode(input, STRING_TYPE, textMimeType, null);
082                DataBufferFactory factory = new DefaultDataBufferFactory();
083                return decoded.map(s -> factory.wrap(s.getBytes(StandardCharsets.UTF_8)));
084        }
085
086}