Java Code Examples for javax.json.JsonArray#forEach()

The following examples show how to use javax.json.JsonArray#forEach() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomersResource.java    From problematic-microservices with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@PUT
@Path("batchadd/")
@Consumes(MediaType.APPLICATION_JSON)
public Response putUsers(JsonObject jsonEntity) {
	JsonArray jsonArray = jsonEntity.getJsonArray("customers");
	final JsonArrayBuilder result = Json.createArrayBuilder();
	jsonArray.forEach((jsonValue) -> {
		try {
			Customer c = createCustomer(jsonValue.asJsonObject());
			result.add(c.toJSon());
		} catch (ValidationException e) {
			result.add(Utils.errorAsJSon(e));
		}
	});
	return Response.accepted(result.build()).build();
}
 
Example 2
Source File: CommunicationManager.java    From vicinity-gateway-api with GNU General Public License v3.0 5 votes vote down vote up
/**
 * getThingDescriptions - Return all pages of the thing descriptions of IoT object(s).
 * 
 * @param sourceObjectId 
 * 
 * @return The list of thing descriptions 
 */
public Representation getThingDescriptions(String sourceObjectId) {
	
	if (sourceObjectId == null || sourceObjectId.isEmpty()) {
		logger.warning("Method parameter sourceObjectId can't be null nor empty.");
		
		return null;
	}
	
	JsonObjectBuilder mainObjectBuilder = Json.createObjectBuilder();
	JsonArrayBuilder mainArrayBuilder = Json.createArrayBuilder();
	
	Representation r = null;
	int i = 0;
	
	do {
		
		r = getThingDescriptions(sourceObjectId, i++);
		
		if (r == null) {
			break;
		}
		
		JsonArray tds = parseThingDescriptionsFromRepresentation(r);
		tds.forEach(item -> {
			mainArrayBuilder.add(item);
		});
	}
	while (true);
	
	mainObjectBuilder.add(ATTR_TDS, mainArrayBuilder);
	JsonObject json = mainObjectBuilder.build();
	
	return new JsonRepresentation(json.toString());
}
 
Example 3
Source File: StanfordLoader.java    From NLIWOD with GNU Affero General Public License v3.0 4 votes vote down vote up
public static List<IQuestion> load(final InputStream is) {
	List<IQuestion> output = new ArrayList<>();
	JsonReader jsonReader = Json.createReader(is);
	JsonObject mainJsonObject = jsonReader.readObject();
	JsonArray dataArray = mainJsonObject.getJsonArray("data");
	log.info("Number of WikiArticles " + dataArray.size());
	dataArray.forEach(article -> {
		JsonArray contexts = ((JsonObject) article).getJsonArray("paragraphs");
		contexts.forEach(paragraph -> {
			JsonObject jsonObject = (JsonObject) paragraph;
			JsonArray qas = jsonObject.getJsonArray("qas");
			qas.forEach(x -> {
				IQuestion q = new Question();
				JsonString question = ((JsonObject) x).getJsonString("question");
				// FIXME Micha, geht das hier nicht schöner mit Java 8?
				HashMap<String, String> map = new HashMap<>();
				map.put("en", question.getString());
				q.setLanguageToQuestion(map);

				JsonString id = ((JsonObject) x).getJsonString("id");
				q.setId(id.getString());

				JsonArray answers = ((JsonObject) x).getJsonArray("answers");
				HashSet<String> goldenAnswers = new HashSet<>();
				answers.forEach(y -> {
					JsonString text = ((JsonObject) y).getJsonString("text");
					if (text == null) {
						log.info("id: " + id.getString() + " has answertext : NULL");

					}
					goldenAnswers.add(text != null ? text.getString().trim() : null);
				});
				// if (goldenAnswers.size() > 1) {
				// goldenAnswers.forEach(iterator -> log.debug(iterator));
				// }
				q.setGoldenAnswers(goldenAnswers);
				output.add(q);
			});
		});

	});
	log.debug("Number of Questions: " + output.size());

	List<IQuestion> result = output.stream().filter(x -> x.getGoldenAnswers().size() > 1).collect(Collectors.toList());
	log.debug("Number of Questions with more than one answer: " + result.size());

	return output;
}