mirror of https://github.com/apache/jclouds.git
JCLOUDS-1187: Do not load Json payloads to memory when serializing objects
This commit is contained in:
parent
37101b7825
commit
a43acaffce
|
@ -30,7 +30,6 @@ import org.jclouds.http.HttpResponse;
|
|||
import org.jclouds.http.HttpResponseException;
|
||||
import org.jclouds.json.Json;
|
||||
import org.jclouds.logging.Logger;
|
||||
import org.jclouds.util.Strings2;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.inject.TypeLiteral;
|
||||
|
@ -57,8 +56,8 @@ public class ParseJson<T> implements Function<HttpResponse, T> {
|
|||
* parses the http response body to create a new {@code <T>}.
|
||||
*/
|
||||
public T apply(HttpResponse from) {
|
||||
InputStream gson = from.getPayload().getInput();
|
||||
try {
|
||||
InputStream gson = from.getPayload().openStream();
|
||||
return apply(gson);
|
||||
} catch (Exception e) {
|
||||
StringBuilder message = new StringBuilder();
|
||||
|
@ -80,7 +79,7 @@ public class ParseJson<T> implements Function<HttpResponse, T> {
|
|||
@SuppressWarnings("unchecked")
|
||||
public <V> V apply(InputStream stream, Type type) throws IOException {
|
||||
try {
|
||||
return (V) json.fromJson(Strings2.toStringAndClose(stream), type);
|
||||
return (V) json.fromJson(stream, type);
|
||||
} finally {
|
||||
if (stream != null)
|
||||
stream.close();
|
||||
|
|
|
@ -30,7 +30,7 @@ public interface Payload extends Closeable {
|
|||
/**
|
||||
* Creates a new InputStream object of the payload.
|
||||
*
|
||||
* @deprecated see openStream
|
||||
* @deprecated see {@link Payload#openStream()}.
|
||||
*/
|
||||
@Deprecated
|
||||
InputStream getInput();
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
*/
|
||||
package org.jclouds.json;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public interface Json {
|
||||
|
@ -42,5 +43,17 @@ public interface Json {
|
|||
* {@link #fromJson(Object, Type)}
|
||||
*/
|
||||
<T> T fromJson(String json, Class<T> classOfT);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
}
|
||||
|
|
|
@ -16,6 +16,11 @@
|
|||
*/
|
||||
package org.jclouds.json.internal;
|
||||
|
||||
import static org.jclouds.util.Closeables2.closeQuietly;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
@ -46,6 +51,27 @@ public class GsonWrapper extends ForwardingObject implements Json {
|
|||
public <T> T fromJson(String json, Class<T> classOfT) {
|
||||
return gson.fromJson(json, classOfT);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> T fromJson(InputStream json, Type type) {
|
||||
Reader reader = new InputStreamReader(json);
|
||||
try {
|
||||
return (T) gson.fromJson(reader, type);
|
||||
} finally {
|
||||
closeQuietly(reader);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T fromJson(InputStream json, Class<T> classOfT) {
|
||||
Reader reader = new InputStreamReader(json);
|
||||
try {
|
||||
return gson.fromJson(reader, classOfT);
|
||||
} finally {
|
||||
closeQuietly(reader);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toJson(Object src) {
|
||||
|
|
Loading…
Reference in New Issue