Map from charset name rather than Charset

With this commit it is no longer assumed that all charset names in the
JsonEncoding can be resolved by Charset.forName. Instead, we store the
charset name itself, rather than the Charset object.
This commit is contained in:
Arjen Poutsma 2020-06-22 09:48:41 +02:00
parent 0a208a4941
commit 405fdd3a57
2 changed files with 10 additions and 11 deletions

View File

@ -69,7 +69,7 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple
private static final Map<MediaType, byte[]> STREAM_SEPARATORS; private static final Map<MediaType, byte[]> STREAM_SEPARATORS;
private static final Map<Charset, JsonEncoding> ENCODINGS; private static final Map<String, JsonEncoding> ENCODINGS;
static { static {
STREAM_SEPARATORS = new HashMap<>(4); STREAM_SEPARATORS = new HashMap<>(4);
@ -78,8 +78,7 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple
ENCODINGS = new HashMap<>(JsonEncoding.values().length); ENCODINGS = new HashMap<>(JsonEncoding.values().length);
for (JsonEncoding encoding : JsonEncoding.values()) { for (JsonEncoding encoding : JsonEncoding.values()) {
Charset charset = Charset.forName(encoding.getJavaName()); ENCODINGS.put(encoding.getJavaName(), encoding);
ENCODINGS.put(charset, encoding);
} }
} }
@ -116,7 +115,7 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple
} }
if (mimeType != null && mimeType.getCharset() != null) { if (mimeType != null && mimeType.getCharset() != null) {
Charset charset = mimeType.getCharset(); Charset charset = mimeType.getCharset();
if (!ENCODINGS.containsKey(charset)) { if (!ENCODINGS.containsKey(charset.name())) {
return false; return false;
} }
} }
@ -287,7 +286,7 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple
protected JsonEncoding getJsonEncoding(@Nullable MimeType mimeType) { protected JsonEncoding getJsonEncoding(@Nullable MimeType mimeType) {
if (mimeType != null && mimeType.getCharset() != null) { if (mimeType != null && mimeType.getCharset() != null) {
Charset charset = mimeType.getCharset(); Charset charset = mimeType.getCharset();
JsonEncoding result = ENCODINGS.get(charset); JsonEncoding result = ENCODINGS.get(charset.name());
if (result != null) { if (result != null) {
return result; return result;
} }

View File

@ -76,7 +76,7 @@ import org.springframework.util.TypeUtils;
*/ */
public abstract class AbstractJackson2HttpMessageConverter extends AbstractGenericHttpMessageConverter<Object> { public abstract class AbstractJackson2HttpMessageConverter extends AbstractGenericHttpMessageConverter<Object> {
private static final Map<Charset, JsonEncoding> ENCODINGS = jsonEncodings(); private static final Map<String, JsonEncoding> ENCODINGS = jsonEncodings();
/** /**
* The default charset used by the converter. * The default charset used by the converter.
@ -184,7 +184,7 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener
} }
if (mediaType != null && mediaType.getCharset() != null) { if (mediaType != null && mediaType.getCharset() != null) {
Charset charset = mediaType.getCharset(); Charset charset = mediaType.getCharset();
if (!ENCODINGS.containsKey(charset)) { if (!ENCODINGS.containsKey(charset.name())) {
return false; return false;
} }
} }
@ -247,7 +247,7 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener
MediaType contentType = inputMessage.getHeaders().getContentType(); MediaType contentType = inputMessage.getHeaders().getContentType();
Charset charset = getCharset(contentType); Charset charset = getCharset(contentType);
boolean isUnicode = ENCODINGS.containsKey(charset); boolean isUnicode = ENCODINGS.containsKey(charset.name());
try { try {
if (inputMessage instanceof MappingJacksonInputMessage) { if (inputMessage instanceof MappingJacksonInputMessage) {
Class<?> deserializationView = ((MappingJacksonInputMessage) inputMessage).getDeserializationView(); Class<?> deserializationView = ((MappingJacksonInputMessage) inputMessage).getDeserializationView();
@ -374,7 +374,7 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener
protected JsonEncoding getJsonEncoding(@Nullable MediaType contentType) { protected JsonEncoding getJsonEncoding(@Nullable MediaType contentType) {
if (contentType != null && contentType.getCharset() != null) { if (contentType != null && contentType.getCharset() != null) {
Charset charset = contentType.getCharset(); Charset charset = contentType.getCharset();
JsonEncoding encoding = ENCODINGS.get(charset); JsonEncoding encoding = ENCODINGS.get(charset.name());
if (encoding != null) { if (encoding != null) {
return encoding; return encoding;
} }
@ -399,9 +399,9 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener
return super.getContentLength(object, contentType); return super.getContentLength(object, contentType);
} }
private static Map<Charset, JsonEncoding> jsonEncodings() { private static Map<String, JsonEncoding> jsonEncodings() {
return EnumSet.allOf(JsonEncoding.class).stream() return EnumSet.allOf(JsonEncoding.class).stream()
.collect(Collectors.toMap(encoding -> Charset.forName(encoding.getJavaName()), Function.identity())); .collect(Collectors.toMap(JsonEncoding::getJavaName, Function.identity()));
} }
} }