List of usage examples for com.google.gson JsonElement toString
@Override
public String toString()
From source file:com.balajeetm.mystique.core.ConcatMystTurn.java
License:Open Source License
@Override public JsonElement transmute(List<JsonElement> source, JsonObject deps, JsonObject aces, JsonObject turn) { StringBuilder stringBuilder = new StringBuilder(); if (CollectionUtils.isNotEmpty(source)) { String separator = mystiqueLever.asString(turn.get(MystiqueConstants.SEPARATOR), MystiqueConstants.EMPTY); for (int count = 0; count < source.size(); count++) { JsonElement granularSource = getGranularSource(source.get(count), turn, deps, aces); if (count != 0) { stringBuilder.append(separator); }// www . j ava 2 s . c o m stringBuilder.append(StringUtils.strip(granularSource.toString(), MystiqueConstants.DOUBLE_QUOTES)); } } return new JsonPrimitive(stringBuilder.toString()); }
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * Null safe toString method for a json element. * * @param source the element/*from ww w. j a v a 2 s. c o m*/ * @return the json string */ public String toString(JsonElement source) { return null != source ? source.toString() : null; }
From source file:com.bicitools.ws.ConsumeServicios.java
public static RespuestaJson consumeTiempoDist(Object st) { RespuestaNuevoJson out = new RespuestaNuevoJson(); RespuestaJson output = new RespuestaJson(); try {//from ww w. ja v a2 s .c o m ClientConfig clientConfig = new DefaultClientConfig(); clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); Client client = Client.create(clientConfig); WebResource webResource = client.resource( "http://localhost:8080/bicitoolsRU/webresources/myresource/ObtenerDistanciaTiempoRuta"); ClientResponse response = webResource.accept("application/json").type("application/json") .post(ClientResponse.class, st); if (response.getStatus() != 200) { output = ConstruyeRespuesta.construyeRespuestaFalla("Fallo 200"); } String json = response.getEntity(String.class); Gson gson = new Gson(); JsonParser jParser = new JsonParser(); JsonObject jObject = (JsonObject) jParser.parse(json); JsonElement elem = jObject.get("codigo"); JsonElement elem2 = jObject.get("valor"); JsonElement elem3 = jObject.get("data"); JsonElement elem4 = jObject.get("descripcion"); JsonObject jObjectInterno = (JsonObject) jParser.parse(elem3.toString()); JsonElement elem5 = jObjectInterno.get("tiempo"); JsonElement elem6 = jObjectInterno.get("distancia"); TiempoDistanciaInfo t = new TiempoDistanciaInfo(elem5.toString(), elem6.toString()); ArrayList datos = new ArrayList(); //RespuestaNuevoJson info = gson.fromJson(json, RespuestaNuevoJson.class); //System.out.println(""); output.setCodigo(Integer.parseInt(elem.toString())); output.setValor(elem2.toString()); output.setDescripcion(elem4.toString()); datos.add(t); output.setDatos(datos); output.setDescripcion(out.getDescripcion()); } catch (Exception e) { output = ConstruyeRespuesta.construyeRespuestaFalla(e.getMessage()); e.printStackTrace(); } return output; }
From source file:com.bing.maps.rest.services.impl.BaseBingMapsApiQuery.java
License:Apache License
@Override public List<T> list() { InputStream jsonContent = null; try {//www.ja va2 s. co m jsonContent = callApiGet(apiUrlBuilder.buildUrl()); JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET)); if (response.isJsonObject()) { List<T> responseList = unmarshallList(response.getAsJsonObject()); notifyObservers(responseList); return responseList; } throw new BingMapsException("Unknown content found in response:" + response.toString()); } catch (Exception e) { throw new BingMapsException(e); } finally { closeStream(jsonContent); } }
From source file:com.blackducksoftware.integration.hub.detect.detector.packagist.PackagistParser.java
License:Apache License
private NameVersion parseNameVersionFromJson(final JsonObject json) { final JsonElement nameElement = json.get("name"); final JsonElement versionElement = json.get("version"); String name = null;//from ww w. j a v a 2 s .c o m String version = null; if (nameElement != null) { name = nameElement.toString().replace("\"", ""); } if (versionElement != null) { version = versionElement.toString().replace("\"", ""); } return new NameVersion(name, version); }
From source file:com.bynder.sdk.util.BooleanTypeAdapter.java
License:Open Source License
/** * Check {@link JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext)} for * more information./*from w ww.ja v a 2 s .com*/ */ @Override public Boolean deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) { if (Arrays.asList(Boolean.TRUE.toString(), Boolean.FALSE.toString()).contains(json.toString())) { return json.getAsBoolean(); } else { try { int code = json.getAsInt(); if (code == 0) { return false; } else if (code == 1) { return true; } else { return null; } } catch (NumberFormatException e) { return null; } } }
From source file:com.bzcentre.dapiPush.MeetingPayload.java
License:Open Source License
public MeetingPayload deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { NginxClojureRT.log//from ww w. ja v a 2s .c o m .debug(TAG + "Deserialize json to type:" + typeOfT.getTypeName() + "=>" + json.toString()); JsonObject jsonObject = json.getAsJsonObject(); extractAps(jsonObject); return this; }
From source file:com.ccc.crest.core.cache.crest.Paging.java
License:Open Source License
protected void pagingDeserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator(); while (objectIter.hasNext()) { Entry<String, JsonElement> objectEntry = objectIter.next(); String key = objectEntry.getKey(); JsonElement value = objectEntry.getValue(); if (pagingDeserialize(key, value)) continue; if (ItemsKey.equals(key)) { JsonElement objectElement = objectEntry.getValue(); if (!objectElement.isJsonArray()) throw new JsonParseException( "Expected " + ItemsKey + " array received json element " + objectElement.toString()); int size = ((JsonArray) objectElement).size(); for (int i = 0; i < size; i++) { JsonElement childElement = ((JsonArray) objectElement).get(i); items.add(getPagedItem(childElement, typeOfT, context)); }//from w w w.j a va 2 s. c o m } else LoggerFactory.getLogger(getClass()) .warn(key + " has a field not currently being handled: \n" + objectEntry.toString()); } }
From source file:com.ccc.crest.core.cache.crest.schema.endpoint.EndpointCollection.java
License:Open Source License
@Override public EndpointCollection deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject topObj = (JsonObject) json; Set<Entry<String, JsonElement>> topSet = topObj.entrySet(); Iterator<Entry<String, JsonElement>> topIter = topSet.iterator(); do {// w w w . j a va 2 s. c o m if (!topIter.hasNext()) break; Entry<String, JsonElement> topEntry = topIter.next(); String topKey = topEntry.getKey(); JsonElement topElement = topEntry.getValue(); if (topKey.equals(UserCountKey)) { userCount = Long.parseLong(topElement.getAsString()); continue; } if (topKey.equals(UserCountStrKey)) continue; if (topKey.equals(ServerVersionKey)) { serverVersion = topElement.getAsString(); continue; } if (topKey.equals(ServerNameKey)) { serverName = topElement.getAsString(); continue; } if (topKey.equals(ServerStatusKey)) { serviceStatus = topElement.getAsString(); continue; } // if its not a top object level known variable from above list, it must be a group object if (topElement.isJsonPrimitive()) { log.warn("unexpected key: " + topKey + " = " + topObj.toString()); continue; } if (!topElement.isJsonObject()) { log.warn("expected an object: " + topKey + " = " + topObj.toString()); continue; } // first pass you should have a group in the topElement String groupName = topKey; EndpointGroup endpointGroup = new EndpointGroup(groupName); callGroups.add(endpointGroup); Set<Entry<String, JsonElement>> groupSet = topElement.getAsJsonObject().entrySet(); Iterator<Entry<String, JsonElement>> groupIter = groupSet.iterator(); do { if (!groupIter.hasNext()) break; Entry<String, JsonElement> groupEntry = groupIter.next(); // expecting a primitive href here String endpointName = groupEntry.getKey(); JsonElement hrefElement = groupEntry.getValue(); if (hrefElement.isJsonObject()) { JsonObject groupChildObj = (JsonObject) hrefElement; Set<Entry<String, JsonElement>> groupChildSet = groupChildObj.entrySet(); Iterator<Entry<String, JsonElement>> groupChildIter = groupChildSet.iterator(); if (!groupChildIter.hasNext()) break; Entry<String, JsonElement> groupChildEntry = groupChildIter.next(); String groupChildKey = groupChildEntry.getKey(); JsonElement groupChildElement = groupChildEntry.getValue(); endpointGroup.addEndpoint(new CrestEndpoint(endpointName, groupChildElement.getAsString())); continue; } // expect an object with href in it if (!hrefElement.isJsonPrimitive()) { log.warn("expected a primitive after group: " + groupName + " = " + hrefElement.toString()); continue; } endpointGroup.addEndpoint(new CrestEndpoint(endpointName, hrefElement.getAsString())); break; } while (true); } while (true); return this; }
From source file:com.ccc.crest.core.cache.crest.schema.option.Representations.java
License:Open Source License
@Override public Representations deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator(); while (objectIter.hasNext()) { Entry<String, JsonElement> objectEntry = objectIter.next(); String key = objectEntry.getKey(); JsonElement value = objectEntry.getValue(); if (RepresentationsKey.equals(key)) { JsonElement objectElement = objectEntry.getValue(); if (!objectElement.isJsonArray()) throw new JsonParseException("Expected " + RepresentationsKey + " array received json element " + objectElement.toString()); int size = ((JsonArray) objectElement).size(); for (int i = 0; i < size; i++) { JsonElement childElement = ((JsonArray) objectElement).get(i); Representation representation = new Representation(); representations.add(representation); representation.deserialize(childElement, typeOfT, context); }/*from w ww . j a v a2 s . c om*/ } else LoggerFactory.getLogger(getClass()) .warn(key + " has a field not currently being handled: \n" + objectEntry.toString()); } return this; }