List of usage examples for com.google.gson JsonElement toString
@Override
public String toString()
From source file:com.sonie.web.util.HueUtil.java
License:Open Source License
public static Map<String, HueGroup> getGroupsWithId(ApplicationConfiguration config) { RestTemplate restTemplate = new RestTemplate(); Map<String, HueGroup> groups = new HashMap<>(); String url = "http://" + config.getHue().getIp() + "/api/" + config.getHue().getUser() + "/groups"; ResponseEntity<String> hueGroupsResponse = restTemplate.exchange(url, HttpMethod.GET, null, new ParameterizedTypeReference<String>() { });//from w w w . ja va2s. c o m JsonParser parser = new JsonParser(); JsonObject jsonObject = parser.parse(hueGroupsResponse.getBody()).getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { String id = entry.getKey(); HueGroup group = new HueGroup(); List<String> lights = new ArrayList<>(); group.setName(entry.getValue().getAsJsonObject().get("name").toString()); JsonArray jsonArray = entry.getValue().getAsJsonObject().get("lights").getAsJsonArray(); for (JsonElement obj : jsonArray) { lights.add(obj.toString()); } group.setLights(lights); groups.put(id, group); } return groups; }
From source file:com.sowhoyou.twitchbot.chatbot.BotHelper.java
License:Open Source License
public void getModList() { try {/* w w w . java2 s. co m*/ URL url = new URL(("http://tmi.twitch.tv/group/user/" + this.getBot().getBotConfig().getBotChannel().toLowerCase().replace("#", "") + "/chatters")); String jText = IOUtils.toString(url); JsonParser jPhraser = new JsonParser(); JsonArray jArray = new JsonArray(); JsonObject jObject = jPhraser.parse(jText).getAsJsonObject(); String streamObject = jObject.get("chatters").toString(); jObject = jPhraser.parse(streamObject).getAsJsonObject(); String moderators = jObject.get("moderators").toString(); jArray.addAll(jPhraser.parse(moderators).getAsJsonArray()); StringBuilder sb = new StringBuilder(); int viewerCount = 0; for (JsonElement s : jArray.getAsJsonArray()) { User u = this.getBot().getUserList() .getUser(s.toString().toLowerCase().replace('"', ' ').replace(" ", "")); u.setMod(true); sb.append(", " + s.toString().replace('"', ' ').replace(" ", "")); viewerCount++; } if (viewerCount > 0) { this.getBot().sendDebug("Moderators: [" + sb.toString().replaceFirst(", ", "") + "]"); } else { this.getBot().sendDebug("There are No Visable Moderators!"); } } catch (Exception e) { } }
From source file:com.sowhoyou.twitchbot.chatbot.BotHelper.java
License:Open Source License
public String getModListToString() { try {/*from w ww . j a va 2 s . c om*/ URL url = new URL(("http://tmi.twitch.tv/group/user/" + this.getBot().getBotConfig().getBotChannel().toLowerCase().replace("#", "") + "/chatters")); String jText = IOUtils.toString(url); JsonParser jPhraser = new JsonParser(); JsonArray jArray = new JsonArray(); JsonObject jObject = jPhraser.parse(jText).getAsJsonObject(); String streamObject = jObject.get("chatters").toString(); jObject = jPhraser.parse(streamObject).getAsJsonObject(); String moderators = jObject.get("moderators").toString(); jArray.addAll(jPhraser.parse(moderators).getAsJsonArray()); StringBuilder sb = new StringBuilder(); int viewerCount = 0; for (JsonElement s : jArray.getAsJsonArray()) { sb.append(", " + s.toString().replace('"', ' ').replace(" ", "")); viewerCount++; } if (viewerCount > 0) { return ("Moderators: [" + sb.toString().replaceFirst(", ", "") + "]"); } else { return ("There are No Visable Moderators!"); } } catch (Exception e) { } return null; }
From source file:com.sowhoyou.twitchbot.chatbot.BotHelper.java
License:Open Source License
public void getViewerList() { try {/*from w w w .j ava2s .c o m*/ URL url = new URL(("http://tmi.twitch.tv/group/user/" + this.getBot().getBotConfig().getBotChannel().toLowerCase().replace("#", "") + "/chatters")); String jText = IOUtils.toString(url); JsonParser jPhraser = new JsonParser(); JsonArray jArray = new JsonArray(); JsonObject jObject = jPhraser.parse(jText).getAsJsonObject(); String streamObject = jObject.get("chatters").toString(); jObject = jPhraser.parse(streamObject).getAsJsonObject(); String viewers = jObject.get("viewers").toString(); jArray.addAll(jPhraser.parse(viewers).getAsJsonArray()); StringBuilder sb = new StringBuilder(); int viewerCount = 0; for (JsonElement s : jArray.getAsJsonArray()) { User u = this.getBot().getUserList() .getUser(s.toString().toLowerCase().replace('"', ' ').replace(" ", "")); u.setMod(false); sb.append(", " + s.toString().replace('"', ' ').replace(" ", "")); viewerCount++; } if (viewerCount > 0) { this.getBot().sendDebug("Viewers: [" + sb.toString().replaceFirst(", ", "") + "]"); } else { this.getBot().sendDebug("There are No Visable Viewers!"); } } catch (Exception e) { } }
From source file:com.sowhoyou.twitchbot.chatbot.BotHelper.java
License:Open Source License
public String getViewerListToString() { try {// ww w .jav a2s . co m URL url = new URL(("http://tmi.twitch.tv/group/user/" + this.getBot().getBotConfig().getBotChannel().toLowerCase().replace("#", "") + "/chatters")); String jText = IOUtils.toString(url); JsonParser jPhraser = new JsonParser(); JsonArray jArray = new JsonArray(); JsonObject jObject = jPhraser.parse(jText).getAsJsonObject(); String streamObject = jObject.get("chatters").toString(); jObject = jPhraser.parse(streamObject).getAsJsonObject(); String viewers = jObject.get("viewers").toString(); jArray.addAll(jPhraser.parse(viewers).getAsJsonArray()); StringBuilder sb = new StringBuilder(); int viewerCount = 0; for (JsonElement s : jArray.getAsJsonArray()) { sb.append(", " + s.toString().replace('"', ' ').replace(" ", "")); viewerCount++; } if (viewerCount > 0) { return ("Viewers: [" + sb.toString().replaceFirst(", ", "") + "]"); } else { return ("There are No Visable Viewers!"); } } catch (Exception e) { } return null; }
From source file:com.springer.api.services.impl.BaseSpringerQuery.java
License:Apache License
@Override public PagedList<E> list() { InputStream jsonContent = null; try {/* w ww .j ava 2 s .co m*/ jsonContent = callApiGet(apiUrlBuilder.buildUrl()); JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET)); if (response.isJsonObject()) { JsonObject object = response.getAsJsonObject(); setApiKey(object.get("apiKey").getAsString()); setQuery(object.get("query").getAsString()); List<Cursor> cursors = getGsonBuilder().create().fromJson(object.get("result"), new TypeToken<List<Cursor>>() { }.getType()); PagedArrayList<E> responseList = new PagedArrayList<E>(); ; if (!cursors.isEmpty()) { responseList.setCursor(cursors.get(0)); } JsonElement jsonElement = object.get("records"); if (jsonElement.isJsonArray()) { JsonArray results = jsonElement.getAsJsonArray(); for (JsonElement result : results) { E element = unmarshall(result); responseList.add(element); } } jsonElement = object.get("facets"); if (jsonElement != null) { List<Facet> facets = getGsonBuilder().create().fromJson(jsonElement, new TypeToken<List<Facet>>() { }.getType()); setFacets(facets); } return responseList; } throw new SpringerException("Unknown content found in response:" + response.toString()); } catch (Exception e) { throw new SpringerException(e); } finally { closeStream(jsonContent); } }
From source file:com.stackmob.sdk.request.StackMobAccessTokenRequest.java
License:Apache License
private static StackMobRawCallback getIntermediaryCallback(final StackMobSession session, final StackMobRawCallback callback) { return new StackMobRawCallback() { @Override/*from w w w . j a va 2 s . c o m*/ public void unsent(StackMobException e) { callback.unsent(e); } @Override public void temporaryPasswordResetRequired(StackMobException e) { callback.temporaryPasswordResetRequired(e); } @Override public void done(HttpVerb requestVerb, String requestURL, List<Map.Entry<String, String>> requestHeaders, String requestBody, Integer responseStatusCode, List<Map.Entry<String, String>> responseHeaders, byte[] responseBody) { JsonElement responseElt = new JsonParser().parse(new String(responseBody)); byte[] finalResponseBody = responseBody; if (responseElt.isJsonObject()) { // Parse out the token and expiration JsonElement tokenElt = responseElt.getAsJsonObject().get("access_token"); JsonElement macKeyElt = responseElt.getAsJsonObject().get("mac_key"); JsonElement expirationElt = responseElt.getAsJsonObject().get("expires_in"); JsonElement refreshTokenElt = responseElt.getAsJsonObject().get("refresh_token"); if (tokenElt != null && tokenElt.isJsonPrimitive() && tokenElt.getAsJsonPrimitive().isString() && macKeyElt != null && macKeyElt.isJsonPrimitive() && macKeyElt.getAsJsonPrimitive().isString() && expirationElt != null && expirationElt.isJsonPrimitive() && expirationElt.getAsJsonPrimitive().isNumber() && refreshTokenElt != null && refreshTokenElt.isJsonPrimitive() && refreshTokenElt.getAsJsonPrimitive().isString()) { session.setOAuth2TokensAndExpiration(tokenElt.getAsString(), macKeyElt.getAsString(), refreshTokenElt.getAsString(), expirationElt.getAsInt()); } JsonElement stackmobElt = responseElt.getAsJsonObject().get("stackmob"); if (stackmobElt != null && stackmobElt.isJsonObject()) { // Return only the user to be compatible with the old login JsonElement userElt = stackmobElt.getAsJsonObject().get("user"); session.setLastUserLoginName( userElt.getAsJsonObject().get(session.getUserIdName()).getAsString()); finalResponseBody = userElt.toString().getBytes(); } } callback.setDone(requestVerb, requestURL, requestHeaders, requestBody, responseStatusCode, responseHeaders, finalResponseBody); } @Override public void circularRedirect(String originalUrl, Map<String, String> redirectHeaders, String redirectBody, String newURL) { callback.circularRedirect(originalUrl, redirectHeaders, redirectBody, newURL); } }; }
From source file:com.synflow.cx.internal.instantiation.properties.InstancePropertiesChecker.java
License:Open Source License
/** * Validates the given clocks object./*from w w w . j a va 2s . c o m*/ * * @param clocks * a clocks object * @param parentClocks * a list of parent clocks * @param entityClocks * a list of entity clocks */ private void validateClocks(JsonObject clocks, JsonArray parentClocks, JsonArray entityClocks) { int size = entityClocks.size(); int got = 0; Iterator<JsonElement> it = entityClocks.iterator(); for (Entry<String, JsonElement> pair : clocks.entrySet()) { String clockName = pair.getKey(); if (NO_CLOCK.equals(clockName)) { // no more clocks after this one => mismatch in number of clocks got = clocks.entrySet().size(); break; } if (!it.hasNext()) { // no more entity clocks => mismatch in number of clocks break; } // check we use a valid entity clock name String expected = it.next().getAsString(); if (!clockName.equals(expected)) { handler.addError(clocks, "given clock name '" + clockName + "' does not match entity's clock '" + expected + "'"); } // check value JsonElement value = pair.getValue(); if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) { got++; if (!Iterables.contains(parentClocks, value)) { handler.addError(value, "given clock name '" + value.getAsString() + "' does not appear in parent's clocks " + parentClocks); } } else { handler.addError(value, "invalid clock value: " + value.toString()); } } if (got < size) { String msg = "not enough clocks given, expected " + size + " clocks, got " + got; handler.addError(clocks, msg); } else if (got > size) { String msg = "too many clocks given, expected " + size + " clocks, got " + got; handler.addError(clocks, msg); } }
From source file:com.tapchatapp.android.client.MessageDeserializer.java
License:Apache License
@Override public Message deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (mDebug) { Log.d("MessageDeserializer", "Got message: " + json.toString()); }/*w w w . j a va2 s .c o m*/ JsonObject jsonObject = json.getAsJsonObject(); Class<? extends Message> klass; if (jsonObject.has("_reqid") && !jsonObject.get("_reqid").isJsonNull()) { klass = ResponseMessage.class; } else { JsonElement messageType = jsonObject.get("type"); if (messageType != null) { String type = messageType.getAsString(); klass = TYPES.get(type); if (klass == null) { klass = UnknownMessage.class; } } else { klass = UnknownMessage.class; } } return context.deserialize(json, klass); }
From source file:com.tesla.framework.common.util.json.JSONHelper.java
License:Apache License
@NonNull public static Map<String, String> json2UrlEncodedMap(@NonNull JsonObject data) { Preconditions.checkNotNull(data);/*from w w w .j av a 2s. co m*/ Map<String, String> map = new HashMap<>(); Set<Map.Entry<String, JsonElement>> set = data.entrySet(); for (Map.Entry<String, JsonElement> entry : set) { String key = entry.getKey(); if (TextUtils.isEmpty(key)) { continue; } JsonElement value = entry.getValue(); if (value == null) { continue; } String content = value.isJsonPrimitive() ? value.getAsString() : value.toString(); try { map.put(key, URLEncoder.encode(content, "UTF-8")); } catch (Exception e) { e.printStackTrace(); } } return map; }