List of usage examples for com.google.gson JsonElement getAsJsonArray
public JsonArray getAsJsonArray()
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 w w. j ava 2 s.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 . ja va 2 s . 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./*from w w w . j a v a 2s .c o 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.seleritycorp.context.PrintUtils.java
License:Apache License
/** * Prints a recommended content item to stdout. * /*from www . ja v a2 s . c om*/ * @param recommendation The recommended content item * @throws Exception if errors occur */ public void printRecommendation(JsonObject recommendation) throws Exception { printer.println(""); printer.println("* " + jsonUtils.getAsString(recommendation, "headline")); printer.println(""); printer.println(" contentID: " + jsonUtils.getAsString(recommendation, "contentID")); printer.println(" contentType: " + jsonUtils.getAsString(recommendation, "contentType")); printer.println(" source: " + jsonUtils.getAsString(recommendation, "source")); printer.println(" timestamp: " + jsonUtils.getAsString(recommendation, "timestamp")); printer.println(" score: " + jsonUtils.getAsString(recommendation, "score")); JsonElement contributions = recommendation.get("contributions"); if (contributions != null) { for (JsonElement contribution : contributions.getAsJsonArray()) { printContribution(contribution.getAsJsonObject()); } } printer.println(" summary: " + jsonUtils.getAsString(recommendation, "summary")); printer.println(" socialInfo->author: " + jsonUtils.getAsString(recommendation, "socialInfo", "author")); printer.println(" linkURL: " + jsonUtils.getAsString(recommendation, "linkURL")); JsonArray relatedContentArray = recommendation.get("relatedContent").getAsJsonArray(); for (JsonElement relatedContentElement : relatedContentArray) { JsonObject relatedContentObject = relatedContentElement.getAsJsonObject(); String line = " related content:"; line += " " + jsonUtils.getAsString(relatedContentObject, "relationship"); line += " " + jsonUtils.getAsString(relatedContentObject, "contentItem", "contentType"); line += " " + jsonUtils.getAsString(relatedContentObject, "contentItem", "linkURL"); printer.println(line); } }
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 w w w . j a v a 2 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.simiacryptus.mindseye.lang.Tensor.java
License:Apache License
/** * From json tensor./*from w ww . j a v a2 s .c o 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; } }
From source file:com.sk89q.worldedit.util.gson.BlockVectorAdapter.java
License:Open Source License
@Override public BlockVector3 deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonArray jsonArray = json.getAsJsonArray(); if (jsonArray.size() != 3) { throw new JsonParseException("Expected array of 3 length for BlockVector3"); }//from w w w . j a va2 s . com double x = jsonArray.get(0).getAsInt(); double y = jsonArray.get(1).getAsInt(); double z = jsonArray.get(2).getAsInt(); return BlockVector3.at(x, y, z); }
From source file:com.sk89q.worldedit.util.gson.VectorAdapter.java
License:Open Source License
@Override public Vector deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonArray jsonArray = json.getAsJsonArray(); if (jsonArray.size() != 3) { throw new JsonParseException("Expected array of 3 length for Vector"); }//from w ww . j a v a 2 s . c o m double x = jsonArray.get(0).getAsDouble(); double y = jsonArray.get(1).getAsDouble(); double z = jsonArray.get(2).getAsDouble(); return new Vector(x, y, z); }
From source file:com.skysql.manager.api.Backups.java
License:Open Source License
public Backups deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException, NullPointerException { Backups backups = new Backups(); JsonElement jsonElement = json.getAsJsonObject().get("backups"); if (jsonElement.isJsonNull()) { backups.setBackupsList(null);/*from w w w . java2s . c o m*/ } else { JsonArray array = jsonElement.getAsJsonArray(); int length = array.size(); LinkedHashMap<String, BackupRecord> backupsList = new LinkedHashMap<String, BackupRecord>(length); for (int i = 0; i < length; i++) { JsonObject backupJson = array.get(i).getAsJsonObject(); JsonElement element; String id = (element = backupJson.get("backupid")).isJsonNull() ? null : element.getAsString(); String node = (element = backupJson.get("nodeid")).isJsonNull() ? null : element.getAsString(); String level = (element = backupJson.get("level")).isJsonNull() ? null : element.getAsString(); String parent = (element = backupJson.get("parentid")).isJsonNull() ? null : element.getAsString(); String status = (element = backupJson.get("state")).isJsonNull() ? null : element.getAsString(); String started = (element = backupJson.get("started")).isJsonNull() ? null : element.getAsString(); String updated = (element = backupJson.get("updated")).isJsonNull() ? null : element.getAsString(); String restored = (element = backupJson.get("restored")).isJsonNull() ? null : element.getAsString(); String size = (element = backupJson.get("size")).isJsonNull() ? null : element.getAsString(); String storage = (element = backupJson.get("backupurl")).isJsonNull() ? null : element.getAsString(); String log = (element = backupJson.get("log")).isJsonNull() ? null : element.getAsString(); BackupRecord backupRecord = new BackupRecord(id, status, started, updated, level, node, size, storage, restored, log, parent); backupsList.put(id, backupRecord); } backups.setBackupsList(backupsList); } return backups; }
From source file:com.skysql.manager.api.BackupStates.java
License:Open Source License
public BackupStates deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException, NullPointerException { BackupStates backupStates = new BackupStates(); JsonElement jsonElement = json.getAsJsonObject().get("backupStates"); if (jsonElement.isJsonNull()) { backupStates.setBackupStatesDescriptions(null); } else {/*from ww w . jav a 2 s.co m*/ JsonArray array = jsonElement.getAsJsonArray(); int length = array.size(); LinkedHashMap<String, String> descriptions = new LinkedHashMap<String, String>(length); for (int i = 0; i < length; i++) { JsonObject pair = array.get(i).getAsJsonObject(); descriptions.put(pair.get("state").getAsString(), pair.get("description").getAsString()); } backupStates.setBackupStatesDescriptions(descriptions); } return backupStates; }