Skip to content

Commit

Permalink
Map from charset name rather than Charset
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
poutsma authored and xcl(徐程林) committed Aug 16, 2020
1 parent bf67191 commit 0854319
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
Expand Up @@ -69,7 +69,7 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple

private static final Map<MediaType, byte[]> STREAM_SEPARATORS;

private static final Map<Charset, JsonEncoding> ENCODINGS;
private static final Map<String, JsonEncoding> ENCODINGS;

static {
STREAM_SEPARATORS = new HashMap<>(4);
Expand All @@ -78,8 +78,7 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple

ENCODINGS = new HashMap<>(JsonEncoding.values().length);
for (JsonEncoding encoding : JsonEncoding.values()) {
Charset charset = Charset.forName(encoding.getJavaName());
ENCODINGS.put(charset, encoding);
ENCODINGS.put(encoding.getJavaName(), encoding);
}
}

Expand Down Expand Up @@ -116,7 +115,7 @@ public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType
}
if (mimeType != null && mimeType.getCharset() != null) {
Charset charset = mimeType.getCharset();
if (!ENCODINGS.containsKey(charset)) {
if (!ENCODINGS.containsKey(charset.name())) {
return false;
}
}
Expand Down Expand Up @@ -287,7 +286,7 @@ private byte[] streamSeparator(@Nullable MimeType mimeType) {
protected JsonEncoding getJsonEncoding(@Nullable MimeType mimeType) {
if (mimeType != null && mimeType.getCharset() != null) {
Charset charset = mimeType.getCharset();
JsonEncoding result = ENCODINGS.get(charset);
JsonEncoding result = ENCODINGS.get(charset.name());
if (result != null) {
return result;
}
Expand Down
Expand Up @@ -76,7 +76,7 @@
*/
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.
Expand Down Expand Up @@ -184,7 +184,7 @@ public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) {
}
if (mediaType != null && mediaType.getCharset() != null) {
Charset charset = mediaType.getCharset();
if (!ENCODINGS.containsKey(charset)) {
if (!ENCODINGS.containsKey(charset.name())) {
return false;
}
}
Expand Down Expand Up @@ -247,7 +247,7 @@ private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) th
MediaType contentType = inputMessage.getHeaders().getContentType();
Charset charset = getCharset(contentType);

boolean isUnicode = ENCODINGS.containsKey(charset);
boolean isUnicode = ENCODINGS.containsKey(charset.name());
try {
if (inputMessage instanceof MappingJacksonInputMessage) {
Class<?> deserializationView = ((MappingJacksonInputMessage) inputMessage).getDeserializationView();
Expand Down Expand Up @@ -374,7 +374,7 @@ protected JavaType getJavaType(Type type, @Nullable Class<?> contextClass) {
protected JsonEncoding getJsonEncoding(@Nullable MediaType contentType) {
if (contentType != null && contentType.getCharset() != null) {
Charset charset = contentType.getCharset();
JsonEncoding encoding = ENCODINGS.get(charset);
JsonEncoding encoding = ENCODINGS.get(charset.name());
if (encoding != null) {
return encoding;
}
Expand All @@ -399,9 +399,9 @@ protected Long getContentLength(Object object, @Nullable MediaType contentType)
return super.getContentLength(object, contentType);
}

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

}

0 comments on commit 0854319

Please sign in to comment.