Skip to content

Commit 405fdd3

Browse files
committed
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.
1 parent 0a208a4 commit 405fdd3

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Encoder.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple
6969

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

72-
private static final Map<Charset, JsonEncoding> ENCODINGS;
72+
private static final Map<String, JsonEncoding> ENCODINGS;
7373

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

7979
ENCODINGS = new HashMap<>(JsonEncoding.values().length);
8080
for (JsonEncoding encoding : JsonEncoding.values()) {
81-
Charset charset = Charset.forName(encoding.getJavaName());
82-
ENCODINGS.put(charset, encoding);
81+
ENCODINGS.put(encoding.getJavaName(), encoding);
8382
}
8483
}
8584

@@ -116,7 +115,7 @@ public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType
116115
}
117116
if (mimeType != null && mimeType.getCharset() != null) {
118117
Charset charset = mimeType.getCharset();
119-
if (!ENCODINGS.containsKey(charset)) {
118+
if (!ENCODINGS.containsKey(charset.name())) {
120119
return false;
121120
}
122121
}
@@ -287,7 +286,7 @@ private byte[] streamSeparator(@Nullable MimeType mimeType) {
287286
protected JsonEncoding getJsonEncoding(@Nullable MimeType mimeType) {
288287
if (mimeType != null && mimeType.getCharset() != null) {
289288
Charset charset = mimeType.getCharset();
290-
JsonEncoding result = ENCODINGS.get(charset);
289+
JsonEncoding result = ENCODINGS.get(charset.name());
291290
if (result != null) {
292291
return result;
293292
}

spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
*/
7777
public abstract class AbstractJackson2HttpMessageConverter extends AbstractGenericHttpMessageConverter<Object> {
7878

79-
private static final Map<Charset, JsonEncoding> ENCODINGS = jsonEncodings();
79+
private static final Map<String, JsonEncoding> ENCODINGS = jsonEncodings();
8080

8181
/**
8282
* The default charset used by the converter.
@@ -184,7 +184,7 @@ public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) {
184184
}
185185
if (mediaType != null && mediaType.getCharset() != null) {
186186
Charset charset = mediaType.getCharset();
187-
if (!ENCODINGS.containsKey(charset)) {
187+
if (!ENCODINGS.containsKey(charset.name())) {
188188
return false;
189189
}
190190
}
@@ -247,7 +247,7 @@ private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) th
247247
MediaType contentType = inputMessage.getHeaders().getContentType();
248248
Charset charset = getCharset(contentType);
249249

250-
boolean isUnicode = ENCODINGS.containsKey(charset);
250+
boolean isUnicode = ENCODINGS.containsKey(charset.name());
251251
try {
252252
if (inputMessage instanceof MappingJacksonInputMessage) {
253253
Class<?> deserializationView = ((MappingJacksonInputMessage) inputMessage).getDeserializationView();
@@ -374,7 +374,7 @@ protected JavaType getJavaType(Type type, @Nullable Class<?> contextClass) {
374374
protected JsonEncoding getJsonEncoding(@Nullable MediaType contentType) {
375375
if (contentType != null && contentType.getCharset() != null) {
376376
Charset charset = contentType.getCharset();
377-
JsonEncoding encoding = ENCODINGS.get(charset);
377+
JsonEncoding encoding = ENCODINGS.get(charset.name());
378378
if (encoding != null) {
379379
return encoding;
380380
}
@@ -399,9 +399,9 @@ protected Long getContentLength(Object object, @Nullable MediaType contentType)
399399
return super.getContentLength(object, contentType);
400400
}
401401

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

407407
}

0 commit comments

Comments
 (0)