in.ashwanthkumar.gocd.client.http.GsonObjectParser Maven / Gradle / Ivy
The newest version!
package in.ashwanthkumar.gocd.client.http;
import com.google.api.client.util.ObjectParser;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
/**
* ObjectParser implementation to be used along with google-http-client backed by Gson.
* The default implementation doesn't work at all for some reason, and I'm not sure either why the whole json
* parsing was re-invented in the http-client instead of just delegating it to the underlying framework.
* I'm just going to assume Google engineers are way smarter than me but it's just that I don't know how to use
* the in-built new JsonObjectParser(new GsonFactory)
.
*/
public class GsonObjectParser implements ObjectParser {
final Gson gson;
public GsonObjectParser(Gson gson) {
this.gson = gson;
}
@Override
public T parseAndClose(InputStream in, Charset charset, Class dataClass) throws IOException {
return parseAndClose(new InputStreamReader(in), dataClass);
}
@Override
public Object parseAndClose(InputStream in, Charset charset, Type dataType) throws IOException {
return parseAndClose(new InputStreamReader(in), dataType);
}
@Override
public T parseAndClose(Reader reader, Class dataClass) throws IOException {
return gson.fromJson(reader, dataClass);
}
@Override
public Object parseAndClose(Reader reader, Type dataType) throws IOException {
return gson.fromJson(reader, dataType);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy