List of usage examples for com.google.gson JsonElement getAsJsonArray
public JsonArray getAsJsonArray()
From source file:com.cdancy.artifactory.rest.fallbacks.ArtifactoryFallbacks.java
License:Apache License
public static RequestStatus createPromoteBuildFromError(String message) { List<Message> messages = new ArrayList<>(); List<Error> errors = new ArrayList<>(); JsonReader reader = new JsonReader(new StringReader(message)); reader.setLenient(true);/*from w w w. j a v a2 s.c om*/ JsonElement possibleMessages = parser.parse(reader).getAsJsonObject().get("messages"); if (possibleMessages != null) { JsonArray jsonArray = possibleMessages.getAsJsonArray(); Iterator<JsonElement> iter = jsonArray.iterator(); while (iter.hasNext()) { JsonElement jsonElement = iter.next(); JsonObject jsonObject = jsonElement.getAsJsonObject(); Message mess = Message.create(jsonObject.get("level").getAsString(), jsonObject.get("message").getAsString()); messages.add(mess); } } JsonElement possibleErrors = parser.parse(message).getAsJsonObject().get("errors"); if (possibleErrors != null) { JsonArray jsonArray = possibleErrors.getAsJsonArray(); Iterator<JsonElement> iter = jsonArray.iterator(); while (iter.hasNext()) { JsonElement jsonElement = iter.next(); JsonObject jsonObject = jsonElement.getAsJsonObject(); Error error = Error.create(jsonObject.get("status").getAsInt(), jsonObject.get("message").getAsString()); errors.add(error); } } return RequestStatus.create(messages, errors); }
From source file:com.cinchapi.concourse.importer.LineBasedImporter.java
License:Apache License
/** * Import the data contained in {@code file} into {@link Concourse}. * <p>//from ww w . j a v a2 s . com * <strong>Note</strong> that if {@code resolveKey} is specified, an attempt * will be made to add the data in from each group into the existing records * that are found using {@code resolveKey} and its corresponding value in * the group. * </p> * * @param file * @param resolveKey * @return a collection of {@link ImportResult} objects that describes the * records created/affected from the import and whether any errors * occurred. */ public final Set<Long> importFile(String file, @Nullable String resolveKey) { // TODO add option to specify batchSize, which is how many objects to // send over the wire in one atomic batch List<String> lines = FileOps.readLines(file); String[] keys = header(); JsonArray array = new JsonArray(); boolean checkedFileFormat = false; for (String line : lines) { if (!checkedFileFormat) { validateFileFormat(line); checkedFileFormat = true; } if (keys == null) { keys = parseKeys(line); log.info("Parsed keys from header: " + line); } else { JsonObject object = parseLine(line, keys); if (resolveKey != null && object.has(resolveKey)) { JsonElement resolveValue = object.get(resolveKey); if (!resolveValue.isJsonArray()) { JsonArray temp = new JsonArray(); temp.add(resolveValue); resolveValue = temp; } for (int i = 0; i < resolveValue.getAsJsonArray().size(); ++i) { String value = resolveValue.getAsJsonArray().get(i).toString(); Object stored = Convert.stringToJava(value); Set<Long> resolved = concourse.find(resolveKey, Operator.EQUALS, stored); for (long record : resolved) { object = parseLine(line, keys); // this is // inefficient, but // there is no good // way to clone the // original object object.addProperty(Constants.JSON_RESERVED_IDENTIFIER_NAME, record); array.add(object); } } } else { array.add(object); } log.info("Importing {}", line); } } Set<Long> records = importString(array.toString()); return records; }
From source file:com.claresco.tinman.json.XapiCredentialsListJson.java
License:Open Source License
@Override public XapiCredentialsList deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { if (!arg0.isJsonArray()) { return null; }//from w w w .j a v a2 s. c om JsonArray theArray = arg0.getAsJsonArray(); HashMap<XapiKeySecret, XapiCredentials> theCredentialMap = new HashMap<XapiKeySecret, XapiCredentials>(); for (JsonElement e : theArray) { JsonObject theObject = JsonUtility.convertJsonElementToJsonObject(e); JsonObject theKeySecretJson = JsonUtility .convertJsonElementToJsonObject(JsonUtility.get(theObject, KEYSECRET)); XapiKeySecret theKS = new XapiKeySecret(JsonUtility.getElementAsString(theKeySecretJson, KEY), JsonUtility.getElementAsString(theKeySecretJson, SECRET)); XapiCredentials theCred = JsonUtility.delegateDeserialization(arg2, JsonUtility.get(theObject, CREDENTIALS), XapiCredentials.class); theCredentialMap.put(theKS, theCred); } return new XapiCredentialsList(theCredentialMap); }
From source file:com.claresco.tinman.json.XapiStatementBatchJson.java
License:Open Source License
@Override public XapiStatementBatch deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { XapiStatementBatch theBatch = new XapiStatementBatch(); // if it is not an array of statement if (!arg0.isJsonArray()) { XapiStatement theStatement = JsonUtility.delegateDeserialization(arg2, arg0, XapiStatement.class); theBatch.addStatementToBatch(theStatement); } else {//from w w w . j a v a 2 s .com JsonArray statementArrayJson = arg0.getAsJsonArray(); for (JsonElement element : statementArrayJson) { XapiStatement theStatement = JsonUtility.delegateDeserialization(arg2, element, XapiStatement.class); theBatch.addStatementToBatch(theStatement); } } return theBatch; }
From source file:com.cloud.agent.transport.ArrayTypeAdaptor.java
License:Apache License
@Override @SuppressWarnings("unchecked") public T[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonArray array = json.getAsJsonArray(); Iterator<JsonElement> it = array.iterator(); ArrayList<T> cmds = new ArrayList<T>(); while (it.hasNext()) { JsonObject element = (JsonObject) it.next(); Map.Entry<String, JsonElement> entry = element.entrySet().iterator().next(); String name = s_pkg + entry.getKey(); Class<?> clazz;/*from w w w. jav a2 s . co m*/ try { clazz = Class.forName(name); } catch (ClassNotFoundException e) { throw new CloudRuntimeException("can't find " + name); } T cmd = (T) _gson.fromJson(entry.getValue(), clazz); cmds.add(cmd); } Class<?> type = ((Class<?>) typeOfT).getComponentType(); T[] ts = (T[]) Array.newInstance(type, cmds.size()); return cmds.toArray(ts); }
From source file:com.cloudbase.CBNaturalDeserializer.java
License:Open Source License
public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { if (json.isJsonNull()) return null; else if (json.isJsonPrimitive()) return handlePrimitive(json.getAsJsonPrimitive()); else if (json.isJsonArray()) return handleArray(json.getAsJsonArray(), context); else/* www . j av a 2s . co m*/ return handleObject(json.getAsJsonObject(), context); }
From source file:com.codereligion.bugsnag.logback.resource.TabVOSerializer.java
License:Apache License
private void filterJsonElement(final JsonElement jsonElement) { if (jsonElement.isJsonArray()) { filterJsonArray(jsonElement.getAsJsonArray()); } else if (jsonElement.isJsonObject()) { filterJsonObject(jsonElement.getAsJsonObject()); }// w ww . j a va2 s . co m }
From source file:com.collaide.fileuploader.requests.notifications.RepoItemsNotificationRequest.java
private JsonArray getJsonNotifications(int userId, long timestamp) { String url = "user/" + String.valueOf(userId) + "/groups/" + String.valueOf(groupId) + "/notify?" + CurrentUser.getAuthParams(); if (timestamp > 0) { //TODO escaper les espaces url = url + "&from_time=2014-08-18 17:58:44";// + toCollaideFormat(timestamp); }/*from w w w . j ava 2 s .c o m*/ ClientResponse response = request(url).get(ClientResponse.class); if (response.getStatus() != 200) { return null; } JsonElement notifications = getResponseToJsonElement(response); return notifications.getAsJsonArray(); }
From source file:com.collaide.fileuploader.requests.repository.RepositoryRequest.java
/** * Get a list of items(files or folders) from the server * * @param response The Request to execute * @return a class <code>Repository</code> containing a list of files and a * list of folders// ww w . ja v a 2 s . com */ private Repository getRepository(JsonElement json) { Repository repo = new Repository(); for (JsonElement jsonElement : json.getAsJsonArray()) { JsonObject repoItem = jsonElement.getAsJsonObject(); if (repoItem.get("is_folder").getAsBoolean()) { repo.getServerFolders().put(repoItem.get("name").getAsString(), gson.fromJson(repoItem, RepoFolder.class)); } else { repo.getServerFiles().put(repoItem.get("md5").getAsString() + repoItem.get("name").getAsString(), gson.fromJson(repoItem, RepoFile.class)); } } return repo; }
From source file:com.collective.celos.ci.testing.fixtures.compare.json.JsonElementConstructor.java
License:Apache License
private JsonElement deepCopy(String path, JsonElement el, Set<String> ignorePaths) { if (el.isJsonArray()) { return deepCopyJsonArray(path, el.getAsJsonArray(), ignorePaths); }//w w w . jav a 2s . c om if (el.isJsonObject()) { return deepCopyJsonObject(path, el.getAsJsonObject(), ignorePaths); } if (el.isJsonPrimitive() || el.isJsonNull()) { return el; } throw new IllegalStateException( "JSONElement should be either Array, Object, Primitive or Null. So you cannot get here"); }