Polishing
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -114,7 +114,7 @@ public abstract class Jackson2CodecSupport {
|
||||
|
||||
@Nullable
|
||||
protected MethodParameter getParameter(ResolvableType type) {
|
||||
return type.getSource() instanceof MethodParameter ? (MethodParameter) type.getSource() : null;
|
||||
return (type.getSource() instanceof MethodParameter ? (MethodParameter) type.getSource() : null);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -32,7 +32,6 @@ import reactor.core.publisher.Flux;
|
||||
import org.springframework.core.codec.DecodingException;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link Function} to transform a JSON stream of arbitrary size, byte array
|
||||
@@ -40,6 +39,7 @@ import org.springframework.util.Assert;
|
||||
* well-formed JSON object.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
*/
|
||||
class Jackson2Tokenizer {
|
||||
@@ -55,39 +55,17 @@ class Jackson2Tokenizer {
|
||||
private int arrayDepth;
|
||||
|
||||
// TODO: change to ByteBufferFeeder when supported by Jackson
|
||||
// See https://github.com/FasterXML/jackson-core/issues/478
|
||||
private final ByteArrayFeeder inputFeeder;
|
||||
|
||||
|
||||
private Jackson2Tokenizer(JsonParser parser, boolean tokenizeArrayElements) {
|
||||
Assert.notNull(parser, "'parser' must not be null");
|
||||
|
||||
this.parser = parser;
|
||||
this.tokenizeArrayElements = tokenizeArrayElements;
|
||||
this.tokenBuffer = new TokenBuffer(parser);
|
||||
this.inputFeeder = (ByteArrayFeeder) this.parser.getNonBlockingInputFeeder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tokenize the given {@code Flux<DataBuffer>} into {@code Flux<TokenBuffer>}.
|
||||
* @param dataBuffers the source data buffers
|
||||
* @param jsonFactory the factory to use
|
||||
* @param tokenizeArrayElements if {@code true} and the "top level" JSON
|
||||
* object is an array, each element is returned individually, immediately
|
||||
* after it is received.
|
||||
* @return the result token buffers
|
||||
*/
|
||||
public static Flux<TokenBuffer> tokenize(Flux<DataBuffer> dataBuffers, JsonFactory jsonFactory,
|
||||
boolean tokenizeArrayElements) {
|
||||
|
||||
try {
|
||||
JsonParser parser = jsonFactory.createNonBlockingByteArrayParser();
|
||||
Jackson2Tokenizer tokenizer = new Jackson2Tokenizer(parser, tokenizeArrayElements);
|
||||
return dataBuffers.flatMap(tokenizer::tokenize, Flux::error, tokenizer::endOfInput);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
return Flux.error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private Flux<TokenBuffer> tokenize(DataBuffer dataBuffer) {
|
||||
byte[] bytes = new byte[dataBuffer.readableByteCount()];
|
||||
@@ -99,8 +77,7 @@ class Jackson2Tokenizer {
|
||||
return parseTokenBufferFlux();
|
||||
}
|
||||
catch (JsonProcessingException ex) {
|
||||
return Flux.error(new DecodingException(
|
||||
"JSON decoding error: " + ex.getOriginalMessage(), ex));
|
||||
return Flux.error(new DecodingException("JSON decoding error: " + ex.getOriginalMessage(), ex));
|
||||
}
|
||||
catch (IOException ex) {
|
||||
return Flux.error(ex);
|
||||
@@ -113,8 +90,7 @@ class Jackson2Tokenizer {
|
||||
return parseTokenBufferFlux();
|
||||
}
|
||||
catch (JsonProcessingException ex) {
|
||||
return Flux.error(new DecodingException(
|
||||
"JSON decoding error: " + ex.getOriginalMessage(), ex));
|
||||
return Flux.error(new DecodingException("JSON decoding error: " + ex.getOriginalMessage(), ex));
|
||||
}
|
||||
catch (IOException ex) {
|
||||
return Flux.error(ex);
|
||||
@@ -127,12 +103,11 @@ class Jackson2Tokenizer {
|
||||
while (true) {
|
||||
JsonToken token = this.parser.nextToken();
|
||||
// SPR-16151: Smile data format uses null to separate documents
|
||||
if ((token == JsonToken.NOT_AVAILABLE) ||
|
||||
if (token == JsonToken.NOT_AVAILABLE ||
|
||||
(token == null && (token = this.parser.nextToken()) == null)) {
|
||||
break;
|
||||
}
|
||||
updateDepth(token);
|
||||
|
||||
if (!this.tokenizeArrayElements) {
|
||||
processTokenNormal(token, result);
|
||||
}
|
||||
@@ -163,8 +138,7 @@ class Jackson2Tokenizer {
|
||||
private void processTokenNormal(JsonToken token, List<TokenBuffer> result) throws IOException {
|
||||
this.tokenBuffer.copyCurrentEvent(this.parser);
|
||||
|
||||
if ((token.isStructEnd() || token.isScalarValue()) &&
|
||||
this.objectDepth == 0 && this.arrayDepth == 0) {
|
||||
if ((token.isStructEnd() || token.isScalarValue()) && this.objectDepth == 0 && this.arrayDepth == 0) {
|
||||
result.add(this.tokenBuffer);
|
||||
this.tokenBuffer = new TokenBuffer(this.parser);
|
||||
}
|
||||
@@ -176,8 +150,7 @@ class Jackson2Tokenizer {
|
||||
this.tokenBuffer.copyCurrentEvent(this.parser);
|
||||
}
|
||||
|
||||
if (this.objectDepth == 0 &&
|
||||
(this.arrayDepth == 0 || this.arrayDepth == 1) &&
|
||||
if (this.objectDepth == 0 && (this.arrayDepth == 0 || this.arrayDepth == 1) &&
|
||||
(token == JsonToken.END_OBJECT || token.isScalarValue())) {
|
||||
result.add(this.tokenBuffer);
|
||||
this.tokenBuffer = new TokenBuffer(this.parser);
|
||||
@@ -189,4 +162,26 @@ class Jackson2Tokenizer {
|
||||
(token == JsonToken.END_ARRAY && this.arrayDepth == 0));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tokenize the given {@code Flux<DataBuffer>} into {@code Flux<TokenBuffer>}.
|
||||
* @param dataBuffers the source data buffers
|
||||
* @param jsonFactory the factory to use
|
||||
* @param tokenizeArrayElements if {@code true} and the "top level" JSON object is
|
||||
* an array, each element is returned individually immediately after it is received
|
||||
* @return the resulting token buffers
|
||||
*/
|
||||
public static Flux<TokenBuffer> tokenize(
|
||||
Flux<DataBuffer> dataBuffers, JsonFactory jsonFactory, boolean tokenizeArrayElements) {
|
||||
|
||||
try {
|
||||
JsonParser parser = jsonFactory.createNonBlockingByteArrayParser();
|
||||
Jackson2Tokenizer tokenizer = new Jackson2Tokenizer(parser, tokenizeArrayElements);
|
||||
return dataBuffers.flatMap(tokenizer::tokenize, Flux::error, tokenizer::endOfInput);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
return Flux.error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -39,11 +39,10 @@ import org.springframework.web.context.ServletContextAware;
|
||||
/**
|
||||
* Subclass of {@link GenericApplicationContext}, suitable for web environments.
|
||||
*
|
||||
* <p>Implements the
|
||||
* {@link org.springframework.web.context.ConfigurableWebApplicationContext},
|
||||
* but is not intended for declarative setup in {@code web.xml}. Instead,
|
||||
* it is designed for programmatic setup, for example for building nested contexts or
|
||||
* for use within Spring 3.1 {@link org.springframework.web.WebApplicationInitializer}s.
|
||||
* <p>Implements {@link org.springframework.web.context.ConfigurableWebApplicationContext},
|
||||
* but is not intended for declarative setup in {@code web.xml}. Instead, it is designed
|
||||
* for programmatic setup, for example for building nested contexts or for use within
|
||||
* {@link org.springframework.web.WebApplicationInitializer WebApplicationInitializers}.
|
||||
*
|
||||
* <p><b>If you intend to implement a WebApplicationContext that reads bean definitions
|
||||
* from configuration files, consider deriving from AbstractRefreshableWebApplicationContext,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,7 +23,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -43,21 +42,13 @@ import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.http.codec.Pojo;
|
||||
import org.springframework.util.MimeType;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.core.ResolvableType.forClass;
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON;
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8;
|
||||
import static org.springframework.http.MediaType.APPLICATION_STREAM_JSON;
|
||||
import static org.springframework.http.MediaType.APPLICATION_XML;
|
||||
import static org.springframework.http.codec.json.Jackson2JsonDecoder.JSON_VIEW_HINT;
|
||||
import static org.springframework.http.codec.json.JacksonViewBean.MyJacksonView1;
|
||||
import static org.springframework.http.codec.json.JacksonViewBean.MyJacksonView3;
|
||||
import static java.util.Arrays.*;
|
||||
import static java.util.Collections.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.core.ResolvableType.*;
|
||||
import static org.springframework.http.MediaType.*;
|
||||
import static org.springframework.http.codec.json.Jackson2JsonDecoder.*;
|
||||
import static org.springframework.http.codec.json.JacksonViewBean.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link Jackson2JsonDecoder}.
|
||||
@@ -80,7 +71,7 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
|
||||
assertFalse(decoder.canDecode(forClass(Pojo.class), APPLICATION_XML));
|
||||
}
|
||||
|
||||
@Test // SPR-15866
|
||||
@Test // SPR-15866
|
||||
public void canDecodeWithProvidedMimeType() {
|
||||
MimeType textJavascript = new MimeType("text", "javascript", StandardCharsets.UTF_8);
|
||||
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder(new ObjectMapper(), textJavascript);
|
||||
@@ -269,20 +260,24 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
|
||||
public String getProperty2() {
|
||||
return property2;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@JsonDeserialize(using = Deserializer.class)
|
||||
public static class TestObject {
|
||||
|
||||
private int test;
|
||||
|
||||
public int getTest() {
|
||||
return test;
|
||||
}
|
||||
|
||||
public void setTest(int test) {
|
||||
this.test = test;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class Deserializer extends StdDeserializer<TestObject> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@@ -292,8 +287,7 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
|
||||
}
|
||||
|
||||
@Override
|
||||
public TestObject deserialize(JsonParser p,
|
||||
DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||
public TestObject deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||
JsonNode node = p.readValueAsTree();
|
||||
TestObject result = new TestObject();
|
||||
result.setTest(node.get("test").asInt());
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -45,10 +45,10 @@ import static java.util.Collections.*;
|
||||
*/
|
||||
public class Jackson2TokenizerTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
private JsonFactory jsonFactory;
|
||||
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
|
||||
@Before
|
||||
public void createParser() {
|
||||
@@ -56,6 +56,7 @@ public class Jackson2TokenizerTests extends AbstractDataBufferAllocatingTestCase
|
||||
this.objectMapper = new ObjectMapper(this.jsonFactory);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void doNotTokenizeArrayElements() {
|
||||
testTokenize(
|
||||
@@ -178,7 +179,7 @@ public class Jackson2TokenizerTests extends AbstractDataBufferAllocatingTestCase
|
||||
testTokenize(asList("[1", ",2,", "3]"), asList("1", "2", "3"), true);
|
||||
}
|
||||
|
||||
@Test(expected = DecodingException.class) // SPR-16521
|
||||
@Test(expected = DecodingException.class) // SPR-16521
|
||||
public void jsonEOFExceptionIsWrappedAsDecodingError() {
|
||||
Flux<DataBuffer> source = Flux.just(stringBuffer("{\"status\": \"noClosingQuote}"));
|
||||
Flux<TokenBuffer> tokens = Jackson2Tokenizer.tokenize(source, this.jsonFactory, false);
|
||||
@@ -187,11 +188,9 @@ public class Jackson2TokenizerTests extends AbstractDataBufferAllocatingTestCase
|
||||
|
||||
|
||||
private void testTokenize(List<String> source, List<String> expected, boolean tokenizeArrayElements) {
|
||||
|
||||
Flux<TokenBuffer> tokenBufferFlux = Jackson2Tokenizer.tokenize(
|
||||
Flux.fromIterable(source).map(this::stringBuffer),
|
||||
this.jsonFactory,
|
||||
tokenizeArrayElements);
|
||||
this.jsonFactory, tokenizeArrayElements);
|
||||
|
||||
Flux<String> result = tokenBufferFlux
|
||||
.map(tokenBuffer -> {
|
||||
@@ -228,4 +227,5 @@ public class Jackson2TokenizerTests extends AbstractDataBufferAllocatingTestCase
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user