List of usage examples for com.google.gson JsonElement toString
@Override
public String toString()
From source file:br.com.bean.RestControllers.lookupController.java
@RequestMapping("nivel-acesso") @ResponseBody//from w w w .jav a 2s. c o m public String nivelAcesso() { Map<Integer, String> niveisAcesso = new HashMap<Integer, String>(); niveisAcesso.put(1, "Administrador"); niveisAcesso.put(2, "Aluno"); niveisAcesso.put(3, "Faculdade"); niveisAcesso.put(4, "Representante"); niveisAcesso.put(5, "Dependente"); JsonElement jsonNiveis = new Gson().toJsonTree(niveisAcesso); return jsonNiveis.toString(); }
From source file:br.com.bean.RestControllers.lookupController.java
@RequestMapping("vooComEscala") @ResponseBody//from w w w. j av a 2s. co m public String vooComEscala() { Map<Integer, String> tiposVoo = new HashMap<Integer, String>(); tiposVoo.put(1, "Com Escala"); tiposVoo.put(2, "Sem Escala"); JsonElement jsonTipos = new Gson().toJsonTree(tiposVoo); return jsonTipos.toString(); }
From source file:br.com.bean.RestControllers.lookupController.java
@RequestMapping("grauParentesco") @ResponseBody//from w w w .j av a 2s .com public String grauParentesco() { Map<Integer, String> grausParentesco = new HashMap<Integer, String>(); grausParentesco.put(1, "Me"); grausParentesco.put(2, "Pai"); grausParentesco.put(3, "Tio(a)"); grausParentesco.put(4, "Irm/Irmo"); grausParentesco.put(5, "Primo"); grausParentesco.put(6, "V/V"); grausParentesco.put(7, "Outro"); JsonElement grausDeParentesco = new Gson().toJsonTree(grausParentesco); return grausDeParentesco.toString(); }
From source file:br.com.bean.RestControllers.lookupController.java
@RequestMapping("tiposEvento") @ResponseBody//ww w .ja v a 2 s .c o m public String tiposEvento() { Map<Integer, String> tiposEvento = new HashMap<Integer, String>(); tiposEvento.put(1, "Formatura"); tiposEvento.put(2, "Aventura"); tiposEvento.put(3, "Debutante"); JsonElement jsonEventos = new Gson().toJsonTree(tiposEvento); return jsonEventos.toString(); }
From source file:br.com.bean.RestControllers.lookupController.java
@RequestMapping("bandeiras") @ResponseBody/*from ww w .j a v a2 s . co m*/ public String bandeiras() { Map<Integer, String> tiposBandeira = new HashMap<Integer, String>(); tiposBandeira.put(1, "MasterCard"); tiposBandeira.put(2, "Visa"); tiposBandeira.put(3, "DinnersClub"); tiposBandeira.put(4, "OroCred"); JsonElement jsonBandeiras = new Gson().toJsonTree(tiposBandeira); return jsonBandeiras.toString(); }
From source file:br.com.thiaguten.contrib.JsonContrib.java
License:Apache License
private static String getJsonElementAsString(JsonElement jsonElement) { if (jsonElement == null) { return JsonNull.INSTANCE.toString(); }/*from w w w. j a v a 2 s.co m*/ if (jsonElement.isJsonPrimitive()) { return jsonElement.getAsString(); } return jsonElement.toString(); }
From source file:br.org.cesar.knot.lib.connection.ThingApi.java
License:Open Source License
/** * Get all information regarding the device. * * @param clazz The class for this device. Meshblu works with any type of objects and * it is necessary deserialize the return to a valid object. * Note: The class parameter should be a extension of {@link AbstractThingDevice} * @return an json element containing device informations * @throws KnotException KnotException/*from w w w. j a v a 2 s .c o m*/ */ public <T extends AbstractThingDevice> T whoAmI(Class<T> clazz) throws InvalidDeviceOwnerStateException, KnotException { // Check if the current state of device owner is valid if (!isValidDeviceOwner()) { throw new InvalidDeviceOwnerStateException("The device owner is invalid or null"); } // Get all information regarding the given device final String endPoint = mEndPoint + WHOAMI; Request request = generateBasicRequestBuild(this.abstractDeviceOwner.getUuid(), this.abstractDeviceOwner.getToken(), endPoint).build(); try { Response response = mHttpClient.newCall(request).execute(); JsonElement jsonElement = new JsonParser().parse(response.body().string()); return mGson.fromJson(jsonElement.toString(), clazz); } catch (Exception e) { throw new KnotException(e); } }
From source file:br.org.cesar.knot.lib.connection.ThingApi.java
License:Open Source License
/** * Send a message in Meshblu instance//from w w w . java2 s . c o m * * @param message model sample to create a new message. Basically this message model * contains attributes that will be send into Meshblu. * @return New message with meshblu content. * @throws KnotException KnotException * @see AbstractThingMessage */ public <T extends AbstractThingMessage> T sendMessage(T message) throws KnotException, InvalidDeviceOwnerStateException { // Check if the current state of device owner is valid if (!isValidDeviceOwner()) { throw new InvalidDeviceOwnerStateException("The device owner is invalid or null"); } final String endPoint = mEndPoint + MESSAGE; String json = mGson.toJson(message); RequestBody body = createRequestBodyWith(json); Request request = generateBasicRequestBuild(this.abstractDeviceOwner.getUuid(), this.abstractDeviceOwner.getToken(), endPoint).post(body).build(); try { Response response = mHttpClient.newCall(request).execute(); JsonElement jsonElement = new JsonParser().parse(response.body().string()); return (T) mGson.fromJson(jsonElement.toString(), message.getClass()); } catch (Exception e) { throw new KnotException(e); } }
From source file:brooklyn.event.feed.http.JsonFunctions.java
License:Apache License
/** * returns an element from a single json primitive value given a full path {@link com.jayway.jsonpath.JsonPath} *//*from w w w . j a v a 2 s .c o m*/ public static <T> Function<JsonElement, T> getPath(final String path) { return new Function<JsonElement, T>() { @SuppressWarnings("unchecked") @Override public T apply(JsonElement input) { String jsonString = input.toString(); Object rawElement = JsonPath.read(jsonString, path); return (T) rawElement; } }; }
From source file:brooklyn.networking.cloudstack.CloudstackNew40FeaturesClient.java
License:Apache License
protected String waitForJobCompletion(int statusCode, InputStream payload, String message) throws InterruptedException { if (statusCode < 200 || statusCode >= 300) { String payloadStr = null; try {//from ww w. j a v a 2 s . c o m payloadStr = Streams.readFullyString(payload); } catch (Exception e) { Exceptions.propagateIfFatal(e); LOG.debug("On HttpResponse failure, failed to get string payload; continuing with reporting error", e); } throw new RuntimeException( "Error " + statusCode + ": " + message + (payloadStr != null ? "; " + payloadStr : "")); } JsonElement jr = json(payload); LOG.debug(pretty(jr)); String responseId; String jobId; try { JsonObject jobfields = jr.getAsJsonObject().entrySet().iterator().next().getValue().getAsJsonObject(); JsonElement responseIdJson = jobfields.get("id"); responseId = responseIdJson != null ? responseIdJson.getAsString() : null; jobId = jobfields.get("jobid").getAsString(); } catch (NullPointerException | NoSuchElementException | IllegalStateException e) { // TODO Not good using exceptions for normal control flow; but easiest way to handle // problems in unexpected json structure. throw new IllegalStateException("Problem parsing job response: " + jr.toString()); } do { AsyncJob<Object> job = getAsyncJobClient().getAsyncJob(jobId); LOG.debug("waiting: " + job); if (job.hasFailed()) throw new IllegalStateException("Failed job: " + job); if (job.hasSucceed()) { Status status = job.getStatus(); if (Status.FAILED.equals(status)) throw new IllegalStateException("Failed job: " + job); if (Status.SUCCEEDED.equals(status)) return responseId; } Thread.sleep(1000); } while (true); }