List of usage examples for com.google.gson JsonElement isJsonArray
public boolean isJsonArray()
From source file:fr.inria.atlanmod.discoverer.JsonInjector.java
License:Open Source License
/** * Injects a model conforming to the metamodel from a set of Json Objects * // w w w. ja v a 2 s .c o m * @param rootElement * @param ePackage * @return */ private List<EObject> inject(JsonElement rootElement, EPackage ePackage) { // Getting the JSON objects List<JsonObject> elements = new ArrayList<JsonObject>(); if (rootElement.isJsonArray()) { LOGGER.finer("Several objects found"); for (int i = 0; i < rootElement.getAsJsonArray().size(); i++) if (rootElement.getAsJsonArray().get(i).isJsonObject()) elements.add(rootElement.getAsJsonArray().get(i).getAsJsonObject()); } else if (rootElement.isJsonObject()) { LOGGER.finer("Only one object found"); elements.add(rootElement.getAsJsonObject()); } else { LOGGER.finest("The root element was " + rootElement.getClass().getName()); LOGGER.finest("It is: " + rootElement.getAsString()); } // Getting the root element metamodel = ePackage; EClassifier eClassifier = metamodel.getEClassifier(jsonSource.getName()); List<EObject> eObjects = new ArrayList<EObject>(); for (JsonObject jsonObject : elements) { EObject eObject = instantiateEClassifier(eClassifier, jsonObject); eObjects.add(eObject); } return eObjects; }
From source file:fr.inria.atlanmod.discoverer.JsonInjector.java
License:Open Source License
protected EObject instantiateEClassifier(EClassifier eClassifier, JsonObject jsonObject) { EObject result = null;/*from w w w.j a v a 2 s.c om*/ if (eClassifier instanceof EClass) { LOGGER.finer("Instantiating class " + eClassifier.getName()); EClass eClass = (EClass) eClassifier; result = EcoreUtil.create(eClass); Iterator<Map.Entry<String, JsonElement>> pairs = jsonObject.entrySet().iterator(); while (pairs.hasNext()) { Map.Entry<String, JsonElement> pair = pairs.next(); String pairId = pair.getKey(); JsonElement value = pair.getValue(); EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature(pairId); if (eStructuralFeature != null) { if (value.isJsonArray()) { for (int i = 0; i < value.getAsJsonArray().size(); i++) { JsonElement singleValue = value.getAsJsonArray().get(i); setStructuralFeature(result, eStructuralFeature, singleValue); } } else { setStructuralFeature(result, eStructuralFeature, value); } } } } return result; }
From source file:fr.inria.atlanmod.discoverer.JsonInjector.java
License:Open Source License
@SuppressWarnings("unchecked") protected void setStructuralFeature(EObject result, EStructuralFeature eStructuralFeature, JsonElement value) { if (value.isJsonArray()) { LOGGER.finer("Detected array in array for " + eStructuralFeature.getName()); for (int i = 0; i < value.getAsJsonArray().size(); i++) { JsonElement singleValue = value.getAsJsonArray().get(i); setStructuralFeature(result, eStructuralFeature, singleValue); }//from w ww .j av a 2 s . c o m } else { LOGGER.finer("Setting feature " + eStructuralFeature.getName()); if (eStructuralFeature instanceof EAttribute) { EAttribute eAttribute = (EAttribute) eStructuralFeature; if (eStructuralFeature.getUpperBound() == -1) { EList<Object> set = (EList<Object>) result.eGet(eAttribute); set.add(digestValue(eAttribute, value)); } else { result.eSet(eAttribute, digestValue(eAttribute, value)); } } else if (eStructuralFeature instanceof EReference) { EReference eReference = (EReference) eStructuralFeature; if (value.isJsonObject()) { JsonObject childJsonObject = value.getAsJsonObject(); String childClassName = eReference.getEType().getName(); EClassifier eChildClassifier = metamodel.getEClassifier(childClassName); if (eChildClassifier != null) { EObject child = instantiateEClassifier(eChildClassifier, childJsonObject); if (eStructuralFeature.getUpperBound() == -1) { EList<Object> set = (EList<Object>) result.eGet(eReference); set.add(child); } else { result.eSet(eReference, child); } } } } } }
From source file:fr.inria.atlanmod.discoverer.JsonInjector.java
License:Open Source License
protected Object digestValue(EAttribute eAttribute, JsonElement value) { if (eAttribute.getEType().equals(EcorePackage.Literals.ESTRING)) { if (value.isJsonArray() || value.isJsonObject()) return ""; // TODO Improve discovery process to deal with this else/* w ww .ja v a 2 s.c o m*/ return value.getAsJsonPrimitive().getAsString(); } else if (eAttribute.getEType().equals(EcorePackage.Literals.EINT)) { return new Integer(value.getAsJsonPrimitive().getAsNumber().intValue()); } else if (eAttribute.getEType().equals(EcorePackage.Literals.EBOOLEAN)) { return value.getAsJsonPrimitive().getAsBoolean() ? Boolean.TRUE : Boolean.FALSE; } else { return null; } }
From source file:fr.inria.atlanmod.discoverer.JsonMultiDiscoverer.java
License:Open Source License
/** * Gets the values for a particular key// w ww . ja v a2 s .c o m * * @param name * @param sourceName * @return * @throws FileNotFoundException */ private List<Object> getJSONValues(EAttribute eAttribute, String sourceName) throws FileNotFoundException { if (eAttribute == null) throw new IllegalArgumentException("EAttribute cannot be null"); if (sourceName == null || sourceName.equals("")) throw new IllegalArgumentException("sourceName cannot be null or empty"); List<Object> result = new ArrayList<Object>(); List<JsonData> jsonDefs = this.sourceSet.getJsonSource(sourceName).getJsonData(); if (jsonDefs.size() == 0) return result; JsonElement rootElement = jsonDefs.get(0).getData(); // TODO Consider all of them! List<JsonObject> elements = new ArrayList<JsonObject>(); if (rootElement.isJsonArray()) { LOGGER.finest("Several objects found"); for (int i = 0; i < rootElement.getAsJsonArray().size(); i++) if (rootElement.getAsJsonArray().get(i).isJsonObject()) elements.add(rootElement.getAsJsonArray().get(i).getAsJsonObject()); } else if (rootElement.isJsonObject()) { LOGGER.finest("Only one object found"); elements.add(rootElement.getAsJsonObject()); } else { LOGGER.finest("The root element was " + rootElement.getClass().getName()); LOGGER.finest("It is: " + rootElement.getAsString()); } String containmentElementName = eAttribute.eContainer().eClass().getName(); String attributeName = eAttribute.getName(); if (containmentElementName.equals(sourceName)) { // If the containmentElement name matches with the sourceName, // we have to deal with the root elements of the JSON for (JsonObject jsonObject : elements) { Iterator<Map.Entry<String, JsonElement>> pairs = jsonObject.entrySet().iterator(); while (pairs.hasNext()) { Map.Entry<String, JsonElement> pair = pairs.next(); String key = pair.getKey(); if (key.equals(attributeName) && pair.getValue().isJsonPrimitive() && pair.getValue().getAsJsonPrimitive().isString()) { result.add(pair.getValue().getAsJsonPrimitive().toString()); } } } } else { // Else, take the objects inside the JSON for (JsonObject jsonObject : elements) { Iterator<Map.Entry<String, JsonElement>> pairs = jsonObject.entrySet().iterator(); while (pairs.hasNext()) { Map.Entry<String, JsonElement> pair = pairs.next(); String key = pair.getKey(); String jsonElementName = digestId(key); if (pair.getValue().isJsonObject() && jsonElementName.equals(containmentElementName)) { JsonObject jsonObject2 = pair.getValue().getAsJsonObject(); Iterator<Map.Entry<String, JsonElement>> pairs2 = jsonObject2.entrySet().iterator(); while (pairs2.hasNext()) { Map.Entry<String, JsonElement> pair2 = pairs2.next(); String key2 = pair2.getKey(); if (key2.equals(attributeName) && pair2.getValue().isJsonPrimitive() && pair2.getValue().getAsJsonPrimitive().isString()) { result.add(pair2.getValue().getAsJsonPrimitive().toString()); } // TODO make this recursive! } } } } } return result; }
From source file:fr.inria.atlanmod.discoverer.JsonMultiInjector.java
License:Open Source License
protected void inject(JsonElement rootElement, EPackage metamodel, Coverage coverage) throws FileNotFoundException { List<JsonObject> elements = new ArrayList<JsonObject>(); if (rootElement.isJsonArray()) { LOGGER.finer("Several objects found"); for (int i = 0; i < rootElement.getAsJsonArray().size(); i++) if (rootElement.getAsJsonArray().get(i).isJsonObject()) elements.add(rootElement.getAsJsonArray().get(i).getAsJsonObject()); } else if (rootElement.isJsonObject()) { LOGGER.finer("Only one object found"); elements.add(rootElement.getAsJsonObject()); } else {/* ww w . java2 s . co m*/ LOGGER.finest("The root element was " + rootElement.getClass().getName()); LOGGER.finest("It is: " + rootElement.getAsString()); } EClassifier eClassifier = metamodel.getEClassifier("Root"); for (JsonObject jsonObject : elements) { EObject eObject = instantiateEClassifier(eClassifier, jsonObject, null, coverage, metamodel); } }
From source file:fr.inria.atlanmod.discoverer.JsonMultiInjector.java
License:Open Source License
protected EObject instantiateEClassifier(EClassifier eClassifier, JsonObject jsonObject, EClassifier compositeEClassifier, Coverage coverage, EPackage metamodel) { EObject result = null;/*w w w .j ava2 s. c o m*/ EObject compositeResult = null; if (eClassifier instanceof EClass) { EClass eClass = (EClass) eClassifier; result = EcoreUtil.create(eClass); EClass compositeEClass = (EClass) findCompositeEClass(eClass, coverage); EObject existingCompositeEObject = findExistingEObject(eClass, jsonObject, compositeEClass, coverage, metamodel); if (existingCompositeEObject != null) { return existingCompositeEObject; } compositeResult = EcoreUtil.create(compositeEClass); currentModel.add(compositeResult); Iterator<Map.Entry<String, JsonElement>> pairs = jsonObject.entrySet().iterator(); while (pairs.hasNext()) { Map.Entry<String, JsonElement> pair = pairs.next(); String pairId = pair.getKey(); JsonElement value = pair.getValue(); EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature(pairId); if (eStructuralFeature != null) { if (value.isJsonArray()) { for (int i = 0; i < value.getAsJsonArray().size(); i++) { JsonElement singleValue = value.getAsJsonArray().get(i); if (singleValue.isJsonObject()) // TODO What happens if it is not a jsonObject? setStructuralFeature(result, eStructuralFeature, singleValue, compositeResult, coverage, metamodel); } } else { setStructuralFeature(result, eStructuralFeature, value, compositeResult, coverage, metamodel); } } } } return compositeResult; }
From source file:fr.inria.atlanmod.discoverer.JsonSource.java
License:Open Source License
/** * Computes a list of JSON objects according to the data of this source. * If the source includes inputs, the list will include the set of input elements as roots for the * JSON data of each source provided.//from w w w . java 2 s .c om * If the source does not include inputs, the listt will include all the objects from the JSON data * of each source provided. * * @return */ public List<JsonObject> getSourceDigested() { List<JsonObject> result = new ArrayList<JsonObject>(); if (this.withInput == true) { for (JsonData data : this.getJsonData()) { JsonObject inputElement = data.getInput(); JsonElement outputElement = data.getData(); inputElement.getAsJsonObject().add(getName() + "Output", outputElement); result.add(inputElement); } } else { for (JsonData data : this.getJsonData()) { JsonElement outputElement = data.getData(); if (outputElement.isJsonArray()) { for (int i = 0; i < outputElement.getAsJsonArray().size(); i++) if (outputElement.getAsJsonArray().get(i).isJsonObject()) result.add(outputElement.getAsJsonArray().get(i).getAsJsonObject()); } else if (outputElement.isJsonObject()) { result.add(outputElement.getAsJsonObject()); } } } return result; }
From source file:fr.zcraft.MultipleInventories.snaphots.ItemStackSnapshot.java
License:Open Source License
/** * From a JSON element, constructs a data structure representing * the same structure (recursively) using native types. * * <p>We had to re-implement this to ensure the generated structure to have the * right data type (instead of all numbers being doubles) and precision.</p> * * @param element The json element to be decoded. * @return A native data structure (either a {@link Map Map<String, Object>}, * a {@link List List<Object>}, or a native type) representing the same * structure (recursively).//from w w w . j a v a 2s . c o m * * @see #jsonToNative(JsonObject) Converts a json object to an explicit {@link Map}. * The JavaDoc also contains explainations on why this is needed. */ private static Object jsonToNative(final JsonElement element) { if (element.isJsonObject()) { return jsonToNative(element.getAsJsonObject()); } else if (element.isJsonArray()) { final List<Object> list = new ArrayList<>(); element.getAsJsonArray().forEach(listElement -> { final Object nativeValue = jsonToNative(listElement); if (nativeValue != null) { list.add(nativeValue); } }); return list; } else if (element.isJsonPrimitive()) { final JsonPrimitive primitive = element.getAsJsonPrimitive(); if (primitive.isBoolean()) { return primitive.getAsBoolean(); } else if (primitive.isString()) { return primitive.getAsString(); } else /* it's a number we yet have to find the type. */ { final BigDecimal number = primitive.getAsBigDecimal(); try { return number.byteValueExact(); } catch (final ArithmeticException e1) { try { return number.shortValueExact(); } catch (final ArithmeticException e2) { try { return number.intValueExact(); } catch (final ArithmeticException e3) { try { return number.longValueExact(); } catch (final ArithmeticException e4) { try { return number.doubleValue(); } catch (final ArithmeticException | NumberFormatException e5) { return number; } } } } } } } // Else the element is null. return null; }
From source file:gate.crowdsource.rest.CrowdFlowerClient.java
License:Open Source License
/** * Get the list of judgments for the given unit. If there are no * judgments, null is returned./* w w w.j a v a2s .c om*/ * * @param jobId the CrowdFlower job identifier * @param unitId the unit identifier * @return the "judgments" array for this unit, or null if none found. */ public JsonArray getJudgments(long jobId, long unitId) { try { String uri = "/jobs/" + jobId + "/units/" + unitId; JsonElement unitResponse = get(uri); if (!unitResponse.isJsonObject()) { throw new GateRuntimeException("Response from " + uri + " was not a JSON object"); } JsonElement results = unitResponse.getAsJsonObject().get("results"); if (!results.isJsonObject()) { log.info("No results found for job " + jobId + " unit " + unitId); return null; } JsonElement judgments = results.getAsJsonObject().get("judgments"); if (judgments.isJsonArray()) { return judgments.getAsJsonArray(); } else { return null; } } catch (IOException e) { throw new GateRuntimeException("Could not retrieve unit details", e); } }