/** * Returns the <code>long</code> value of the member with the specified name in this object. If * this object does not contain a member with this name, the given default value is returned. If * this object contains multiple members with the given name, the last one will be picked. If this * member's value does not represent a JSON number or if it cannot be interpreted as Java * <code>long</code>, an exception is thrown. * * @param name * the name of the member whose value is to be returned * @param defaultValue * the value to be returned if the requested member is missing * @return the value of the last member with the specified name, or the given default value if * this object does not contain a member with that name */ public long getLong(String name, long defaultValue) { JsonValue value = get(name); return value != null ? value.asLong() : defaultValue; }
@Override public Long read(JsonValue jsonValue) throws Exception { return jsonValue.asLong(); }
@Override public long asLong() { return wrapped.asLong(); }
@Override protected Long getValue(JsonValue jsonValue) throws Exception { return jsonValue.asLong(); }
public static Long getLong(JsonValue value, String name) { JsonValue result = ((JsonObject) value).get(name); if (result == null) { return null; } return result.asLong(); }
/** * Parse a given JsonValue to a long regardless of whether that value is a String or a long. * * @param value a JsonValue to parse to a long. * @return a long representation of the given value. Can throw a runtime exception (ParseException, UnsupportedOperationException, or NumberFormatException). */ public static long parseJsonValueToLong(JsonValue value) { try { return value.asLong(); } catch (UnsupportedOperationException e) { String s = value.asString().replace("\"", ""); return Long.parseLong(s); } }
public void setSkypeToken(String skypeToken) { this.skypeToken = skypeToken; String[] data = skypeToken.split("\\."); JsonObject object = JsonObject.readFrom( new String(Base64.getDecoder().decode(data[1]), StandardCharsets.UTF_8)); this.skypeTokenExpiryTime = object.get("exp").asLong() * 1000; }
/** * Returns the <code>long</code> value of the member with the specified name in this object. If * this object does not contain a member with this name, the given default value is returned. If * this object contains multiple members with the given name, the last one will be picked. If this * member's value does not represent a JSON number or if it cannot be interpreted as Java * <code>long</code>, an exception is thrown. * * @param name * the name of the member whose value is to be returned * @param defaultValue * the value to be returned if the requested member is missing * @return the value of the last member with the specified name, or the given default value if * this object does not contain a member with that name */ public long getLong(String name, long defaultValue) { JsonValue value = get(name); return value != null ? value.asLong() : defaultValue; }
/** * Returns the <code>long</code> value of the member with the specified name in this object. If * this object does not contain a member with this name, the given default value is returned. If * this object contains multiple members with the given name, the last one will be picked. If this * member's value does not represent a JSON number or if it cannot be interpreted as Java * <code>long</code>, an exception is thrown. * * @param name * the name of the member whose value is to be returned * @param defaultValue * the value to be returned if the requested member is missing * @return the value of the last member with the specified name, or the given default value if * this object does not contain a member with that name */ public long getLong(String name, long defaultValue) { JsonValue value = get(name); return value != null ? value.asLong() : defaultValue; }
public Long getAsLong(final String field){ JsonValue value = getAsJsonValue(field); if (value == null || value.isNull()) { return null; } return value.asLong(); }
public static Object deserialize(JsonValue v, Type type) { switch (type) { case INTEGER: return v.asLong(); case FLOAT: return v.asDouble(); case BOOLEAN: return v.asBoolean(); case OPAQUE: return Base64.decodeBase64(v.asString()); case STRING: return v.asString(); case TIME: return new Date(v.asLong()); default: throw new IllegalArgumentException(String.format("Type %s is not supported", type.name())); } } }
public static Object deserialize(JsonValue v, Type type) { switch (type) { case INTEGER: return v.asLong(); case FLOAT: return v.asDouble(); case BOOLEAN: return v.asBoolean(); case OPAQUE: return Base64.decodeBase64(v.asString()); case STRING: return v.asString(); case TIME: return new Date(v.asLong()); default: throw new IllegalArgumentException(String.format("Type %s is not supported", type.name())); } } }
/** * Starts this EventStream and begins long polling the API. * @throws IllegalStateException if the EventStream is already started. */ public void start() { if (this.started) { throw new IllegalStateException("Cannot start the EventStream because it isn't stopped."); } final long initialPosition; if (this.startingPosition == STREAM_POSITION_NOW) { BoxAPIRequest request = new BoxAPIRequest(this.api, EVENT_URL.build(this.api.getBaseURL(), "now"), "GET"); BoxJSONResponse response = (BoxJSONResponse) request.send(); JsonObject jsonObject = JsonObject.readFrom(response.getJSON()); initialPosition = jsonObject.get("next_stream_position").asLong(); } else { assert this.startingPosition >= 0 : "Starting position must be non-negative"; initialPosition = this.startingPosition; } this.poller = new Poller(initialPosition); this.pollerThread = new Thread(this.poller); this.pollerThread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { public void uncaughtException(Thread t, Throwable e) { EventStream.this.notifyException(e); } }); this.pollerThread.start(); this.started = true; }
/** * Authenticates the API connection by obtaining access and refresh tokens using the auth code that was obtained * from the first half of OAuth. * @param authCode the auth code obtained from the first half of the OAuth process. */ public void authenticate(String authCode) { URL url = null; try { url = new URL(this.tokenURL); } catch (MalformedURLException e) { assert false : "An invalid token URL indicates a bug in the SDK."; throw new RuntimeException("An invalid token URL indicates a bug in the SDK.", e); } String urlParameters = String.format("grant_type=authorization_code&code=%s&client_id=%s&client_secret=%s", authCode, this.clientID, this.clientSecret); BoxAPIRequest request = new BoxAPIRequest(this, url, "POST"); request.shouldAuthenticate(false); request.setBody(urlParameters); BoxJSONResponse response = (BoxJSONResponse) request.send(); String json = response.getJSON(); JsonObject jsonObject = JsonObject.readFrom(json); this.accessToken = jsonObject.get("access_token").asString(); this.refreshToken = jsonObject.get("refresh_token").asString(); this.lastRefresh = System.currentTimeMillis(); this.expires = jsonObject.get("expires_in").asLong() * 1000; }
private void tryRestoreUsingAccessTokenCache() { if (this.accessTokenCache == null) { //no cache specified so force authentication this.authenticate(); } else { String cachedTokenInfo = this.accessTokenCache.get(this.getAccessTokenCacheKey()); if (cachedTokenInfo == null) { //not found; probably first time for this client config so authenticate; info will then be cached this.authenticate(); } else { //pull access token cache info; authentication will occur as needed (if token is expired) JsonObject json = JsonObject.readFrom(cachedTokenInfo); this.setAccessToken(json.get("accessToken").asString()); this.setLastRefresh(json.get("lastRefresh").asLong()); this.setExpires(json.get("expires").asLong()); } } }
private void init(ForecastIO fio){ JsonValue jsonvalue; if(fio.hasAlerts()){ for(int i=0; i < NumberOfAlerts(); i++ ){ alert[i] = new Alert(); jsonvalue = fio.getAlerts().get(i); alert[i].setTitle( jsonvalue.asObject().get("title").asString() ); alert[i].setTime( jsonvalue.asObject().get("time").asLong() ); alert[i].setExpire( jsonvalue.asObject().get("expires").asLong() ); alert[i].setDescription( jsonvalue.asObject().get("description").asString() ); alert[i].setUri( jsonvalue.asObject().get("uri").asString() ); } } }
@Override public JsonRootObject deserialize(JsonObject o) { if (o == null) return null; JsonRootObject jro = new JsonRootObject(); JsonValue e = o.get("e"); if (e != null) jro.setResourceList(serDes.deserialize(e.asArray())); JsonValue bn = o.get("bn"); if (bn != null && bn.isString()) jro.setBaseName(bn.asString()); JsonValue bt = o.get("bt"); if (bt != null && bt.isNumber()) jro.setBaseTime(bt.asLong()); return jro; } }
/** * Restores a saved connection state into this BoxAPIConnection. * * @see #save * @param state the saved state that was created with {@link #save}. */ public void restore(String state) { JsonObject json = JsonObject.readFrom(state); String accessToken = json.get("accessToken").asString(); String refreshToken = json.get("refreshToken").asString(); long lastRefresh = json.get("lastRefresh").asLong(); long expires = json.get("expires").asLong(); String userAgent = json.get("userAgent").asString(); String tokenURL = json.get("tokenURL").asString(); String baseURL = json.get("baseURL").asString(); String baseUploadURL = json.get("baseUploadURL").asString(); boolean autoRefresh = json.get("autoRefresh").asBoolean(); int maxRequestAttempts = json.get("maxRequestAttempts").asInt(); this.accessToken = accessToken; this.refreshToken = refreshToken; this.lastRefresh = lastRefresh; this.expires = expires; this.userAgent = userAgent; this.tokenURL = tokenURL; this.baseURL = baseURL; this.baseUploadURL = baseUploadURL; this.autoRefresh = autoRefresh; this.maxRequestAttempts = maxRequestAttempts; }
jae.setTime(t.asLong());
public static ObservationFull fromJson(JsonObject meta) { ObservationRequest req = new ObservationRequest(); req.setId(meta.getString("id", null)); req.setStartTimeMillis(meta.getLong("start", -1L)); req.setEndTimeMillis(meta.getLong("end", -1L)); req.setOutputSampleRate(meta.getInt("sampleRate", -1)); req.setInputSampleRate(meta.getInt("inputSampleRate", -1)); req.setSatelliteFrequency(meta.getLong("frequency", -1)); req.setActualFrequency(meta.getLong("actualFrequency", -1)); req.setBandwidth(meta.getLong("bandwidth", -1)); req.setDecoder(meta.getString("decoder", null)); req.setSatelliteId(meta.getString("satellite", null)); ObservationResult result = new ObservationResult(); result.setGain(meta.getString("gain", null)); result.setChannelA(meta.getString("channelA", null)); result.setChannelB(meta.getString("channelB", null)); JsonValue decodedPackets = meta.get("numberOfDecodedPackets"); if (decodedPackets != null) { result.setNumberOfDecodedPackets(decodedPackets.asLong()); } result.setaURL(meta.getString("aURL", null)); result.setDataURL(meta.getString("data", null)); result.setSpectogramURL(meta.getString("spectogramURL", null)); ObservationFull full = new ObservationFull(req); full.setResult(result); return full; }