Add NDJSON and deprecate application/stream+json
Closes gh-21283
This commit is contained in:
@@ -51,6 +51,7 @@ import org.springframework.web.testfixture.xml.Pojo;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON;
|
||||
import static org.springframework.http.MediaType.APPLICATION_NDJSON;
|
||||
import static org.springframework.http.MediaType.APPLICATION_STREAM_JSON;
|
||||
import static org.springframework.http.MediaType.APPLICATION_XML;
|
||||
import static org.springframework.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT;
|
||||
@@ -77,6 +78,7 @@ public class Jackson2JsonDecoderTests extends AbstractDecoderTests<Jackson2JsonD
|
||||
@Test
|
||||
public void canDecode() {
|
||||
assertThat(decoder.canDecode(ResolvableType.forClass(Pojo.class), APPLICATION_JSON)).isTrue();
|
||||
assertThat(decoder.canDecode(ResolvableType.forClass(Pojo.class), APPLICATION_NDJSON)).isTrue();
|
||||
assertThat(decoder.canDecode(ResolvableType.forClass(Pojo.class), APPLICATION_STREAM_JSON)).isTrue();
|
||||
assertThat(decoder.canDecode(ResolvableType.forClass(Pojo.class), null)).isTrue();
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -47,6 +47,7 @@ import static java.util.Collections.singletonMap;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON;
|
||||
import static org.springframework.http.MediaType.APPLICATION_NDJSON;
|
||||
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM;
|
||||
import static org.springframework.http.MediaType.APPLICATION_STREAM_JSON;
|
||||
import static org.springframework.http.MediaType.APPLICATION_XML;
|
||||
@@ -66,6 +67,7 @@ public class Jackson2JsonEncoderTests extends AbstractEncoderTests<Jackson2JsonE
|
||||
public void canEncode() {
|
||||
ResolvableType pojoType = ResolvableType.forClass(Pojo.class);
|
||||
assertThat(this.encoder.canEncode(pojoType, APPLICATION_JSON)).isTrue();
|
||||
assertThat(this.encoder.canEncode(pojoType, APPLICATION_NDJSON)).isTrue();
|
||||
assertThat(this.encoder.canEncode(pojoType, APPLICATION_STREAM_JSON)).isTrue();
|
||||
assertThat(this.encoder.canEncode(pojoType, null)).isTrue();
|
||||
|
||||
|
||||
@@ -197,6 +197,43 @@ public class Jackson2TokenizerTests extends AbstractLeakCheckingTests {
|
||||
testTokenize(asList("[1", ",2,", "3]"), asList("1", "2", "3"), true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void tokenizeStream() {
|
||||
|
||||
// NDJSON (Newline Delimited JSON), JSON Lines
|
||||
testTokenize(
|
||||
asList(
|
||||
"{\"id\":1,\"name\":\"Robert\"}",
|
||||
"\n",
|
||||
"{\"id\":2,\"name\":\"Raide\"}",
|
||||
"\n",
|
||||
"{\"id\":3,\"name\":\"Ford\"}"
|
||||
),
|
||||
asList(
|
||||
"{\"id\":1,\"name\":\"Robert\"}",
|
||||
"{\"id\":2,\"name\":\"Raide\"}",
|
||||
"{\"id\":3,\"name\":\"Ford\"}"
|
||||
),
|
||||
true);
|
||||
|
||||
// JSON Sequence with newline separator
|
||||
testTokenize(
|
||||
asList(
|
||||
"\n",
|
||||
"{\"id\":1,\"name\":\"Robert\"}",
|
||||
"\n",
|
||||
"{\"id\":2,\"name\":\"Raide\"}",
|
||||
"\n",
|
||||
"{\"id\":3,\"name\":\"Ford\"}"
|
||||
),
|
||||
asList(
|
||||
"{\"id\":1,\"name\":\"Robert\"}",
|
||||
"{\"id\":2,\"name\":\"Raide\"}",
|
||||
"{\"id\":3,\"name\":\"Ford\"}"
|
||||
),
|
||||
true);
|
||||
}
|
||||
|
||||
private void testTokenize(List<String> input, List<String> output, boolean tokenize) {
|
||||
StepVerifier.FirstStep<String> builder = StepVerifier.create(decode(input, tokenize, -1));
|
||||
output.forEach(expected -> builder.assertNext(actual -> {
|
||||
|
||||
Reference in New Issue
Block a user