List of usage examples for com.google.gson JsonElement getAsJsonObject
public JsonObject getAsJsonObject()
From source file:com.axlight.gbrain.server.GBrainServiceImpl.java
License:Apache License
public void fetchNeuron() throws IOException { URLFetchService ufs = URLFetchServiceFactory.getURLFetchService(); JsonParser parser = new JsonParser(); NeuronData[] targets = getTopNeurons(); for (NeuronData n : targets) { String q = URLEncoder.encode(n.getContent(), "UTF-8"); long parent = n.getId(); int x = n.getX(); int y = n.getY(); int children = n.getChildren(); HTTPResponse res = ufs/*from w ww. j a v a2 s.c om*/ .fetch(new URL("http://search.twitter.com/search.json?q=" + q + "&result_type=recent")); JsonElement top = parser.parse(new String(res.getContent(), "UTF-8")); for (JsonElement ele : top.getAsJsonObject().get("results").getAsJsonArray()) { String content = ele.getAsJsonObject().get("text").getAsString(); if (content.indexOf("http://") == -1) { continue; } if (alreadyExists(content)) { continue; } try { double deg = Math.random() * Math.PI * 2; int xx = x + (int) (Math.random() * PARAM_NEW_X * Math.cos(deg)); int yy = y + (int) (Math.random() * PARAM_NEW_Y * Math.sin(deg)); addNeuron(parent, content, xx, yy); children++; } catch (IllegalArgumentException e) { // ignored } } } }
From source file:com.azure.webapi.JsonEntityParser.java
License:Open Source License
/** * Parses the JSON object to a typed list * //from w w w . ja va2 s .c om * @param results * JSON results * @param gson * Gson object used for parsing * @param clazz * Target entity class * @return List of entities */ public static <E> List<E> parseResults(JsonElement results, Gson gson, Class<E> clazz) { List<E> result = new ArrayList<E>(); String idPropertyName = getIdPropertyName(clazz); // Parse results if (results.isJsonArray()) // Query result { JsonArray elements = results.getAsJsonArray(); for (JsonElement element : elements) { changeIdPropertyName(element.getAsJsonObject(), idPropertyName); E typedElement = gson.fromJson(element, clazz); result.add(typedElement); } } else { // Lookup result if (results.isJsonObject()) { changeIdPropertyName(results.getAsJsonObject(), idPropertyName); } E typedElement = gson.fromJson(results, clazz); result.add(typedElement); } return result; }
From source file:com.azure.webapi.MobileServiceJsonTable.java
License:Open Source License
/** * Looks up a row in the table and retrieves its JSON value. * /* w w w. j a v a 2 s .c om*/ * @param id * The id of the row * @param parameters * A list of user-defined parameters and values to include in the request URI query string * @param callback * Callback to invoke after the operation is completed */ public void lookUp(Object id, List<Pair<String, String>> parameters, final TableJsonOperationCallback callback) { // Create request URL String url; try { Uri.Builder uriBuilder = Uri.parse(mClient.getAppUrl().toString()).buildUpon(); uriBuilder.path(TABLES_URL); uriBuilder.appendPath(URLEncoder.encode(mTableName, MobileServiceClient.UTF8_ENCODING)); uriBuilder.appendPath(URLEncoder.encode(id.toString(), MobileServiceClient.UTF8_ENCODING)); if (parameters != null && parameters.size() > 0) { for (Pair<String, String> parameter : parameters) { uriBuilder.appendQueryParameter(parameter.first, parameter.second); } } url = uriBuilder.build().toString(); } catch (UnsupportedEncodingException e) { if (callback != null) { callback.onCompleted(null, e, null); } return; } executeGetRecords(url, new TableJsonQueryCallback() { @Override public void onCompleted(JsonElement results, int count, Exception exception, ServiceFilterResponse response) { if (callback != null) { if (exception == null && results != null) { if (results.isJsonArray()) { // empty result callback.onCompleted(null, new MobileServiceException("A record with the specified Id cannot be found"), response); } else { // Lookup result callback.onCompleted(results.getAsJsonObject(), exception, response); } } else { callback.onCompleted(null, exception, response); } } } }); }
From source file:com.azure.webapi.MobileServiceJsonTable.java
License:Open Source License
/** * Executes the query against the table//ww w . ja va 2 s. com * * @param request * Request to execute * @param callback * Callback to invoke when the operation is completed */ private void executeTableOperation(ServiceFilterRequest request, final TableJsonOperationCallback callback) { // Create AsyncTask to execute the operation new RequestAsyncTask(request, mClient.createConnection()) { @Override protected void onPostExecute(ServiceFilterResponse result) { if (callback != null) { JsonObject newEntityJson = null; if (mTaskException == null && result != null) { String content = null; content = result.getContent(); // put and delete operations are not supposed to return entity for WEBAPI JsonElement element = new JsonParser().parse(content); if (element != null && element != JsonNull.INSTANCE) { newEntityJson = element.getAsJsonObject(); } callback.onCompleted(newEntityJson, null, result); } else { callback.onCompleted(null, mTaskException, result); } } } }.execute(); }
From source file:com.azure.webapi.MobileServiceJsonTable.java
License:Open Source License
/** * Retrieves a set of rows from using the specified URL * //from w w w.jav a 2 s . c o m * @param query * The URL used to retrieve the rows * @param callback * Callback to invoke when the operation is completed */ private void executeGetRecords(final String url, final TableJsonQueryCallback callback) { ServiceFilterRequest request = new ServiceFilterRequestImpl(new HttpGet(url)); MobileServiceConnection conn = mClient.createConnection(); // Create AsyncTask to execute the request and parse the results new RequestAsyncTask(request, conn) { @Override protected void onPostExecute(ServiceFilterResponse response) { if (callback != null) { if (mTaskException == null && response != null) { JsonElement results = null; int count = 0; try { // Parse the results using the given Entity class String content = response.getContent(); JsonElement json = new JsonParser().parse(content); if (json.isJsonObject()) { JsonObject jsonObject = json.getAsJsonObject(); // If the response has count property, store its // value if (jsonObject.has("results") && jsonObject.has("count")) { // inlinecount // result count = jsonObject.get("count").getAsInt(); results = jsonObject.get("results"); } else { results = json; } } else { results = json; } } catch (Exception e) { callback.onCompleted(null, 0, new MobileServiceException("Error while retrieving data from response.", e), response); return; } callback.onCompleted(results, count, null, response); } else { callback.onCompleted(null, 0, mTaskException, response); } } } }.execute(); }
From source file:com.b12kab.tmdblibrary.AccountStateDeserializer.java
License:Apache License
@Override public AccountState deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { AccountState accountState = new AccountState(); String convertVariable = ""; JsonObject obj = (JsonObject) json;/* w ww . jav a2 s.c o m*/ JsonElement element; JsonElement ratingElement; JsonPrimitive rating; try { convertVariable = "id"; element = obj.get("id"); if (element != null) { if (element.isJsonPrimitive()) { if (element.getAsJsonPrimitive().isNumber()) { accountState.setId(element.getAsInt()); } } } convertVariable = "favorite"; element = obj.get("favorite"); if (element != null) { if (element.isJsonPrimitive()) { if (element.getAsJsonPrimitive().isBoolean()) { accountState.setFavorite(element.getAsBoolean()); } } } accountState.setRated(null); convertVariable = "rated"; element = obj.get("rated"); if (element != null) { if (element.isJsonObject()) { if (element.getAsJsonObject().has("value")) { ratingElement = element.getAsJsonObject().get("value"); if (ratingElement.isJsonPrimitive()) { // rating if (ratingElement.getAsJsonPrimitive().isNumber()) { accountState.setRated(ratingElement.getAsFloat()); } } } } } convertVariable = "watchlist"; element = obj.get("watchlist"); if (element != null) { if (element.isJsonPrimitive()) { if (element.getAsJsonPrimitive().isBoolean()) { accountState.setWatchlist(element.getAsBoolean()); } } } } catch (NullPointerException npe) { Log.e(TAG, "Processing " + convertVariable + " response from Tmdb, NullPointerException occurred" + npe.toString()); return null; } catch (NumberFormatException npe) { Log.e(TAG, "Processing " + convertVariable + " response from Tmdb, NumberFormatException occurred" + npe.toString()); return null; } catch (IllegalStateException ise) { Log.e(TAG, "Processing " + convertVariable + " response from Tmdb, IllegalStateException occurred" + ise.toString()); } return accountState; }
From source file:com.baidu.rigel.biplatform.ac.util.deserialize.DataSourceInfoDeserialize.java
License:Open Source License
@Override public DataSourceInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonObject()) { DataSourceType dataSourceType = context.deserialize(json.getAsJsonObject().get("dataSourceType"), DataSourceType.class); if (dataSourceType.equals(DataSourceType.SQL)) { return context.deserialize(json, SqlDataSourceInfo.class); }/*from w ww . j a v a2 s . c o m*/ } return null; }
From source file:com.baidu.rigel.biplatform.ac.util.deserialize.DimensionDeserialize.java
License:Open Source License
@Override public Dimension deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { Dimension dimension = null;/*from www . j a v a2 s. co m*/ if (json.isJsonObject()) { JsonObject dimensionObject = json.getAsJsonObject(); if (dimensionObject.get("type") != null) { DimensionType dimensionType = DimensionType.valueOf(dimensionObject.get("type").getAsString()); if (dimensionType.equals(DimensionType.TIME_DIMENSION)) { dimension = context.deserialize(json, TimeDimension.class); } else { dimension = context.deserialize(json, StandardDimension.class); } if (dimension != null && MapUtils.isNotEmpty(dimension.getLevels())) { for (Level level : dimension.getLevels().values()) { level.setDimension(dimension); } } } } return dimension; }
From source file:com.baidu.rigel.biplatform.ac.util.deserialize.LevelDeserialize.java
License:Open Source License
@Override public Level deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonObject()) { JsonObject levelJsonObj = json.getAsJsonObject(); if (levelJsonObj.get("type") != null) { LevelType levelType = LevelType.valueOf(levelJsonObj.get("type").getAsString()); if (levelType.equals(LevelType.CALL_BACK)) { return context.deserialize(levelJsonObj, CallbackLevel.class); } else if (levelType.name().equals(LevelType.USER_CUSTOM)) { return context.deserialize(levelJsonObj, UserCustomLevel.class); } else { return context.deserialize(levelJsonObj, MiniCubeLevel.class); }/* w ww.j a v a 2 s. co m*/ } } return null; }
From source file:com.baidu.rigel.biplatform.ac.util.deserialize.MeasureDeserialize.java
License:Open Source License
@Override public Measure deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonObject()) { JsonObject jsObj = json.getAsJsonObject(); String agg = jsObj.get("aggregator").getAsString(); if (agg.equals(Aggregator.CALCULATED.name())) { String type = jsObj.get("type").getAsString(); if (type.equals(MeasureType.CALLBACK.name())) { return context.deserialize(jsObj, CallbackMeasure.class); }/*from w ww .j a va 2s.c o m*/ return context.deserialize(jsObj, ExtendMinicubeMeasure.class); } return context.deserialize(jsObj, MiniCubeMeasure.class); } return null; }