Skip to content

Commit

Permalink
Support for ASCII in Jackson codec & converter
Browse files Browse the repository at this point in the history
This commit introduces support for writing JSON with an US-ASCII
character encoding in the Jackson encoder and message converter,
treating it like UTF-8.

See gh-25322

(cherry picked from commit 79c339b)
  • Loading branch information
poutsma committed Jun 30, 2020
1 parent fc5a6db commit f4ae18f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 17 deletions.
Expand Up @@ -76,10 +76,11 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple
STREAM_SEPARATORS.put(MediaType.APPLICATION_STREAM_JSON, NEWLINE_SEPARATOR);
STREAM_SEPARATORS.put(MediaType.parseMediaType("application/stream+x-jackson-smile"), new byte[0]);

ENCODINGS = new HashMap<>(JsonEncoding.values().length);
ENCODINGS = new HashMap<>(JsonEncoding.values().length + 1);
for (JsonEncoding encoding : JsonEncoding.values()) {
ENCODINGS.put(encoding.getJavaName(), encoding);
}
ENCODINGS.put("US-ASCII", JsonEncoding.UTF8);
}


Expand Down
Expand Up @@ -24,11 +24,9 @@
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
Expand Down Expand Up @@ -76,7 +74,16 @@
*/
public abstract class AbstractJackson2HttpMessageConverter extends AbstractGenericHttpMessageConverter<Object> {

private static final Map<String, JsonEncoding> ENCODINGS = jsonEncodings();
private static final Map<String, JsonEncoding> ENCODINGS;

static {
ENCODINGS = new HashMap<>(JsonEncoding.values().length + 1);
for (JsonEncoding encoding : JsonEncoding.values()) {
ENCODINGS.put(encoding.getJavaName(), encoding);
}
ENCODINGS.put("US-ASCII", JsonEncoding.UTF8);
}


/**
* The default charset used by the converter.
Expand Down Expand Up @@ -398,9 +405,4 @@ protected Long getContentLength(Object object, @Nullable MediaType contentType)
return super.getContentLength(object, contentType);
}

private static Map<String, JsonEncoding> jsonEncodings() {
return EnumSet.allOf(JsonEncoding.class).stream()
.collect(Collectors.toMap(JsonEncoding::getJavaName, Function.identity()));
}

}
Expand Up @@ -91,6 +91,8 @@ public void canDecode() {
assertFalse(decoder.canDecode(forClass(Pojo.class), APPLICATION_XML));
assertTrue(this.decoder.canDecode(forClass(Pojo.class),
new MediaType("application", "json", StandardCharsets.UTF_8)));
assertTrue(this.decoder.canDecode(forClass(Pojo.class),
new MediaType("application", "json", StandardCharsets.US_ASCII)));
assertTrue(this.decoder.canDecode(forClass(Pojo.class),
new MediaType("application", "json", StandardCharsets.ISO_8859_1)));

Expand Down Expand Up @@ -246,8 +248,7 @@ public void decodeNonUnicode() {
stringBuffer("{\"føø\":\"bår\"}", StandardCharsets.ISO_8859_1)
);

testDecode(input, ResolvableType.forType(new ParameterizedTypeReference<Map<String, String>>() {
}),
testDecode(input, ResolvableType.forType(new ParameterizedTypeReference<Map<String, String>>() {}),
step -> step.assertNext(o -> {
assertTrue(o instanceof Map);
Map<String, String> map = (Map<String, String>) o;
Expand All @@ -263,8 +264,7 @@ public void decodeNonUnicode() {
public void decodeMonoNonUtf8Encoding() {
Mono<DataBuffer> input = stringBuffer("{\"foo\":\"bar\"}", StandardCharsets.UTF_16);

testDecodeToMono(input, ResolvableType.forType(new ParameterizedTypeReference<Map<String, String>>() {
}),
testDecodeToMono(input, ResolvableType.forType(new ParameterizedTypeReference<Map<String, String>>() {}),
step -> step.assertNext(o -> {
Map<String, String> map = (Map<String, String>) o;
assertEquals("bar", map.get("foo"));
Expand All @@ -274,6 +274,24 @@ public void decodeMonoNonUtf8Encoding() {
null);
}

@Test
@SuppressWarnings("unchecked")
public void decodeAscii() {
Flux<DataBuffer> input = Flux.concat(
stringBuffer("{\"foo\":\"bar\"}", StandardCharsets.US_ASCII)
);

testDecode(input, ResolvableType.forType(new ParameterizedTypeReference<Map<String, String>>() {}),
step -> step.assertNext(o -> {
Map<String, String> map = (Map<String, String>) o;
assertEquals("bar", map.get("foo"));
})
.verifyComplete(),
MediaType.parseMediaType("application/json; charset=us-ascii"),
null);
}


private Mono<DataBuffer> stringBuffer(String value) {
return stringBuffer(value, StandardCharsets.UTF_8);
}
Expand Down
Expand Up @@ -42,7 +42,9 @@
import org.springframework.util.MimeTypeUtils;

import static java.util.Collections.singletonMap;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8;
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM;
Expand Down Expand Up @@ -73,6 +75,8 @@ public void canEncode() {

assertTrue(this.encoder.canEncode(ResolvableType.forClass(Pojo.class),
new MediaType("application", "json", StandardCharsets.UTF_8)));
assertTrue(this.encoder.canEncode(ResolvableType.forClass(Pojo.class),
new MediaType("application", "json", StandardCharsets.US_ASCII)));
assertFalse(this.encoder.canEncode(ResolvableType.forClass(Pojo.class),
new MediaType("application", "json", StandardCharsets.ISO_8859_1)));

Expand Down Expand Up @@ -223,6 +227,17 @@ public void encodeWithFlushAfterWriteOff() {
.verify(Duration.ofSeconds(5));
}

@Test
public void encodeAscii() {
Mono<Object> input = Mono.just(new Pojo("foo", "bar"));

testEncode(input, ResolvableType.forClass(Pojo.class), step -> step
.consumeNextWith(expectString("{\"foo\":\"foo\",\"bar\":\"bar\"}"))
.verifyComplete(),
new MimeType("application", "json", StandardCharsets.US_ASCII), null);

}


@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
private static class ParentClass {
Expand Down
Expand Up @@ -43,8 +43,14 @@
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.lang.Nullable;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
* Jackson 2.x converter tests.
Expand All @@ -65,6 +71,7 @@ public void canRead() {
assertTrue(converter.canRead(MyBean.class, new MediaType("application", "json")));
assertTrue(converter.canRead(Map.class, new MediaType("application", "json")));
assertTrue(converter.canRead(MyBean.class, new MediaType("application", "json", StandardCharsets.UTF_8)));
assertTrue(converter.canRead(MyBean.class, new MediaType("application", "json", StandardCharsets.US_ASCII)));
assertTrue(converter.canRead(MyBean.class, new MediaType("application", "json", StandardCharsets.ISO_8859_1)));
}

Expand All @@ -73,6 +80,7 @@ public void canWrite() {
assertTrue(converter.canWrite(MyBean.class, new MediaType("application", "json")));
assertTrue(converter.canWrite(Map.class, new MediaType("application", "json")));
assertTrue(converter.canWrite(MyBean.class, new MediaType("application", "json", StandardCharsets.UTF_8)));
assertTrue(converter.canWrite(MyBean.class, new MediaType("application", "json", StandardCharsets.US_ASCII)));
assertFalse(converter.canWrite(MyBean.class, new MediaType("application", "json", StandardCharsets.ISO_8859_1)));
}

Expand Down Expand Up @@ -465,6 +473,34 @@ public void readNonUnicode() throws Exception {
assertEquals("bår", result.get("føø"));
}

@Test
@SuppressWarnings("unchecked")
public void readAscii() throws Exception {
String body = "{\"foo\":\"bar\"}";
Charset charset = StandardCharsets.US_ASCII;
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(charset));
inputMessage.getHeaders().setContentType(new MediaType("application", "json", charset));
HashMap<String, Object> result = (HashMap<String, Object>) this.converter.read(HashMap.class, inputMessage);

assertEquals(1, result.size());
assertEquals("bar", result.get("foo"));
}

@Test
@SuppressWarnings("unchecked")
public void writeAscii() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
Map<String,Object> body = new HashMap<>();
body.put("foo", "bar");
Charset charset = StandardCharsets.US_ASCII;
MediaType contentType = new MediaType("application", "json", charset);
converter.write(body, contentType, outputMessage);

String result = outputMessage.getBodyAsString(charset);
assertEquals("{\"foo\":\"bar\"}", result);
assertEquals(contentType, outputMessage.getHeaders().getContentType());
}


interface MyInterface {

Expand Down

0 comments on commit f4ae18f

Please sign in to comment.