List of usage examples for com.google.gson JsonElement getAsJsonPrimitive
public JsonPrimitive getAsJsonPrimitive()
From source file:com.iheart.quickio.client.QuickIo.java
License:Open Source License
private void move(final JsonElement json) { if (!json.isJsonPrimitive()) { return;/* w ww.ja v a 2s . c om*/ } JsonPrimitive pr = json.getAsJsonPrimitive(); if (!pr.isString()) { return; } this.reconnect(pr.getAsString()); }
From source file:com.ikanow.infinit.e.data_model.store.MongoDbUtil.java
License:Apache License
public static Object encodeUnknown(JsonElement from) { if (from.isJsonArray()) { // Array return encodeArray(from.getAsJsonArray()); } //TESTED/*w ww. j av a2s.co m*/ else if (from.isJsonObject()) { // Object JsonObject obj = from.getAsJsonObject(); // Check for OID/Date: if (1 == obj.entrySet().size()) { if (obj.has("$date")) { try { return _format.parse(obj.get("$date").getAsString()); } catch (ParseException e) { try { return _format2.parse(obj.get("$date").getAsString()); } catch (ParseException e2) { return null; } } } //TESTED else if (obj.has("$oid")) { return new ObjectId(obj.get("$oid").getAsString()); } //TESTED } return encode(obj); } //TESTED else if (from.isJsonPrimitive()) { // Primitive JsonPrimitive val = from.getAsJsonPrimitive(); if (val.isNumber()) { return val.getAsNumber(); } //TESTED else if (val.isBoolean()) { return val.getAsBoolean(); } //TESTED else if (val.isString()) { return val.getAsString(); } //TESTED } //TESTED return null; }
From source file:com.indragie.cmput301as1.DateJSONDeserializer.java
License:Open Source License
@Override public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonPrimitive()) { JsonPrimitive primitive = json.getAsJsonPrimitive(); if (primitive.isNumber()) { return new Date(primitive.getAsLong()); }/*from w ww . j a v a 2 s . c o m*/ } return null; }
From source file:com.indragie.cmput301as1.JSONHelpers.java
License:Open Source License
/** * Gets the value of the JSON element as a string if possible. * @param element The JSON element./*w w w .ja v a2 s . co m*/ * @return A string if the JSON element was a string, or * null otherwise. */ public static String getStringIfPossible(JsonElement element) { if (element == null) return null; if (element.isJsonPrimitive()) { JsonPrimitive primitive = element.getAsJsonPrimitive(); if (primitive.isString()) { return primitive.getAsString(); } } return null; }
From source file:com.indragie.cmput301as1.JSONHelpers.java
License:Open Source License
/** * Gets the value of the JSON element as a boolean if possible. * @param element The JSON element.// w w w . j a v a2 s .c om * @return A string if the JSON element was a boolean, or * false otherwise. */ public static boolean getBooleanIfPossible(JsonElement element) { if (element == null) return false; if (element.isJsonPrimitive()) { JsonPrimitive primitive = element.getAsJsonPrimitive(); if (primitive.isBoolean()) { return primitive.getAsBoolean(); } } return false; }
From source file:com.indragie.cmput301as1.JSONHelpers.java
License:Open Source License
/** * Gets the value of the JSON element as a long if possible. * @param element The JSON element./*from w ww . j a v a 2s .c om*/ * @return A string if the JSON element was a long, or * 0 otherwise. */ public static long getLongIfPossible(JsonElement element) { if (element == null) return 0; if (element.isJsonPrimitive()) { JsonPrimitive primitive = element.getAsJsonPrimitive(); if (primitive.isNumber()) { return primitive.getAsLong(); } } return 0; }
From source file:com.javacreed.examples.gson.part3.AuthorDeserializer.java
License:Apache License
@Override public Author deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { // Only the ID is available if (json.isJsonPrimitive()) { final JsonPrimitive primitive = json.getAsJsonPrimitive(); return getOrCreate(primitive.getAsInt()); }//from w w w. j a va2 s .c o m // The whole object is available if (json.isJsonObject()) { final JsonObject jsonObject = json.getAsJsonObject(); final Author author = getOrCreate(jsonObject.get("id").getAsInt()); author.setName(jsonObject.get("name").getAsString()); return author; } throw new JsonParseException("Unexpected JSON type: " + json.getClass().getSimpleName()); }
From source file:com.jayway.jsonpath.internal.spi.json.GsonJsonProvider.java
License:Apache License
public static Object unwrap(Object o) { if (o == null) { return null; }//w w w . j a v a 2 s . c o m if (!(o instanceof JsonElement)) { return o; } JsonElement e = (JsonElement) o; if (e.isJsonNull()) { return null; } else if (e.isJsonPrimitive()) { JsonPrimitive p = e.getAsJsonPrimitive(); if (p.isString()) { return p.getAsString(); } else if (p.isBoolean()) { return p.getAsBoolean(); } else if (p.isNumber()) { return unwrapNumber(p.getAsNumber()); } } return o; }
From source file:com.jayway.jsonpath.spi.json.GsonJsonProvider.java
License:Apache License
public Object unwrap(final Object o) { if (o == null) { return null; }//from w ww .j a v a 2 s .c o m if (!(o instanceof JsonElement)) { return o; } JsonElement e = (JsonElement) o; if (e.isJsonNull()) { return null; } else if (e.isJsonPrimitive()) { JsonPrimitive p = e.getAsJsonPrimitive(); if (p.isString()) { return p.getAsString(); } else if (p.isBoolean()) { return p.getAsBoolean(); } else if (p.isNumber()) { return unwrapNumber(p.getAsNumber()); } } return o; }
From source file:com.luan.thermospy.android.core.rest.GetActiveLogSessionReq.java
License:Open Source License
@Override public void onOkResponse(JSONObject response) { GsonBuilder builder = new GsonBuilder(); // Register an adapter to manage the date types as long values builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return new Date(json.getAsJsonPrimitive().getAsLong()); }/*from w ww. java 2 s. co m*/ }); Gson gson = builder.create(); mListener.onActiveLogSessionRecv(gson.fromJson(response.toString(), LogSession.class)); }