List of usage examples for com.google.gson JsonElement isJsonArray
public boolean isJsonArray()
From source file:com.sonaive.v2ex.io.ReviewsHandler.java
License:Open Source License
@Override public void process(JsonElement element) { if (element.isJsonArray()) { for (Review review : new Gson().fromJson(element, Review[].class)) { mReviews.put(String.valueOf(review.id), review); }// ww w . j av a2 s. c o m } }
From source file:com.springer.api.services.impl.BaseSpringerQuery.java
License:Apache License
@Override public PagedList<E> list() { InputStream jsonContent = null; try {//w w w . j a v a2 s . co m jsonContent = callApiGet(apiUrlBuilder.buildUrl()); JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET)); if (response.isJsonObject()) { JsonObject object = response.getAsJsonObject(); setApiKey(object.get("apiKey").getAsString()); setQuery(object.get("query").getAsString()); List<Cursor> cursors = getGsonBuilder().create().fromJson(object.get("result"), new TypeToken<List<Cursor>>() { }.getType()); PagedArrayList<E> responseList = new PagedArrayList<E>(); ; if (!cursors.isEmpty()) { responseList.setCursor(cursors.get(0)); } JsonElement jsonElement = object.get("records"); if (jsonElement.isJsonArray()) { JsonArray results = jsonElement.getAsJsonArray(); for (JsonElement result : results) { E element = unmarshall(result); responseList.add(element); } } jsonElement = object.get("facets"); if (jsonElement != null) { List<Facet> facets = getGsonBuilder().create().fromJson(jsonElement, new TypeToken<List<Facet>>() { }.getType()); setFacets(facets); } return responseList; } throw new SpringerException("Unknown content found in response:" + response.toString()); } catch (Exception e) { throw new SpringerException(e); } finally { closeStream(jsonContent); } }
From source file:com.strato.hidrive.api.connection.gateway.HiDriveDomainGateway.java
License:Apache License
protected List<DataReader> prepareDataReaderList(Response<String> response) throws Exception { if (response.getResponseData() == null) { return new ArrayList<DataReader>(); }/*w w w. java 2s .com*/ String responseString = unescapeResponseData(response.getResponseData()); JsonParser jsonParser = new JsonParser(); JsonElement jsonElement = jsonParser.parse(responseString); List<DataReader> dataReaders = new ArrayList<DataReader>(); if (jsonElement.isJsonArray()) { for (JsonElement jsonObject : (JsonArray) jsonElement) { dataReaders.add(new JSONDataReader((JsonObject) jsonObject)); } } else if (jsonElement.isJsonObject()) { dataReaders.add(new JSONDataReader((JsonObject) jsonElement)); } return dataReaders; }
From source file:com.synflow.core.util.CoreUtil.java
License:Open Source License
/** * Returns the dependencies of the given entity. * //from ww w .j a v a 2 s . com * @param entity * an entity * @return a JSON array, empty if the entity has no dependencies */ private static JsonArray getDependencies(JsonObject implementation) { if (implementation != null) { JsonElement dependencies = implementation.get(PROP_DEPENDENCIES); if (dependencies != null && dependencies.isJsonArray()) { return dependencies.getAsJsonArray(); } } return new JsonArray(); }
From source file:com.synflow.cx.internal.instantiation.properties.EntityPropertiesChecker.java
License:Open Source License
/** * Checks the "test" property. We use an instantiable because the entity is not translated when * we do this check (in the skeleton maker). * /* www . j a va2 s. c o m*/ * @param instantiable * instantiable * @param test * test element */ private void checkTest(Instantiable instantiable, JsonElement test) { if (test == null) { return; } if (!test.isJsonObject()) { handler.addError(test, "test must be an object"); } JsonObject objTest = test.getAsJsonObject(); Set<String> ports = new HashSet<>(); for (Variable port : CxUtil.getPorts(instantiable.getPortDecls())) { String name = port.getName(); ports.add(name); if (!objTest.has(name)) { handler.addError(objTest, "missing test values for port '" + name + "'"); continue; } JsonElement values = objTest.get(name); if (!values.isJsonArray()) { handler.addError(objTest, "test values for port '" + name + "' must be an array"); continue; } } for (Entry<String, JsonElement> entry : objTest.entrySet()) { String name = entry.getKey(); if (!ports.contains(name)) { handler.addError(objTest, "unknown port '" + name + "'"); } } }
From source file:com.synflow.cx.internal.instantiation.properties.InstancePropertiesChecker.java
License:Open Source License
/** * Checks and transforms the clocks in the properties of the given instance. Clocks accepts an * array and an object. If clocks is an array, this method transforms it to an object and checks * it./*from w w w . j a va2 s . c o m*/ * * @param instance */ private void updateClocksInstantiated(Instance instance) { DPN dpn = instance.getDPN(); JsonArray parentClocks = dpn.getProperties().getAsJsonArray(PROP_CLOCKS); Entity entity = instance.getEntity(); JsonArray entityClocks = entity.getProperties().getAsJsonArray(PROP_CLOCKS); // use {clock: 'name'} as a shortcut for {clocks: ['name']} JsonObject properties = instance.getProperties(); applyClockShortcut(properties); JsonElement clocks = properties.get(PROP_CLOCKS); if (clocks == null) { clocks = makeClocksObject(entityClocks, parentClocks); properties.add(PROP_CLOCKS, clocks); } else { if (clocks.isJsonArray()) { if (checkClockArray(clocks)) { clocks = makeClocksObject(entityClocks, clocks.getAsJsonArray()); properties.add(PROP_CLOCKS, clocks); } else { // do not check clocks return; } } else if (!clocks.isJsonObject()) { // do not check clocks handler.addError(clocks, "clocks must be an array or an object"); return; } } validateClocks(clocks.getAsJsonObject(), parentClocks, entityClocks); }
From source file:com.synflow.cx.internal.instantiation.properties.PropertiesChecker.java
License:Open Source License
/** * Check the 'clocks 'array./*from w ww .j av a 2 s . c o m*/ * * @param clocks * an array of clock names * @return <code>true</code> if it is valid */ protected boolean checkClockArray(JsonElement clocks) { boolean isValid; if (clocks.isJsonArray()) { isValid = true; JsonArray clocksArray = clocks.getAsJsonArray(); for (JsonElement clock : clocksArray) { if (!clock.isJsonPrimitive() || !clock.getAsJsonPrimitive().isString()) { isValid = false; break; } } } else { isValid = false; } if (!isValid) { handler.addError(clocks, "'clocks' must be an array of clock names"); } return isValid; }
From source file:com.synflow.cx.internal.validation.ClockDomainComputer.java
License:Open Source License
/** * Returns true if the entity referenced by this instance is combinational (has no clocks). * /*from w w w . j ava 2s .co m*/ * @param instance * an instance * @return a boolean */ public boolean isCombinational(Instance instance) { JsonObject properties = instance.getEntity().getProperties(); JsonElement clocks = properties.get(PROP_CLOCKS); return clocks.isJsonArray() && clocks.getAsJsonArray().size() == 0; }
From source file:com.talvish.tales.serialization.json.translators.JsonArrayToArrayTranslator.java
License:Apache License
/** * Translates the received object into an array; * If the object is of the wrong type, a TranslationException will occur. *///from w ww. j av a 2 s .com @Override public Object translate(Object anObject) { Object returnValue; if (anObject == null || anObject.equals(JsonNull.INSTANCE)) { returnValue = null; } else { try { // things to look at later JsonElement jsonElement = (JsonElement) anObject; if (jsonElement.isJsonArray()) { JsonArray jsonArray = (JsonArray) anObject; Object array = Array.newInstance(elementType, jsonArray.size()); for (int count = 0; count < jsonArray.size(); count += 1) { Array.set(array, count, elementTranslator.translate(jsonArray.get(count))); } returnValue = array; } else if (readSingles) { Object array = Array.newInstance(elementType, 1); Array.set(array, 0, elementTranslator.translate(jsonElement)); returnValue = array; } else { throw new TranslationException( String.format("Attempt to translate an array but a single object was sent instead.")); } } catch (IllegalArgumentException e) { throw new TranslationException(e); } catch (ClassCastException e) { throw new TranslationException(e); } } return returnValue; }
From source file:com.tesla.framework.common.util.json.JSONHelper.java
License:Apache License
/** * @param element/*from w ww. j a v a2 s.co m*/ * @return ?element???? * elementlist */ @NonNull public static List<JsonObject> toJsonObjects(@NonNull JsonElement element) { Preconditions.checkNotNull(element); if (element.isJsonNull()) return Collections.emptyList(); List<JsonObject> list = new ArrayList<>(); if (element.isJsonObject()) { list.add((JsonObject) element); return list; } if (element.isJsonArray()) { JsonArray arr = (JsonArray) element; Iterator<JsonElement> itr = arr.iterator(); while (itr.hasNext()) { JsonElement e = itr.next(); list.addAll(toJsonObjects(e)); } return list; } return list; }