@ -22,6 +22,7 @@ import java.io.FileInputStream;
@@ -22,6 +22,7 @@ import java.io.FileInputStream;
import java.io.IOException ;
import java.io.InputStream ;
import java.io.InputStreamReader ;
import java.nio.charset.Charset ;
import org.springframework.core.io.ClassPathResource ;
import org.springframework.core.io.Resource ;
@ -31,20 +32,24 @@ import org.springframework.util.FileCopyUtils;
@@ -31,20 +32,24 @@ import org.springframework.util.FileCopyUtils;
* Internal helper used to load JSON from various sources .
*
* @author Phillip Webb
* @author Andy Wilkinson
* /
class JsonLoader {
private final Class < ? > resourceLoadClass ;
JsonLoader ( Class < ? > resourceLoadClass ) {
private final Charset charset ;
JsonLoader ( Class < ? > resourceLoadClass , Charset charset ) {
this . resourceLoadClass = resourceLoadClass ;
this . charset = charset = = null ? Charset . forName ( "UTF-8" ) : charset ;
}
public Class < ? > getResourceLoadClass ( ) {
Class < ? > getResourceLoadClass ( ) {
return this . resourceLoadClass ;
}
public String getJson ( CharSequence source ) {
String getJson ( CharSequence source ) {
if ( source = = null ) {
return null ;
}
@ -55,15 +60,15 @@ class JsonLoader {
@@ -55,15 +60,15 @@ class JsonLoader {
return source . toString ( ) ;
}
public String getJson ( String path , Class < ? > resourceLoadClass ) {
String getJson ( String path , Class < ? > resourceLoadClass ) {
return getJson ( new ClassPathResource ( path , resourceLoadClass ) ) ;
}
public String getJson ( byte [ ] source ) {
String getJson ( byte [ ] source ) {
return getJson ( new ByteArrayInputStream ( source ) ) ;
}
public String getJson ( File source ) {
String getJson ( File source ) {
try {
return getJson ( new FileInputStream ( source ) ) ;
}
@ -72,7 +77,7 @@ class JsonLoader {
@@ -72,7 +77,7 @@ class JsonLoader {
}
}
public String getJson ( Resource source ) {
String getJson ( Resource source ) {
try {
return getJson ( source . getInputStream ( ) ) ;
}
@ -81,9 +86,10 @@ class JsonLoader {
@@ -81,9 +86,10 @@ class JsonLoader {
}
}
public String getJson ( InputStream source ) {
String getJson ( InputStream source ) {
try {
return FileCopyUtils . copyToString ( new InputStreamReader ( source ) ) ;
return FileCopyUtils
. copyToString ( new InputStreamReader ( source , this . charset ) ) ;
}
catch ( IOException ex ) {
throw new IllegalStateException ( "Unable to load JSON from InputStream" , ex ) ;