List of usage examples for com.google.gson JsonElement getAsLong
public long getAsLong()
From source file:com.ibm.streamsx.topology.generator.spl.OperatorGenerator.java
License:Open Source License
static void appendWindowPolicy(String policyName, JsonElement config, String timeUnit, StringBuilder sb) { switch (policyName) { case POLICY_COUNT: sb.append("count("); sb.append(config.getAsInt());//from ww w . ja v a 2 s .co m sb.append(")"); break; case POLICY_DELTA: break; case POLICY_NONE: break; case POLICY_PUNCTUATION: break; case POLICY_TIME: { TimeUnit unit = TimeUnit.valueOf(timeUnit.toString()); long time = config.getAsLong(); double secs; switch (unit) { case DAYS: case HOURS: case MINUTES: case SECONDS: secs = unit.toSeconds(time); break; case MILLISECONDS: secs = ((double) time) / 1000.0; break; case MICROSECONDS: secs = ((double) time) / 1000_000.0; break; case NANOSECONDS: secs = ((double) time) / 1000_000_000.0; break; default: throw new IllegalStateException(); } sb.append("time("); sb.append(secs); sb.append(")"); break; } default: break; } }
From source file:com.kyloth.serleena.synchronization.kylothcloud.TelemetryEntityDeserializer.java
License:Open Source License
@Override public TelemetryEntity deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { TelemetryEntity te = new TelemetryEntity(); te.events = new ArrayList<Long>(); JsonArray events = json.getAsJsonObject().get("events").getAsJsonArray(); for (JsonElement event : events) { te.events.add(event.getAsLong()); }// w w w . jav a 2 s .co m return te; }
From source file:com.master.aluca.fitnessmd.common.webserver.WebserverManager.java
License:Open Source License
private synchronized void parseStatisticsData() { if (mDB.getConnectedUser() != null) { InMemoryDocument userdoc = FitnessMDMeteor.getInstance().getDatabase().getCollection("users") .getDocument(mDB.getConnectedUser().getDocId()); if (userdoc != null) { Gson gson = new Gson(); String json = gson.toJson(userdoc.getField("pedometerData")); if (json != null) { JsonParser parser = new JsonParser(); JsonElement element = parser.parse(json); JsonArray arr = element.getAsJsonArray(); LinkedHashMap<Long, Integer> map = new LinkedHashMap<>(); LinkedHashMap<Long, Integer> stats = new LinkedHashMap<>(); long startOfCurrentDay = Constants.getStartOfCurrentDay(); int totalSteps = 0; long[] daysAgo = new long[7]; for (int k = 7; k >= 1; k--) { daysAgo[k - 1] = startOfCurrentDay - (k * 24 * 60 * 60 * 1000); }//from w w w .ja v a 2s. c om // data is separated by hour index. concatenate data per day //Log.d(LOG_TAG,"map.size : " + map.size() + " entrySet.size : " + map.entrySet().size()); for (int t = 0; t < arr.size(); t++) { JsonObject elem = arr.get(t).getAsJsonObject(); JsonElement dayElement = elem.get("day"); if (dayElement != null) { Integer stepsForDay = map.get(dayElement.getAsLong()); //Log.d(LOG_TAG,"day : " + dayElement.getAsLong() + " steps : " + elem.get("steps").getAsInt() + " stepsForDay : " + stepsForDay); if (stepsForDay != null) { int steps = map.get(elem.get("day").getAsLong()); steps += elem.get("steps").getAsInt(); map.put(elem.get("day").getAsLong(), steps); } else { map.put(elem.get("day").getAsLong(), elem.get("steps").getAsInt()); } } totalSteps += elem.get("steps").getAsInt(); } int maxSteps = -1; long dayForMaxSteps = -1; AtomicBoolean valueInStatsChanged = new AtomicBoolean(false); Log.d(LOG_TAG, "savedStats.entrySet.size : " + savedStats.entrySet().size()); for (Map.Entry<Long, Integer> entry : map.entrySet()) { for (int l = 0; l < daysAgo.length; l++) { //Log.d(LOG_TAG,entry.getKey() + " : " + entry.getValue() + " daysAgo : " + daysAgo[l]); if (entry.getKey().longValue() == daysAgo[l]) { if (savedStats.entrySet().size() == 0) { valueInStatsChanged.set(true); } if (savedStats.containsKey(entry.getKey())) { Log.d(LOG_TAG, entry.getKey() + " : " + entry.getValue() + " savedStats : " + savedStats.get(entry.getKey().longValue())); if (savedStats.get(entry.getKey().longValue()) != entry.getValue() .longValue()) { Log.d(LOG_TAG, "values not equal : " + entry.getValue() + " savedStats : " + savedStats.get(entry.getKey().longValue())); valueInStatsChanged.set(true); } } else { savedStats.put(entry.getKey(), entry.getValue()); } stats.put(entry.getKey(), entry.getValue()); } } if (entry.getValue() > maxSteps) { maxSteps = entry.getValue(); dayForMaxSteps = entry.getKey(); mDB.updateBestSteps(entry.getKey(), entry.getValue()); } } for (IStatsChanged callback : statsCallbackList) { callback.onTotalStepsChanged(totalSteps); callback.onAverageStepsChanged(totalSteps / (arr.size() / 8)); callback.onMaxStepsChanged(dayForMaxSteps, maxSteps); if (valueInStatsChanged.get()) { Log.d(LOG_TAG, "stats values CHANGED"); callback.onLast7DaysStats(stats); } else { Log.d(LOG_TAG, "stats values not changed"); } } } } } }
From source file:com.meltmedia.cadmium.cli.HistoryCommand.java
License:Apache License
/** * Retrieves the history of a Cadmium site. * /*from w w w. jav a 2 s. c o m*/ * @param siteUri The uri of a cadmium site. * @param limit The maximum number of history entries to retrieve or if set to -1 tells the site to retrieve all history. * @param filter If true filters out the non revertable history entries. * @param token The Github API token to pass to the Cadmium site for authentication. * * @return A list of {@link HistoryEntry} Objects that are populated with the history returned from the Cadmium site. * * @throws URISyntaxException * @throws IOException * @throws ClientProtocolException * @throws Exception */ public static List<HistoryEntry> getHistory(String siteUri, int limit, boolean filter, String token) throws URISyntaxException, IOException, ClientProtocolException, Exception { if (!siteUri.endsWith("/system/history")) { siteUri += "/system/history"; } List<HistoryEntry> history = null; HttpClient httpClient = httpClient(); HttpGet get = null; try { URIBuilder uriBuilder = new URIBuilder(siteUri); if (limit > 0) { uriBuilder.addParameter("limit", limit + ""); } if (filter) { uriBuilder.addParameter("filter", filter + ""); } URI uri = uriBuilder.build(); get = new HttpGet(uri); addAuthHeader(token, get); HttpResponse resp = httpClient.execute(get); if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = resp.getEntity(); if (entity.getContentType().getValue().equals("application/json")) { String responseContent = EntityUtils.toString(entity); Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { @Override public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx) throws JsonParseException { return new Date(json.getAsLong()); } }).create(); history = gson.fromJson(responseContent, new TypeToken<List<HistoryEntry>>() { }.getType()); } else { System.err .println("Invalid response content type [" + entity.getContentType().getValue() + "]"); System.exit(1); } } else { System.err.println("Request failed due to a [" + resp.getStatusLine().getStatusCode() + ":" + resp.getStatusLine().getReasonPhrase() + "] response from the remote server."); System.exit(1); } } finally { if (get != null) { get.releaseConnection(); } } return history; }
From source file:com.microsoftopentechnologies.auth.utils.JsonUtils.java
License:Apache License
public static long getJsonLongProp(JsonObject obj, String propName) { JsonElement element = obj.get(propName); if (element != null) { return element.getAsLong(); }//from w w w.j a va2 s.co m return Long.MIN_VALUE; }
From source file:com.microsoftopentechnologies.intellij.helpers.aadauth.AuthenticationContext.java
License:Apache License
private long getJsonLongProp(JsonObject obj, String propName) { JsonElement element = obj.get(propName); if (element != null) { return element.getAsLong(); }// w w w.j a v a 2 s .c o m return Long.MIN_VALUE; }
From source file:com.nimbits.server.gson.deserializer.ValueDeserializer.java
License:Apache License
@Override public Value deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { Location location;//from ww w. j av a 2s. com JsonObject jsonObject = jsonElement.getAsJsonObject(); JsonElement valueElement = jsonObject.get("d"); JsonElement dataElement = jsonObject.get("dx"); JsonElement latElement = jsonObject.get("lt"); JsonElement lngElement = jsonObject.get("lg"); JsonElement timestampElement = jsonObject.get("t"); String data = dataElement == null ? null : dataElement.getAsString(); Double lat = latElement == null || latElement.isJsonNull() ? null : latElement.getAsDouble(); Double lng = lngElement == null || lngElement.isJsonNull() ? null : lngElement.getAsDouble(); Double value = valueElement == null || valueElement.isJsonNull() ? null : valueElement.getAsDouble(); Long timestamp = timestampElement == null || timestampElement.isJsonNull() ? 0 : timestampElement.getAsLong(); if (lat != null && lng != null) { location = LocationFactory.createLocation(lat, lng); } else { location = LocationFactory.createEmptyLocation(); } Date time = timestamp > 0 ? new Date(timestamp) : new Date(); ValueData valueData; if (data != null && data.length() > 0) { valueData = ValueDataModel.getInstance(SimpleValue.getInstance(data)); } else { valueData = ValueDataModel.getEmptyInstance(); } return ValueFactory.createValueModel(location, value, time, valueData, AlertType.OK); }
From source file:com.popdeem.sdk.core.deserializer.PDLongDeserializer.java
License:Open Source License
@Override public Long deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.getAsString().equalsIgnoreCase("")) { return (long) -1; } else {//from ww w . jav a 2s . co m return json.getAsLong(); } }
From source file:com.relayrides.pushy.apns.DateAsMillisecondsSinceEpochTypeAdapter.java
License:Open Source License
@Override public Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { final Date date; if (json.isJsonPrimitive()) { date = new Date(json.getAsLong()); } else if (json.isJsonNull()) { date = null;/*from ww w . j a v a2 s . c o m*/ } else { throw new JsonParseException( "Dates represented as seconds since the epoch must either be numbers or null."); } return date; }
From source file:com.relayrides.pushy.apns.DateAsSecondsSinceEpochTypeAdapter.java
License:Open Source License
@Override public Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { final Date date; if (json.isJsonPrimitive()) { date = new Date(json.getAsLong() * 1000); } else if (json.isJsonNull()) { date = null;//from w w w.j ava2 s . co m } else { throw new JsonParseException( "Dates represented as seconds since the epoch must either be numbers or null."); } return date; }