Skip to content
Permalink
Browse files
JCLOUDS-1559: add Charset to Json.fromJson InputStream methods
  • Loading branch information
roded authored and gaul committed Dec 5, 2020
1 parent 94f0932 commit 3a7e41f4e2e577b60430a7199cc4ddd1dfeadb62
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
@@ -21,6 +21,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;

import javax.annotation.Resource;
import javax.inject.Inject;
@@ -79,7 +80,7 @@ public T apply(InputStream stream) throws IOException {
@SuppressWarnings("unchecked")
public <V> V apply(InputStream stream, Type type) throws IOException {
try {
return (V) json.fromJson(stream, type);
return (V) json.fromJson(stream, StandardCharsets.UTF_8, type);
} finally {
if (stream != null)
stream.close();
@@ -18,6 +18,7 @@

import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.Charset;

public interface Json {
/**
@@ -48,12 +49,12 @@ public interface Json {
* Deserialize the generic object from json. If the object is not a generic type, use
* {@link #fromJson(Object, Class)}
*/
<T> T fromJson(InputStream json, Type type);
<T> T fromJson(InputStream json, Charset charset, Type type);

/**
* Deserialize the object from json. If the object is a generic type, use
* {@link #fromJson(Object, Type)}
*/
<T> T fromJson(InputStream json, Class<T> classOfT);
<T> T fromJson(InputStream json, Charset charset, Class<T> classOfT);

}
@@ -19,6 +19,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.nio.charset.Charset;

import javax.inject.Inject;
import javax.inject.Singleton;
@@ -51,13 +52,13 @@ public <T> T fromJson(String json, Class<T> classOfT) {

@SuppressWarnings("unchecked")
@Override
public <T> T fromJson(InputStream json, Type type) {
return (T) gson.fromJson(new InputStreamReader(json), type);
public <T> T fromJson(InputStream json, Charset charset, Type type) {
return (T) gson.fromJson(new InputStreamReader(json, charset), type);
}

@Override
public <T> T fromJson(InputStream json, Class<T> classOfT) {
return gson.fromJson(new InputStreamReader(json), classOfT);
public <T> T fromJson(InputStream json, Charset charset, Class<T> classOfT) {
return gson.fromJson(new InputStreamReader(json, charset), classOfT);
}

@Override
@@ -20,7 +20,9 @@
import static com.google.common.primitives.Bytes.asList;
import static org.testng.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -352,6 +354,15 @@ public void autoValueSerializedNames_nullValueInJson() {
SerializedNamesType.create("1234", null));
}

public void nonUtf8InputStream() {
Json json = Guice.createInjector(new GsonModule()).getInstance(Json.class);
String jsonValue = "{\"stringValue\":\"1234\",\"intValue\":1234}";
Charset ebcdicCharset = Charset.forName("IBM-1047");
ByteArrayInputStream inputStream = new ByteArrayInputStream(jsonValue.getBytes(ebcdicCharset));
assertEquals(json.fromJson(inputStream, ebcdicCharset, ObjectNoDefaultConstructor.class),
new ObjectNoDefaultConstructor("1234", 1234));
}

@AutoValue
abstract static class NestedSerializedNamesType {
abstract SerializedNamesType item();

0 comments on commit 3a7e41f

Please sign in to comment.