List of usage examples for com.google.gson JsonElement toString
@Override
public String toString()
From source file:com.google.code.stackexchange.client.query.impl.BaseStackOverflowApiQuery.java
License:Apache License
protected <A> PagedList<A> unmarshallList(Class<A> clazz, InputStream jsonContent) { try {// ww w. j av a 2 s . c om JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET)); if (response.isJsonObject()) { JsonObject adaptee = response.getAsJsonObject(); return unmarshallList(clazz, adaptee); } throw new StackExchangeApiException("Unknown content found in response:" + response.toString()); } catch (Exception e) { throw new StackExchangeApiException(e); } }
From source file:com.google.jstestdriver.HttpServer.java
License:Apache License
@Override public String postJson(String url, JsonElement json) { stopWatch.start("postJson %s", url); HttpURLConnection connection = null; try {//w w w .ja v a 2 s. c o m logger.trace("Post url:{}\nJSON:\n{}\n", url, json); String jsonString = json.toString(); connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-Type", "application/jsonrequest"); connection.setRequestProperty("Content-Length", Integer.toString(jsonString.getBytes().length)); OutputStreamWriter oWriter = new OutputStreamWriter(connection.getOutputStream()); oWriter.write(jsonString); oWriter.close(); connection.connect(); String response = toString(connection.getInputStream()); logger.trace("Post response:\n{}\n", response); return response; } catch (IOException e) { throw new RuntimeException("Connection error on: " + connection, e); } finally { if (connection != null) { connection.disconnect(); } stopWatch.stop("postJson %s", url); } }
From source file:com.google.walkaround.slob.server.ServerMutateRequest.java
License:Apache License
public void setDeltas(String deltas) { JsonArray jsonArray = new JsonParser().parse(deltas).getAsJsonArray(); payload = new ArrayList<String>(); for (JsonElement e : jsonArray) { payload.add(e.toString()); }// ww w . ja v a2 s . c o m }
From source file:com.googleapis.ajax.services.impl.BaseGoogleSearchApiQuery.java
License:Apache License
@Override public PagedList<T> list() { InputStream jsonContent = null; try {// w ww . j a v a 2s . c o m jsonContent = callApiGet(apiUrlBuilder.buildUrl()); JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET)); if (response.isJsonObject()) { PagedList<T> responseList = unmarshallList(response.getAsJsonObject()); notifyObservers(responseList); return responseList; } throw new GoogleSearchException("Unknown content found in response:" + response.toString()); } catch (Exception e) { throw new GoogleSearchException(e); } finally { closeStream(jsonContent); } }
From source file:com.googleapis.ajax.services.impl.BaseGoogleSearchApiQuery.java
License:Apache License
@Override public T singleResult() { InputStream jsonContent = null; try {// ww w . ja va2 s . co m jsonContent = callApiGet(apiUrlBuilder.buildUrl()); JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET)); if (response.isJsonObject()) { JsonObject json = response.getAsJsonObject(); int status = json.get("responseStatus").getAsInt(); if (status != 200) { throw new GoogleSearchException(json.get("responseDetails").getAsString()); } JsonElement data = json.get("responseData"); if (data != null) { return unmarshall(data); } } throw new GoogleSearchException("Unknown content found in response:" + response.toString()); } catch (Exception e) { throw new GoogleSearchException(e); } finally { closeStream(jsonContent); } }
From source file:com.googleapis.ajax.services.impl.FindFeedQueryImpl.java
License:Apache License
@Override public FindFeedResult singleResult() { InputStream jsonContent = null; try {/*w w w . j a va2 s . co m*/ jsonContent = callApiGet(apiUrlBuilder.buildUrl()); JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET)); if (response.isJsonObject()) { JsonObject json = response.getAsJsonObject(); int status = json.get("responseStatus").getAsInt(); if (status != 200) { throw new GoogleSearchException(json.get("responseDetails").getAsString()); } JsonElement data = json.get("responseData"); if (data != null) { return unmarshall(data); } } throw new GoogleSearchException("Unknown content found in response:" + response.toString()); } catch (Exception e) { throw new GoogleSearchException(e); } finally { closeStream(jsonContent); } }
From source file:com.googleapis.ajax.services.impl.LoadFeedQueryImpl.java
License:Apache License
@Override public LoadFeedResult singleResult() { InputStream jsonContent = null; try {//from w ww . j a v a 2 s. c om jsonContent = callApiGet(apiUrlBuilder.buildUrl()); JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET)); if (response.isJsonObject()) { JsonObject json = response.getAsJsonObject(); int status = json.get("responseStatus").getAsInt(); if (status != 200) { throw new GoogleSearchException(json.get("responseDetails").getAsString()); } JsonElement data = json.get("responseData"); if (data != null) { return unmarshall(data.getAsJsonObject().get("feed")); } } throw new GoogleSearchException("Unknown content found in response:" + response.toString()); } catch (Exception e) { throw new GoogleSearchException(e); } finally { closeStream(jsonContent); } }
From source file:com.googleapis.ajax.services.impl.LookupFeedQueryImpl.java
License:Apache License
@Override public LookupFeedResult singleResult() { InputStream jsonContent = null; try {/*from w ww . j av a 2s .co m*/ jsonContent = callApiGet(apiUrlBuilder.buildUrl()); JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET)); if (response.isJsonObject()) { JsonObject json = response.getAsJsonObject(); int status = json.get("responseStatus").getAsInt(); if (status != 200) { throw new GoogleSearchException(json.get("responseDetails").getAsString()); } JsonElement data = json.get("responseData"); if (data != null) { return unmarshall(data); } } throw new GoogleSearchException("Unknown content found in response:" + response.toString()); } catch (Exception e) { throw new GoogleSearchException(e); } finally { closeStream(jsonContent); } }
From source file:com.googleapis.ajax.services.impl.TranslateLanguageQueryImpl.java
License:Apache License
@Override public PagedList<TranslateLanguageResult> list() { InputStream jsonContent = null; try {//from w ww.ja v a 2 s . co m jsonContent = callApiPost(apiUrlBuilder.buildUrl(), parameters); JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET)); if (response.isJsonObject()) { PagedList<TranslateLanguageResult> responseList = unmarshallList(response.getAsJsonObject()); notifyObservers(responseList); return responseList; } throw new GoogleSearchException("Unknown content found in response:" + response.toString()); } catch (Exception e) { throw new GoogleSearchException(e); } finally { closeStream(jsonContent); } }
From source file:com.googleapis.ajax.services.impl.TranslateLanguageQueryImpl.java
License:Apache License
@Override public TranslateLanguageResult singleResult() { InputStream jsonContent = null; try {//from w ww . ja va 2 s . co m jsonContent = callApiPost(apiUrlBuilder.buildUrl(), parameters); JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET)); if (response.isJsonObject()) { JsonObject json = response.getAsJsonObject(); int status = json.get("responseStatus").getAsInt(); if (status != 200) { throw new GoogleSearchException(json.get("responseDetails").getAsString()); } JsonElement data = json.get("responseData"); if (data != null) { return unmarshall(data); } } throw new GoogleSearchException("Unknown content found in response:" + response.toString()); } catch (Exception e) { throw new GoogleSearchException(e); } finally { closeStream(jsonContent); } }