List of usage examples for com.google.gson JsonObject entrySet
public Set<Map.Entry<String, JsonElement>> entrySet()
From source file:com.nfa.drs.DataReductionPanel.java
private void importThermalBias(Path mcFile) { try {//w ww.j av a 2 s.co m String json = Files.readAllLines(mcFile).stream().reduce("", (String first, String second) -> first + System.lineSeparator() + second); JsonObject map = new JsonParser().parse(json).getAsJsonObject(); this.thermalBiasSettings.clear(); map.entrySet().stream().forEach((Entry<String, JsonElement> entry) -> { String name = entry.getKey(); JsonElement element = entry.getValue(); try { ThermalBiasSettings tbs = DataReducer.GSON.fromJson(element, ThermalBiasSettings.class); this.thermalBiasSettings.put(name, tbs); } catch (Exception ex) { } }); this.thermalBiasItemEvent(null); this.resizeThermalBiasView(); } catch (IOException ex) { JOptionPane.showMessageDialog(this, "There was an error while importing the thermal bias.", "Import Error", JOptionPane.ERROR_MESSAGE); } this.resizeThermalBiasView(); }
From source file:com.northernwall.hadrian.details.simple.SimpleHostDetailsHelper.java
License:Apache License
private void getDetailsFromUrl(Host host, String url, List<GetPairData> pairs) { Request httpRequest = new Request.Builder().url(url).build(); try {// w ww .ja va2 s . c o m Response resp = client.newCall(httpRequest).execute(); try (Reader reader = new InputStreamReader(resp.body().byteStream())) { if (resp.isSuccessful()) { JsonElement jsonElement = parser.parse(reader); if (jsonElement.isJsonObject()) { JsonObject jsonObject = jsonElement.getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { processAttribute(null, entry, pairs); } } } else { logger.warn("Call to {} failed with code {}", url, resp.code()); } } } catch (Exception ex) { logger.warn("Error while getting secondary host details for {}, error {}", host.getHostName(), ex.getMessage()); } }
From source file:com.northernwall.hadrian.details.simple.SimpleHostDetailsHelper.java
License:Apache License
private void processAttribute(String prefix, Map.Entry<String, JsonElement> entry, List<GetPairData> pairs) { if (entry.getValue().isJsonPrimitive()) { addPair(prefix, entry.getKey(), entry.getValue().getAsString(), pairs); } else if (entry.getValue().isJsonArray()) { StringBuffer buffer = null; JsonArray jsonArray = entry.getValue().getAsJsonArray(); for (int i = 0; i < jsonArray.size(); i++) { JsonElement arrayElement = jsonArray.get(i); if (arrayElement.isJsonPrimitive()) { if (buffer == null) { buffer = new StringBuffer(arrayElement.getAsString()); } else { buffer.append(", "); buffer.append(arrayElement.getAsString()); }//from w w w . j a v a 2 s . co m } } if (buffer != null) { addPair(prefix, entry.getKey(), buffer.toString(), pairs); } } else if (entry.getValue().isJsonObject()) { JsonObject jsonObject = entry.getValue().getAsJsonObject(); for (Map.Entry<String, JsonElement> innerEntry : jsonObject.entrySet()) { processAttribute(entry.getKey(), innerEntry, pairs); } } }
From source file:com.ontotext.s4.SBTDemo.parse.JsonToRDF.java
License:Apache License
/** * This method should create/*from w ww.j a va 2s. c o m*/ * * @param jsonLine * @param fileSubject * @return */ public List<String[]> parse(String jsonLine, String fileSubject) throws Exception { List<String[]> hashList = new LinkedList<String[]>(); JsonElement jelement = new JsonParser().parse(jsonLine); JsonObject jobject = jelement.getAsJsonObject(); String text = null; if (jobject.has(textJson)) { text = jobject.getAsJsonPrimitive(textJson).getAsString(); } if (jobject.has(entity)) { JsonObject entities = jobject.getAsJsonObject(entity); for (Iterator<Entry<String, JsonElement>> iterator = entities.entrySet().iterator(); iterator .hasNext();) { Entry<String, JsonElement> element = iterator.next(); JsonArray jsonArrayOfEntities = element.getValue().getAsJsonArray(); for (JsonElement jsonElement : jsonArrayOfEntities) { JsonObject annotation = jsonElement.getAsJsonObject(); try { String[] fileMentionsArray = new String[] { fileSubject, MENTIONS_URI, annotation.getAsJsonPrimitive(inst).getAsString() }; String[] instanceTypeArray = new String[] { annotation.getAsJsonPrimitive(inst).getAsString(), RDF_TYPE_URI, annotation.getAsJsonPrimitive(classProperty).getAsString() }; String[] instanceLabelArray = new String[] { annotation.getAsJsonPrimitive(inst).getAsString(), LABEL_URI, annotation.getAsJsonPrimitive(stringProperty).getAsString() }; String[] classLableArray = new String[] { annotation.getAsJsonPrimitive(classProperty).getAsString(), LABEL_URI, annotation.getAsJsonPrimitive(type).getAsString().replaceAll("_", " ") }; String[] stringAbstractText = new String[] { fileSubject, ABSTRACT_TEXT_URI, text }; hashList.add(fileMentionsArray); hashList.add(instanceTypeArray); hashList.add(classLableArray); hashList.add(instanceLabelArray); hashList.add(stringAbstractText); } catch (Exception e) { logger.error(e); } } } } return hashList; }
From source file:com.optimizely.ab.config.parser.GsonHelpers.java
License:Apache License
private static Map<String, String> parseForcedVariations(JsonObject forcedVariationJson) { Map<String, String> userIdToVariationKeyMap = new HashMap<String, String>(); Set<Map.Entry<String, JsonElement>> entrySet = forcedVariationJson.entrySet(); for (Map.Entry<String, JsonElement> entry : entrySet) { userIdToVariationKeyMap.put(entry.getKey(), entry.getValue().getAsString()); }/* ww w . jav a 2s . c o m*/ return userIdToVariationKeyMap; }
From source file:com.orange.homenap.gson.ConstraintAdapter.java
License:Open Source License
@Override public Constraint deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException { Constraint constraint = new Constraint(); JsonObject jsonObject = jsonElement.getAsJsonObject(); for (final Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { constraint.setName(entry.getKey()); constraint.setValue(entry.getValue().toString()); }//from w ww .j av a2 s. c o m return constraint; }
From source file:com.palantir.gerrit.gerritci.servlets.ConfigServlet.java
License:Apache License
@Override protected void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { if (currentUser.get().getCapabilities().canAdministrateServer() == false) { res.setStatus(403);/* w w w.j a va2 s. c om*/ return; } /* * The actual parameters we send are encoded into a JSON object such * that they are contained in an object under the entry "f". The other * top-level keys seem to be useless. In addition, each key in the * parameters object has ":" prefixed to whatever it is the key is * actually named. Thus, by stripping the first character away from each * key, we arrive at a sane JSONObject of request parameters. Example: * {"b": [], "f": {":projectName": "name", ":verifyBranchRegex": * * ".*"}} */ JsonObject requestBody = (JsonObject) (new JsonParser()).parse(CharStreams.toString(req.getReader())); requestBody = requestBody.get("f").getAsJsonObject(); File confFile = new File(sitePaths.etc_dir, "gerrit-ci.config"); FileBasedConfig cfg = new FileBasedConfig(confFile, FS.DETECTED); try { cfg.load(); } catch (ConfigInvalidException e) { logger.error("Received PUT request. Error loading gerrit-ci.config file:", e); } cfg.clear(); for (Entry<String, JsonElement> entry : requestBody.entrySet()) { // substring(1) removes the ':' character that precedes each key in the requestBody cfg.setString("Settings", "Jenkins", entry.getKey().substring(1), entry.getValue().getAsString()); } cfg.save(); // the gerrit-ci.config file starts off with permissions -rw-rw-r-- cfg.getFile().setReadOnly(); // make unwritable by making file read only for everyone -r--r--r-- cfg.getFile().setReadable(false, false); // Make the file unreadable (readable=false and ownerOnly=false) cfg.getFile().setReadable(true, true); // Sets the file as readable for only the owner (readable=true and ownerOnly=true) -r-------- res.setStatus(200); res.setContentType("text/plain"); }
From source file:com.paranoid.gerrit.objects.FileInfoList.java
License:Apache License
public static FileInfoList deserialize(JsonObject object) { List<FileInfo> newList = new ArrayList<>(); Set<Map.Entry<String, JsonElement>> entries = object.entrySet(); for (Map.Entry<String, JsonElement> entry : entries) { newList.add(FileInfo.deserialise(entry.getKey(), entry.getValue().getAsJsonObject())); }/*from w w w .ja v a 2 s.c o m*/ return new FileInfoList(newList); }
From source file:com.paranoid.gerrit.objects.Projects.java
License:Apache License
@Override public Projects deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext deserializationContext) throws JsonParseException { JsonObject json = jsonElement.getAsJsonObject(); for (Map.Entry<String, JsonElement> project : json.entrySet()) { String path = project.getKey(); JsonObject details = project.getValue().getAsJsonObject(); String kind = details.get("kind").getAsString(); String id = details.get("id").getAsString(); projects.add(new Project(path, kind, id)); }//from w w w. j a v a2s . c o m return this; }
From source file:com.pearson.pdn.learningstudio.content.ContentService.java
License:Apache License
/** * Get content for a specific item in a course with * getItem(courseId, itemId)//from www.j a va2s .co m * by following the links to the item itself * and next to the contentUrl * using OAuth1 or OAuth2 as a student, teacher, or teaching assistant * * @param courseId ID of the course * @param itemId ID of the item * @return Response object with details of status and content * @throws IOException */ public Response getItemContent(String courseId, String itemId) throws IOException { Response response = getItem(courseId, itemId); if (response.isError()) { return response; } // should only be one item here, but it is returned in an array for some reason String courseItemsJson = response.getContent(); JsonObject json = this.jsonParser.parse(courseItemsJson).getAsJsonObject(); JsonArray items = json.get("items").getAsJsonArray(); // again, only one element expected here... Iterator<JsonElement> itemIterator = items.iterator(); if (itemIterator.hasNext()) { JsonObject item = itemIterator.next().getAsJsonObject(); JsonArray links = item.get("links").getAsJsonArray(); for (Iterator<JsonElement> linkIter = links.iterator(); linkIter.hasNext();) { JsonObject link = linkIter.next().getAsJsonObject(); JsonElement title = link.get("title"); // rel on link varies, so identify self by missing title if (title == null) { String relativeUrl = this.getRelativePath(link.get("href").getAsString()); response = doMethod(HttpMethod.GET, relativeUrl, NO_CONTENT); if (response.isError()) { return response; } json = this.jsonParser.parse(response.getContent()).getAsJsonObject(); String itemType = json.entrySet().iterator().next().getKey(); json = json.get(itemType).getAsJsonArray().get(0).getAsJsonObject(); // single element wrapped in an array String contentUrl = json.get("contentUrl").getAsString(); relativeUrl = this.getRelativePath(contentUrl); return doMethod(HttpMethod.GET, relativeUrl, NO_CONTENT); } } } // should never get here throw new RuntimeException("No item content path found"); }