List of usage examples for com.google.gson JsonElement isJsonArray
public boolean isJsonArray()
From source file:com.sat.common.SDdetails.java
License:Open Source License
private static void parserGson(String q) { JsonElement jelement = new JsonParser().parse(q); if (jelement.isJsonArray()) { prArray(jelement);//from w w w.j a v a 2 s .c om } else prObject(jelement); }
From source file:com.sat.common.SDdetails.java
License:Open Source License
private static void prArray(JsonElement jelement) { // System.out.println("------------------------Array---------------------------"); JsonArray jarray = jelement.getAsJsonArray(); for (JsonElement jobjecte : jarray) { if (jobjecte.isJsonObject()) { prObject(jobjecte);//from ww w .ja v a 2s . c o m } else if (jobjecte.isJsonArray()) { prArray(jobjecte); } else { System.out.println("sathish"); } } }
From source file:com.savoirtech.json.processor.JsonComparisonProcessor.java
License:Apache License
/** * Walk the JSON and compare the actual JSON to the template JSON, applying rules as-needed. * * @param path current path to the JSON elements given. * @param templateEle the template, or expected, JSON at this path. * @param actualEle the actual JSON at this path. * @return result indicating whether there is a match, and providing a description when there is a * mismatch./*from w w w .j a v a 2s. c o m*/ */ private JsonComparatorResult walkAndCompare(String path, JsonElement templateEle, JsonElement actualEle) { JsonComparatorResult result; // Find the rule that applies, if any JsonComparatorCompiledRule rule = this.ruleProcessor.findMatchingRule(path); if (rule != null) { result = rule.compare(path, templateEle, actualEle, this.childRuleComparator); } else { result = this.shallowCompareJsonElements(path, templateEle, actualEle); } // Make sure contents of objects and arrays are walked, as needed if ((result.isMatch()) && (!result.isDeep())) { if (actualEle.isJsonObject()) { result = this.walkJsonObjectFields(path, templateEle.getAsJsonObject(), actualEle.getAsJsonObject()); } else if (actualEle.isJsonArray()) { result = this.walkJsonArray(path, templateEle.getAsJsonArray(), actualEle.getAsJsonArray()); } } return result; }
From source file:com.savoirtech.json.processor.JsonComparisonProcessor.java
License:Apache License
/** * Performs a minimal, shallow comparison of the two given JSON elements. *//*from ww w . j av a 2s .c om*/ private JsonComparatorResult shallowCompareJsonElements(String path, JsonElement expected, JsonElement actual) { JsonComparatorResult result; if (expected.isJsonObject()) { if (actual.isJsonObject()) { return new JsonComparatorResult(false, true, null, null); } else { return new JsonComparatorResult(false, false, "actual json at path " + path + " is not an object, but an object is expected", path); } } else if (expected.isJsonArray()) { if (actual.isJsonArray()) { return new JsonComparatorResult(false, true, null, null); } else { return new JsonComparatorResult(false, false, "actual json at path " + path + " is not an array, but an array is expected", path); } } else { if (expected.equals(actual)) { return new JsonComparatorResult(false, true, null, null); } else { return new JsonComparatorResult(false, false, "primitive mismatch at path " + path + ": actual=" + actual + "; expected=" + expected, path); } } }
From source file:com.savoirtech.json.rules.impl.ArrayAsSetRule.java
License:Apache License
@Override public JsonComparatorResult compare(String path, JsonElement templateElement, JsonElement actualElement, JsonComparatorRuleSpecification specification, RuleChildComparator childComparator) { JsonComparatorResult result;/*w ww. j a va 2s. c o m*/ if (actualElement.isJsonArray()) { if (templateElement.isJsonArray()) { result = this.compareArraysAsSets(path, templateElement.getAsJsonArray(), actualElement.getAsJsonArray(), childComparator); } else { result = new JsonComparatorResult(true, false, "set rule on non-array template element at path " + path, path); } } else { result = new JsonComparatorResult(true, false, "set rule on non-array element at path " + path, path); } return result; }
From source file:com.scvngr.levelup.core.model.factory.json.GsonModelFactory.java
License:Apache License
/** * Parse a list of models from the JSON array passed. * * @param json the JSON representation of the list of models to parse. * @return a {@link List} of model instances. * @throws JsonParseException if there is a problem decoding the JSON structure. *//*from w w w .j ava 2s .c o m*/ @NonNull public final List<T> fromList(@NonNull final String json) throws JsonParseException { final JsonElement root = new JsonParser().parse(json); if (!root.isJsonArray()) { throw new JsonSyntaxException(NullUtils.format("JSON data must be a JSON array. Type is '%s'.", root.getClass().getSimpleName())); } return fromList(NullUtils.nonNullContract(root.getAsJsonArray())); }
From source file:com.seleritycorp.common.base.config.ConfigUtils.java
License:Apache License
/** * Adds a JSON element to a Config./* w ww. ja v a2s . co m*/ * * @param element The element to add * @param config The config instance to add the element to * @param key The key in the config space */ private static void loadJson(JsonElement element, ConfigImpl config, String key) { if (element.isJsonObject()) { loadJson(element.getAsJsonObject(), config, key); } else if (element.isJsonArray()) { loadJson(element.getAsJsonArray(), config, key); } else if (element.isJsonPrimitive()) { loadJson(element.getAsJsonPrimitive(), config, key); } else if (element.isJsonNull()) { // null does not need a dedicated representation, so we // skip this case. } else { throw new UnsupportedOperationException("Unimplemented " + "JsonElement state"); } }
From source file:com.shazam.shazamcrest.FieldsIgnorer.java
License:Apache License
private static void findPath(JsonElement jsonElement, String pathToFind, final List<String> pathSegments) { String field = headOf(pathSegments); if (pathSegments.size() == 1) { if (jsonElement.isJsonPrimitive()) { throw new IllegalArgumentException(); }//from www. j a v a2 s .c om ignorePath(jsonElement, pathToFind); } else { JsonElement child = jsonElement.getAsJsonObject().get(field); List<String> tail = pathSegments.subList(1, pathSegments.size()); if (child == null) { return; } if (child.isJsonArray()) { Iterator<JsonElement> iterator = child.getAsJsonArray().iterator(); while (iterator.hasNext()) { findPath((JsonElement) iterator.next(), pathToFind, tail); } } else { findPath(child, pathToFind, tail); } } }
From source file:com.shopify.buy.model.Checkout.java
License:Open Source License
/** * @param json The json input.//from w ww . jav a 2 s . c om * @return A checkout object created using the values in the JSON string. * */ public static Checkout fromJson(String json) { if (TextUtils.isEmpty(json)) { return null; } Gson gson = BuyClientUtils.createDefaultGson(Checkout.class); JsonObject checkoutElement = gson.fromJson(json, JsonElement.class).getAsJsonObject(); Checkout checkout = gson.fromJson(checkoutElement, Checkout.class); JsonElement attributeElement = checkoutElement.get(ATTRIBUTES_JSON_KEY); if (attributeElement != null) { List<CheckoutAttribute> attributes; // The attributes are an array of CheckoutAttributes when received from the server, and are flattened and serialized as a hash map // when serializing for the server, or internally. We have to check to see which case it is and deserialize appropriately. if (attributeElement.isJsonArray()) { attributes = gson.fromJson(attributeElement, new TypeToken<List<CheckoutAttribute>>() { }.getType()); } else { // We serialize the CheckoutAttributes to a hash map internally, and for sending to the server HashMap<String, String> attributesHashMap = gson.fromJson(attributeElement, HashMap.class); attributes = new ArrayList<>(); for (Map.Entry<String, String> pair : attributesHashMap.entrySet()) { attributes.add(new CheckoutAttribute(pair.getKey(), pair.getValue())); } } checkout.attributes = attributes; } return checkout; }
From source file:com.simiacryptus.mindseye.lang.Tensor.java
License:Apache License
/** * From json tensor.// w w w. jav a 2 s . co m * * @param json the json * @param resources the resources * @return the tensor */ @Nullable public static Tensor fromJson(@Nullable final JsonElement json, @Nullable Map<CharSequence, byte[]> resources) { if (null == json) return null; if (json.isJsonArray()) { final JsonArray array = json.getAsJsonArray(); final int size = array.size(); if (array.get(0).isJsonPrimitive()) { final double[] doubles = IntStream.range(0, size).mapToObj(i -> { return array.get(i); }).mapToDouble(element -> { return element.getAsDouble(); }).toArray(); @Nonnull Tensor tensor = new Tensor(doubles); assert tensor.isValid(); return tensor; } else { final List<Tensor> elements = IntStream.range(0, size).mapToObj(i -> { return array.get(i); }).map(element -> { return Tensor.fromJson(element, resources); }).collect(Collectors.toList()); @Nonnull final int[] dimensions = elements.get(0).getDimensions(); if (!elements.stream().allMatch(t -> Arrays.equals(dimensions, t.getDimensions()))) { throw new IllegalArgumentException(); } @Nonnull final int[] newDdimensions = Arrays.copyOf(dimensions, dimensions.length + 1); newDdimensions[dimensions.length] = size; @Nonnull final Tensor tensor = new Tensor(newDdimensions); @Nullable final double[] data = tensor.getData(); for (int i = 0; i < size; i++) { @Nullable final double[] e = elements.get(i).getData(); System.arraycopy(e, 0, data, i * e.length, e.length); } for (@Nonnull Tensor t : elements) { t.freeRef(); } assert tensor.isValid(); return tensor; } } else if (json.isJsonObject()) { JsonObject jsonObject = json.getAsJsonObject(); @Nonnull int[] dims = fromJsonArray(jsonObject.getAsJsonArray("length")); @Nonnull Tensor tensor = new Tensor(dims); SerialPrecision precision = SerialPrecision .valueOf(jsonObject.getAsJsonPrimitive("precision").getAsString()); JsonElement base64 = jsonObject.get("base64"); if (null == base64) { if (null == resources) throw new IllegalArgumentException("No Data Resources"); CharSequence resourceId = jsonObject.getAsJsonPrimitive("resource").getAsString(); tensor.setBytes(resources.get(resourceId), precision); } else { tensor.setBytes(Base64.getDecoder().decode(base64.getAsString()), precision); } assert tensor.isValid(); JsonElement id = jsonObject.get("id"); if (null != id) { tensor.setId(UUID.fromString(id.getAsString())); } return tensor; } else { @Nonnull Tensor tensor = new Tensor(json.getAsJsonPrimitive().getAsDouble()); assert tensor.isValid(); return tensor; } }