List of usage examples for com.google.gson JsonElement isJsonArray
public boolean isJsonArray()
From source file:com.microsoft.windowsazure.mobileservices.MobileServiceJsonTable.java
License:Open Source License
/** * Looks up a row in the table and retrieves its JSON value. * /*from www . j a va 2 s. co m*/ * @param id * The id of the row * @param callback * Callback to invoke after the operation is completed */ public void lookUp(Object id, final TableJsonOperationCallback callback) { // Create request URL String url = null; try { url = mClient.getAppUrl().toString() + TABLES_URL + URLEncoder.encode(mTableName, MobileServiceClient.UTF8_ENCODING) + "/" + URLEncoder.encode(id.toString(), MobileServiceClient.UTF8_ENCODING); } 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) { 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.microsoft.windowsazure.mobileservices.MobileServiceTable.java
License:Open Source License
/** * Parses the JSON object to a typed list * /* ww w . j a va2 s . c o m*/ * @param results * JSON results * @return List of entities */ private List<E> parseResults(JsonElement results) { Gson gson = mClient.getGsonBuilder().create(); List<E> result = new ArrayList<E>(); String idPropertyName = getIdPropertyName(mClazz); // Parse results if (results.isJsonArray()) // Query result { JsonArray elements = results.getAsJsonArray(); for (JsonElement element : elements) { changeIdPropertyName(element.getAsJsonObject(), idPropertyName); E typedElement = gson.fromJson(element, mClazz); result.add(typedElement); } } else { // Lookup result changeIdPropertyName(results.getAsJsonObject(), idPropertyName); E typedElement = gson.fromJson(results, mClazz); result.add(typedElement); } return result; }
From source file:com.microsoft.windowsazure.mobileservices.table.serialization.JsonEntityParser.java
License:Open Source License
/** * Parses the JSON object to a typed list * * @param results JSON results//w ww.j a v a 2 s . c om * @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.microsoft.windowsazure.mobileservices.table.sync.MobileServiceSyncContext.java
License:Open Source License
private void processPull(String tableName, Query query, String queryId) throws Throwable { try {/* ww w.j a v a 2s.co m*/ MobileServiceJsonTable table = this.mClient.getTable(tableName); table.addFeature(MobileServiceFeatures.Offline); if (query == null) { query = table.top(1000).orderBy("id", QueryOrder.Ascending); } else { query = query.deepClone(); } PullStrategy strategy; if (queryId != null) { strategy = new IncrementalPullStrategy(query, queryId, this.mStore, table); } else { strategy = new PullStrategy(query, table); } strategy.initialize(); JsonArray elements = null; do { JsonElement result = table.execute(strategy.getLastQuery()).get(); if (result != null) { if (result.isJsonObject()) { JsonObject jsonObject = result.getAsJsonObject(); if (jsonObject.has("results") && jsonObject.get("results").isJsonArray()) { elements = jsonObject.get("results").getAsJsonArray(); } } else if (result.isJsonArray()) { elements = result.getAsJsonArray(); } processElements(tableName, elements); strategy.onResultsProcessed(elements); } } while (strategy.moveToNextPage(elements.size())); } catch (ExecutionException e) { throw e.getCause(); } catch (RuntimeException e) { throw e.getCause(); } }
From source file:com.microsoft.windowsazure.mobileservices.table.sync.queue.OperationErrorList.java
License:Open Source License
/** * Loads the list of table operation errors from the local store * * @param store the local store/*from ww w . j ava2s. c o m*/ * @return the list of table operation errors * @throws java.text.ParseException * @throws MobileServiceLocalStoreException */ public static OperationErrorList load(MobileServiceLocalStore store) throws ParseException, MobileServiceLocalStoreException { OperationErrorList opQueue = new OperationErrorList(store); JsonElement operations = store.read(QueryOperations.tableName(OPERATION_ERROR_TABLE)); if (operations.isJsonArray()) { JsonArray array = (JsonArray) operations; for (JsonElement element : array) { if (element.isJsonObject()) { TableOperationError operationError = deserialize((JsonObject) element); opQueue.mList.add(operationError); } } } return opQueue; }
From source file:com.microsoft.windowsazure.mobileservices.table.sync.queue.OperationQueue.java
License:Open Source License
/** * Loads the queue of table operations from the local store * * @param store the local store/*from www .j a v a 2s .c o m*/ * @return the queue of table operations * @throws java.text.ParseException * @throws MobileServiceLocalStoreException */ public static OperationQueue load(MobileServiceLocalStore store) throws ParseException, MobileServiceLocalStoreException { OperationQueue opQueue = new OperationQueue(store); JsonElement operations = store.read(QueryOperations.tableName(OPERATION_QUEUE_TABLE) .orderBy("__queueLoadedAt", QueryOrder.Ascending).orderBy("sequence", QueryOrder.Ascending)); if (operations.isJsonArray()) { JsonArray array = (JsonArray) operations; for (JsonElement element : array) { if (element.isJsonObject()) { OperationQueueItem opQueueItem = deserialize((JsonObject) element); opQueue.mQueue.add(opQueueItem); // '/' is a reserved character that cannot be used on string // ids. // We use it to build a unique compound string from // tableName and itemId String tableItemId = opQueueItem.getTableName() + "/" + opQueueItem.getItemId(); opQueue.mIdOperationMap.put(tableItemId, opQueueItem); Integer tableCount = opQueue.mTableCountMap.get(opQueueItem.getTableName()); if (tableCount != null) { opQueue.mTableCountMap.put(opQueueItem.getTableName(), tableCount + 1); } else { opQueue.mTableCountMap.put(opQueueItem.getTableName(), 1); } } } } return opQueue; }
From source file:com.mot.ajaxwebapp.BaseActivity.java
License:Apache License
/** * convert an inputstream to JsonElement with JsonParser. * Throw exception if json convertion failed, caller will handled the exception and close the stream. *//*w ww . jav a 2 s . c o m*/ public static JsonElement unmarshall(InputStream jsonContent) throws Exception { JsonParser parser = new JsonParser(); JsonElement element = parser.parse(new InputStreamReader(jsonContent, "UTF-8")); if (element.isJsonObject()) { return element.getAsJsonObject(); } else if (element.isJsonArray()) { return element.getAsJsonArray(); } else { throw new Exception("Unknown content found in response." + element); } }
From source file:com.nextdoor.bender.operation.json.key.FlattenOperation.java
License:Apache License
protected void perform(JsonObject obj) { Set<Entry<String, JsonElement>> entries = obj.entrySet(); Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries); for (Entry<String, JsonElement> entry : orgEntries) { JsonElement val = entry.getValue(); if (val.isJsonPrimitive() || val.isJsonNull()) { continue; }//from ww w . ja v a 2 s . c o m obj.remove(entry.getKey()); if (val.isJsonObject()) { perform(obj, val.getAsJsonObject(), entry.getKey()); } else if (val.isJsonArray()) { perform(obj, val.getAsJsonArray(), entry.getKey()); } } }
From source file:com.nextdoor.bender.operation.json.key.FlattenOperation.java
License:Apache License
protected void perform(JsonObject obj, JsonArray nested_arr, String parent) { int c = 0;//ww w . j av a2 s .co m for (JsonElement val : nested_arr) { c += 1; String key = parent + separator + c; if (val.isJsonArray()) { perform(obj, val.getAsJsonArray(), key); } else if (val.isJsonObject()) { perform(obj, val.getAsJsonObject(), key); } else { obj.add(key, val); } } }
From source file:com.nextdoor.bender.operation.json.key.FlattenOperation.java
License:Apache License
protected void perform(JsonObject obj, JsonObject nested_obj, String parent) { Set<Entry<String, JsonElement>> entries = nested_obj.entrySet(); Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries); for (Entry<String, JsonElement> entry : orgEntries) { JsonElement val = entry.getValue(); String key = parent + separator + entry.getKey(); if (val.isJsonObject()) { perform(obj, val.getAsJsonObject(), key); } else if (val.isJsonArray()) { perform(obj, val.getAsJsonArray(), key); } else {/*from www. j a v a2 s. c om*/ obj.add(key, val); } } }