private Set<String> extractContentTagSet(JsonObject talkObject) { if (talkObject.containsKey("tags")) { return talkObject.getJsonArray("tags").stream() .map(JsonObject.class::cast) .filter(tagObject -> !tagObject.getString("value").isEmpty()) .map(tagObject -> tagObject.getString("value")) .collect(Collectors.toSet()); } return new HashSet<>(); }
public static Map<String, Set<CandidatePhrase>> readSeedWordsFromJSONString(String str){ Map<String, Set<CandidatePhrase>> seedWords = new HashMap<>(); JsonReader jsonReader = Json.createReader(new StringReader(str)); JsonObject obj = jsonReader.readObject(); jsonReader.close(); for (String o : obj.keySet()){ seedWords.put(o, new HashSet<>()); JsonArray arr = obj.getJsonArray(o); for(JsonValue v: arr) seedWords.get(o).add(CandidatePhrase.createOrGet(v.toString())); } return seedWords; }
private static String getProblemMessages(JsonObject errorResponse) { StringBuilder problemMessages = new StringBuilder(); String mainProblem = getProblemMessage(errorResponse); if (mainProblem != null) { problemMessages.append(getProblemMessage(errorResponse)); } JsonArray subproblems = errorResponse.getJsonArray(SUBPROBLEMS); if (subproblems != null && subproblems.size() > 0) { problemMessages.append(":"); for (JsonObject subproblem : subproblems.getValuesAs(JsonObject.class)) { problemMessages.append("\n").append(getProblemMessage(subproblem)); } } return problemMessages.toString(); }
/** * Extract a key value from json object that is a list of strings if present. * * @param json JsonObject to read key from * @param key key of the value we want to read * @return List of String value if present * @throws JwtException in case the key is of invalid content */ public static Optional<List<String>> getStrings(JsonObject json, String key) throws JwtException { return Optional.ofNullable(json.getJsonArray(key)) .map(it -> { try { return it.stream().map(it2 -> ((JsonString) it2).getString()).collect(Collectors.toList()); } catch (Exception e) { throw new JwtException("Invalid value. Expecting a string array for key " + key); } }); }
private void importTrackIdSet() { this.trackIdSet = new HashSet<>(); String tracksUrl = conferenceBaseUrl + "/tracks"; LOGGER.debug("Sending a request to: {}", tracksUrl); JsonObject rootObject = readJson(tracksUrl, JsonReader::readObject); JsonArray tracksArray = rootObject.getJsonArray("tracks"); for (int i = 0; i < tracksArray.size(); i++) { trackIdSet.add(tracksArray.getJsonObject(i).getString("id")); } }
public String doNewPhrases(String line) throws Exception { System.out.println("adding new phrases"); ConstantsAndVariables constVars = new ConstantsAndVariables(props, humanLabelClasses.keySet(), humanLabelClasses); JsonReader jsonReader = Json.createReader(new StringReader(line)); JsonObject objarr = jsonReader.readObject(); for(Map.Entry<String, JsonValue> o: objarr.entrySet()){ String label = o.getKey(); Set<CandidatePhrase> seed = new HashSet<>(); JsonArray arr = objarr.getJsonArray(o.getKey()); for(int i = 0; i < arr.size(); i++){ String seedw = arr.getString(i); System.out.println("adding " + seedw + " to seed "); seed.add(CandidatePhrase.createOrGet(seedw)); } seedWords.get(label).addAll(seed); constVars.addSeedWords(label, seed); GetPatternsFromDataMultiClass.runLabelSeedWords(Data.sents, humanLabelClasses.get(label), label, seed, constVars, false); //model.labelWords(label, labelclass, Data.sents, seed); } return "SUCCESS added new phrases"; }
private int changeAnnotation(String line, boolean remove){ int tokensNum = 0; JsonReader jsonReader = Json.createReader(new StringReader(line)); JsonObject objarr = jsonReader.readObject(); for(String label: objarr.keySet()) { JsonObject obj4label = objarr.getJsonObject(label); for(String sentid: obj4label.keySet()){ JsonArray tokenArry = obj4label.getJsonArray(sentid); for(JsonValue tokenid: tokenArry){ tokensNum ++; Data.sents.get(sentid).getTokens().get(Integer.parseInt(tokenid.toString())).set(humanLabelClasses.get(label), remove ? backgroundSymbol: label); } } } return tokensNum; }
for (String st : obj.keySet()){ seedWords.put(st, new HashSet<>()); JsonArray arr = obj.getJsonArray(st); for(int i = 0; i < arr.size(); i++){ String val = arr.getString(i);
private void addKeys(JsonObject jsonObject) { JsonArray keyArray = jsonObject.getJsonArray("keys"); keyArray.forEach(it -> { JsonObject aKey = (JsonObject) it; addKey(Jwk.create(aKey)); }); } }
private void importTalkTypeList() { this.talkTypeIdToTalkTypeMap = new HashMap<>(); List<TalkType> talkTypeList = new ArrayList<>(); String proposalTypeUrl = conferenceBaseUrl + "/proposalTypes"; LOGGER.debug("Sending a request to: {}", proposalTypeUrl); JsonObject rootObject = readJson(proposalTypeUrl, JsonReader::readObject); JsonArray talkTypeArray = rootObject.getJsonArray("proposalTypes"); for (int i = 0; i < talkTypeArray.size(); i++) { JsonObject talkTypeObject = talkTypeArray.getJsonObject(i); String talkTypeId = talkTypeObject.getString("id"); if (talkTypeIdToTalkTypeMap.keySet().contains(talkTypeId)) { LOGGER.warn("Duplicate talk type in {} at index {}.", proposalTypeUrl, i); continue; } TalkType talkType = new TalkType((long) i, talkTypeId); talkType.setCompatibleRoomSet(new HashSet<>()); talkType.setCompatibleTimeslotSet(new HashSet<>()); talkTypeList.add(talkType); talkTypeIdToTalkTypeMap.put(talkTypeId, talkType); } solution.setTalkTypeList(talkTypeList); }
String[] queryAccountContactUrls(AcmeAccount account, boolean staging) throws AcmeException { Assert.checkNotNullParam("account", account); HttpURLConnection connection = sendPostAsGetRequest(account, staging, getAccountUrl(account, staging), null, HttpURLConnection.HTTP_OK); JsonObject jsonResponse = getJsonResponse(connection); JsonArray contactsArray = jsonResponse.getJsonArray(CONTACT); if (contactsArray != null && contactsArray.size() > 0) { List<String> contacts = new ArrayList<>(contactsArray.size()); for (JsonString contact : contactsArray.getValuesAs(JsonString.class)) { contacts.add(contact.getString()); } return contacts.toArray(new String[contacts.size()]); } return null; }
private List<Speaker> extractSpeakerList(JsonObject talkObject, String code, String title) { List<Speaker> speakerList = new ArrayList<>(); String mainSpeakerName = talkObject.getString("mainSpeaker"); if (Arrays.asList(IGNORED_SPEAKER_NAMES).contains(mainSpeakerName)) { return speakerList; } speakerList.add(getSpeakerOrCreateOneIfNull(code, title, mainSpeakerName)); if (talkObject.containsKey("secondarySpeaker")) { String secondarySpeakerName = talkObject.getString("secondarySpeaker"); speakerList.add(getSpeakerOrCreateOneIfNull(code, title, secondarySpeakerName)); } if (talkObject.containsKey("otherSpeakers")) { JsonArray otherSpeakersArray = talkObject.getJsonArray("otherSpeakers"); for (JsonValue otherSpeakerName : otherSpeakersArray) { speakerList.add(getSpeakerOrCreateOneIfNull(code, title, otherSpeakerName.toString().replaceAll("\"", ""))); } } return speakerList; }
/** * Returns an array of RoleInfo corresponding to the JSON serialization returned * by {@link QueueControl#listMessageCounterHistory()}. */ public static DayCounterInfo[] fromJSON(final String jsonString) { JsonObject json = JsonUtil.readJsonObject(jsonString); JsonArray dayCounters = json.getJsonArray("dayCounters"); DayCounterInfo[] infos = new DayCounterInfo[dayCounters.size()]; for (int i = 0; i < dayCounters.size(); i++) { JsonObject counter = (JsonObject) dayCounters.get(i); JsonArray hour = counter.getJsonArray("counters"); long[] hourCounters = new long[24]; for (int j = 0; j < 24; j++) { hourCounters[j] = hour.getInt(j); } DayCounterInfo info = new DayCounterInfo(counter.getString("date"), hourCounters); infos[i] = info; } return infos; }
/** * Returns a {@link Attributes} instance based on the given {@link JsonObject}. * * @param claims a json object with the claims to extract * @return an {@link Attributes} instance with attributes from the given json object */ public static Attributes toAttributes(JsonObject claims) { return claims.entrySet().stream().reduce(new MapAttributes(), (mapAttributes, entry) -> { String claimName = entry.getKey(); JsonValue claimValue = entry.getValue(); if (JsonValue.ValueType.ARRAY.equals(claimValue.getValueType())) { JsonArray jsonArray = claims.getJsonArray(claimName); jsonArray.forEach(arrayValue -> mapAttributes.addLast(claimName, asString(arrayValue))); } else { mapAttributes.addLast(claimName, asString(claimValue)); } return mapAttributes; }, (mapAttributes, mapAttributes2) -> mapAttributes); }
private void importTalkList() { this.talkCodeToTalkMap = new HashMap<>(); solution.setTalkList(new ArrayList<>()); String talksUrl = conferenceBaseUrl + "/talks"; LOGGER.debug("Sending a request to: {}", talksUrl); JsonArray talkArray = readJson(talksUrl, JsonReader::readObject).getJsonObject("talks").getJsonArray("accepted"); for (int i = 0; i < talkArray.size(); i++) { JsonObject talkObject = talkArray.getJsonObject(i); String code = talkObject.getString("id"); String title = talkObject.getString("title"); String talkTypeId = talkObject.getJsonObject("talkType").getString("id"); Set<String> themeTrackSet = extractThemeTrackSet(talkObject, code, title); String language = talkObject.getString("lang"); int audienceLevel = Integer.parseInt(talkObject.getString("audienceLevel").replaceAll("[^0-9]", "")); List<Speaker> speakerList = extractSpeakerList(talkObject, code, title); Set<String> contentTagSet = extractContentTagSet(talkObject); if (!Arrays.asList(IGNORED_TALK_TYPES).contains(talkTypeId)) { createTalk(code, title, talkTypeId, themeTrackSet, language, speakerList, audienceLevel, contentTagSet); } } }
private AcmeChallenge respondToChallenges(AcmeAccount account, boolean staging, JsonObject authorization) throws AcmeException { List<AcmeChallenge> challenges = null; if (authorization.getString(STATUS).equals(PENDING)) { JsonObject identifier = authorization.getJsonObject(IDENTIFIER); JsonArray challengeArray = authorization.getJsonArray(CHALLENGES); challenges = new ArrayList<>(challengeArray.size()); for (JsonObject challenge : challengeArray.getValuesAs(JsonObject.class)) { challenges.add(new AcmeChallenge(AcmeChallenge.Type.forName(challenge.getString(TYPE)), challenge.getString(URL), challenge.getString(TOKEN), identifier.getString(TYPE), identifier.getString(VALUE))); } } if (challenges != null && ! challenges.isEmpty()) { AcmeChallenge selectedChallenge = proveIdentifierControl(account, challenges); try { sendPostRequestWithRetries(account, staging, selectedChallenge.getUrl(), false, getEncodedJson(EMPTY_PAYLOAD), HttpURLConnection.HTTP_OK); return selectedChallenge; } catch (AcmeException e) { cleanupAfterChallenge(account, selectedChallenge); throw e; } } return null; }
metadataBuilder.setWebsiteUrl(websiteUrl); JsonArray caaIdentitiesArray = metadata.getJsonArray(CAA_IDENTITIES); if (caaIdentitiesArray != null) { final List<String> caaIdentities = new ArrayList<>(caaIdentitiesArray.size());
@Override public Address parseAddress(JsonObject json) { Address address = new Address(); JsonObject result = json.getJsonArray("result").getJsonObject(0); if (result.containsKey("streetName")) { address.setStreet(result.getString("streetName")); } if (result.containsKey("city")) { address.setSettlement(result.getString("city")); } if (result.containsKey("state")) { address.setState(result.getString("state")); } if (result.containsKey("countryCode")) { address.setCountry(result.getString("countryCode")); } if (result.containsKey("formatedFull")) { address.setFormattedAddress(result.getString("formatedFull")); } return address; }
@Override public Address parseAddress(JsonObject json) { Address address = new Address(); JsonObject result = json .getJsonObject("geocoding_results") .getJsonArray("RESULTS") .getJsonObject(0); JsonObject resultAddress = result.getJsonObject("ADDRESS"); if (result.containsKey("formatted_address")) { address.setFormattedAddress(result.getString("formatted_address")); } if (resultAddress.containsKey("street_number")) { address.setStreet(resultAddress.getString("street_number")); } if (resultAddress.containsKey("street_name")) { address.setStreet(resultAddress.getString("street_name")); } if (resultAddress.containsKey("locality")) { address.setSettlement(resultAddress.getString("locality")); } if (resultAddress.containsKey("admin_1")) { address.setState(resultAddress.getString("admin_1")); } if (resultAddress.containsKey("country")) { address.setCountry(resultAddress.getString("country")); } return address; }
@Override public Address parseAddress(JsonObject json) { JsonArray result = json.getJsonArray("features"); if (result != null && !result.isEmpty()) { JsonObject location = result.getJsonObject(0).getJsonObject("properties"); Address address = new Address(); address.setCountry("FR"); if (location.containsKey("postcode")) { address.setPostcode(location.getString("postcode")); } if (location.containsKey("context")) { address.setDistrict(location.getString("context")); } if (location.containsKey("name")) { address.setStreet(location.getString("name")); } if (location.containsKey("city")) { address.setSettlement(location.getString("city")); } if (location.containsKey("housenumber")) { address.setHouse(location.getString("housenumber")); } if (location.containsKey("label")) { address.setFormattedAddress(location.getString("label")); } return address; } return null; }