List of usage examples for com.google.gson JsonElement getAsJsonPrimitive
public JsonPrimitive getAsJsonPrimitive()
From source file:com.luan.thermospy.android.core.rest.GetLogSessionListReq.java
License:Open Source License
@Override public void onOkResponse(JSONArray response) { List<LogSession> logSessionsList = new ArrayList<LogSession>(); // Creates the json object which will manage the information received 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 w w . j av a2 s . c om }); Gson gson = builder.create(); try { for (int i = 0; i < response.length(); i++) { logSessionsList.add(gson.fromJson(response.getJSONObject(i).toString(), LogSession.class)); } mListener.onLogSessionsRecv(logSessionsList); } catch (JSONException ex) { mListener.onLogSessionsError(); } }
From source file:com.luan.thermospy.android.core.rest.GetLogSessionReq.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()); }// w w w . j a v a 2s .c o m }); Gson gson = builder.create(); mListener.onLogSessionRecv(gson.fromJson(response.toString(), LogSession.class)); }
From source file:com.luan.thermospy.android.core.rest.GetTemperatureEntryListReq.java
License:Open Source License
@Override public void onOkResponse(JSONArray response) { List<TemperatureEntry> logSessionsList = new ArrayList<TemperatureEntry>(); 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 www .jav a 2 s. c o m*/ }); Gson gson = builder.create(); try { for (int i = 0; i < response.length(); i++) { logSessionsList.add(gson.fromJson(response.getJSONObject(i).toString(), TemperatureEntry.class)); } mListener.onTemperatureEntryRecv(logSessionsList); } catch (JSONException ex) { mListener.onTemperatureEntryError(); } }
From source file:com.luan.thermospy.android.core.rest.StartLogSessionReq.java
License:Open Source License
private JSONObject getJsonObject() { List<LogSession> logSessionsList = new ArrayList<LogSession>(); // Creates the json object which will manage the information received 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 ww w . j a v a 2 s . c o m }); Gson gson = builder.create(); try { return new JSONObject(gson.toJson(mLogSession, LogSession.class)); } catch (JSONException | JsonIOException e) { return null; } }
From source file:com.luan.thermospy.android.core.rest.StartLogSessionReq.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 ww w .j a v a 2 s.co m }); Gson gson = builder.create(); try { LogSession logSession = gson.fromJson(response.toString(), LogSession.class); mListener.onStartLogSessionRecv(logSession); } catch (JsonSyntaxException ex) { mListener.onStartLogSessionError(); } }
From source file:com.luan.thermospy.android.core.rest.StopLogSessionReq.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 ww w . j a v a 2 s.c o m*/ }); Gson gson = builder.create(); try { LogSession logSession = gson.fromJson(response.toString(), LogSession.class); mListener.onStopLogSessionRecv(logSession); } catch (JsonSyntaxException ex) { mListener.onStopLogSessionError(); } }
From source file:com.make.json2java.ClassField.java
License:Apache License
/** * Based on the seen json values, infer a type for this field. * Strings are mapped to String. numbers are preferred mapped to ints, then longs and finally as doubles. *//*from ww w . j av a 2 s . c o m*/ private InferredType inferType(Iterable<JsonElement> jsonValues, String type, boolean isArrayType) { if (mappedType) return new InferredType(type, isArrayType); InferredType inferredType = new InferredType(type, isArrayType); for (JsonElement jsonValue : jsonValues) { if (jsonValue instanceof JsonPrimitive) { JsonPrimitive primitive = jsonValue.getAsJsonPrimitive(); if (isBooleanValue(primitive)) { inferredType = new InferredType("boolean", false); } else if (primitive.isString()) { inferredType = new InferredType("String", false); } else if (primitive.isNumber()) { double number = primitive.getAsDouble(); boolean isWholeNumber = number - Math.ceil(number) == 0; if (isWholeNumber) { // int is preferred over long so look for that long longValue = (long) number; boolean isLargerThanInt = longValue > Integer.MAX_VALUE || longValue < Integer.MIN_VALUE; if (isLargerThanInt && !inferredType.type.equals("double")) { // some other value was a floating point inferredType = new InferredType("long", false); } else { // some other jsonValue was big enough to fit in long if (!inferredType.equals("long") && !inferredType.equals("double")) { inferredType = new InferredType("int", false); } } } else { // double is preferred over float inferredType = new InferredType("double", false); } } } else if (jsonValue instanceof JsonArray) { this.isArrayType = true; inferredType = new InferredType(inferType(jsonValue.getAsJsonArray(), type, false).type, true); } } return inferredType; }
From source file:com.microsoft.onenoteapi.service.DateTimeDeserializer.java
License:MIT License
public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return new DateTime(json.getAsJsonPrimitive().getAsString()); }
From source file:com.microsoft.windowsazure.mobileservices.MobileServiceJsonTable.java
License:Open Source License
/** * Validates if the id property is numeric. * @param element/* w w w . j av a2s. c o m*/ * @return */ private boolean isValidTypeId(JsonElement element) { return element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber(); }
From source file:com.microsoft.windowsazure.mobileservices.table.MobileServiceTableBase.java
License:Open Source License
/** * Validates if the object represents a string value. * * @param o/*from ww w.j av a 2 s . c om*/ * @return */ protected boolean isStringType(Object o) { boolean result = (o instanceof String); if (o instanceof JsonElement) { JsonElement json = (JsonElement) o; if (json.isJsonPrimitive()) { JsonPrimitive primitive = json.getAsJsonPrimitive(); result = primitive.isString(); } } return result; }