com.clouway.oauth2.exampleapp.Json Maven / Gradle / Ivy
The newest version!
package com.clouway.oauth2.exampleapp;
import com.google.gson.Gson;
import com.google.inject.Inject;
import com.google.inject.TypeLiteral;
import com.google.sitebricks.client.Transport;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
/**
* @author Miroslav Genov ([email protected])
*/
class Json implements Transport {
private Gson gson;
@Inject
Json(Gson gson) {
this.gson = gson;
}
public T in(InputStream inputStream, Class tClass) throws IOException {
return gson.fromJson(new InputStreamReader(inputStream, "UTF-8"), tClass);
}
@Override
public T in(InputStream inputStream, TypeLiteral typeLiteral) throws IOException {
return gson.fromJson(new InputStreamReader(inputStream,"UTF-8"), typeLiteral.getType());
}
public void out(OutputStream outputStream, Class tClass, T t) {
String json = gson.toJson(t);
try {
outputStream.write(json.getBytes("UTF8"));
} catch (IOException e) {
e.printStackTrace();
}
}
public String contentType() {
return "application/json";
}
}