List of usage examples for javax.json JsonArray size
int size();
From source file:org.apache.johnzon.maven.plugin.ExampleToModelMojo.java
private void handleArray(final Writer writer, final String prefix, final Map<String, JsonObject> nestedTypes, final JsonValue value, final String jsonField, final String fieldName, final int arrayLevel, final Collection<String> imports) throws IOException { final JsonArray array = JsonArray.class.cast(value); if (array.size() > 0) { // keep it simple for now - 1 level, we can have an awesome recursive algo later if needed final JsonValue jsonValue = array.get(0); switch (jsonValue.getValueType()) { case OBJECT: final String javaName = toJavaName(fieldName); nestedTypes.put(javaName, JsonObject.class.cast(jsonValue)); fieldGetSetMethods(writer, jsonField, fieldName, javaName, prefix, arrayLevel, imports); break; case TRUE: case FALSE: fieldGetSetMethods(writer, jsonField, fieldName, "Boolean", prefix, arrayLevel, imports); break; case NUMBER: fieldGetSetMethods(writer, jsonField, fieldName, "Double", prefix, arrayLevel, imports); break; case STRING: fieldGetSetMethods(writer, jsonField, fieldName, "String", prefix, arrayLevel, imports); break; case ARRAY: handleArray(writer, prefix, nestedTypes, jsonValue, jsonField, fieldName, arrayLevel + 1, imports); break; case NULL: default:/*w w w . j av a 2 s . c o m*/ throw new UnsupportedOperationException("Unsupported " + value + "."); } } else { getLog().warn("Empty arrays are ignored"); } }
From source file:org.bsc.confluence.rest.AbstractRESTConfluenceService.java
protected Stream<JsonObject> mapToStream(Response res) { final ResponseBody body = res.body(); try (Reader r = body.charStream()) { final JsonReader rdr = Json.createReader(r); final JsonObject root = rdr.readObject(); final Stream.Builder<JsonObject> stream = Stream.builder(); // Check for Array if (root.containsKey("results")) { final JsonArray results = root.getJsonArray("results"); if (results != null) { for (int ii = 0; ii < results.size(); ++ii) stream.add(results.getJsonObject(ii)); }// ww w. j a v a 2s . c om } else { stream.add(root); } return stream.build(); } catch (IOException | JsonParsingException e) { throw new Error(e); } }
From source file:org.fuin.esmp.Downloads.java
/** * Loads the data from the JSON download versions file. * /*from w w w .ja va2 s . c om*/ * @throws IOException * Parsing the event store version file failed. */ public final void parse() throws IOException { final Reader reader = new FileReader(jsonDownloadsFile); try { final JsonReader jsonReader = Json.createReader(reader); final JsonArray osArray = jsonReader.readArray(); for (int i = 0; i < osArray.size(); i++) { final JsonObject osObj = (JsonObject) osArray.get(i); final String os = osObj.getString("os"); final String currentVersion = osObj.getString("currentVersion"); final JsonArray downloadsArray = osObj.getJsonArray("downloads"); final List<DownloadVersion> versions = new ArrayList<>(); for (int j = 0; j < downloadsArray.size(); j++) { final JsonObject downloadObj = (JsonObject) downloadsArray.get(j); final String version = downloadObj.getString("version"); final String url = downloadObj.getString("url"); versions.add(new DownloadVersion(version, url)); } Collections.sort(versions); osList.add(new DownloadOS(os, currentVersion, versions)); } Collections.sort(osList); } finally { reader.close(); } for (final DownloadOS os : osList) { LOG.info("Latest '" + os + "': " + os.getLatestVersion() + " (Versions: " + os.getVersions().size() + ")"); } }
From source file:org.grogshop.services.endpoints.impl.ShopItemsServiceImpl.java
@Override public Response newItem(@NotNull @FormParam("user_id") Long userId, @NotNull @FormParam("club_id") Long clubId, @NotNull @FormParam("type") String type, @NotNull @NotEmpty @FormParam("name") String name, @NotNull @NotEmpty @FormParam("description") String description, @NotNull @NotEmpty @FormParam("tags") String tags, @FormParam("minPrice") String minPrice, @FormParam("maxPrice") String maxPrice) throws ServiceException { JsonReader reader = Json.createReader(new ByteArrayInputStream(tags.getBytes())); JsonArray array = reader.readArray(); reader.close();/*from w ww . j a va 2s . co m*/ List<String> interestsList = new ArrayList<String>(); if (array != null) { for (int i = 0; i < array.size(); i++) { log.info("Tag[" + i + "]: " + array.getJsonObject(i).getString("text")); interestsList.add(array.getJsonObject(i).getString("text")); } } Long newItem = itemsService.newItem(userId, clubId, type, name, description, interestsList, (minPrice == null) ? new BigDecimal(0) : new BigDecimal(minPrice), (maxPrice == null) ? new BigDecimal(0) : new BigDecimal(maxPrice)); return Response.ok(newItem).build(); }
From source file:org.grogshop.services.endpoints.impl.ShopUserProfileServiceImpl.java
public Response setInterests(@NotNull @PathParam("user_id") Long user_id, @FormParam("interests") String interests) throws ServiceException { log.info("Storing from the database: (" + user_id + ") " + interests); if (interests != null) { JsonReader reader = Json.createReader(new ByteArrayInputStream(interests.getBytes())); JsonArray array = reader.readArray(); reader.close();//from w w w. j ava 2s. c o m List<Interest> interestsList = new ArrayList<Interest>(array.size()); if (array != null) { for (int i = 0; i < array.size(); i++) { log.info("Interest[" + i + "]: " + array.getJsonObject(i).getString("name")); interestsList.add(interestService.get(array.getJsonObject(i).getString("name"))); } } profileService.setInterests(user_id, interestsList); } return Response.ok().build(); }
From source file:org.hyperledger.fabric.sdk.ChaincodeCollectionConfiguration.java
Collection.CollectionConfigPackage parse(JsonArray jsonConfig) throws ChaincodeCollectionConfigurationException { Collection.CollectionConfigPackage.Builder colcofbuilder = Collection.CollectionConfigPackage.newBuilder(); for (int i = jsonConfig.size() - 1; i > -1; --i) { Collection.StaticCollectionConfig.Builder ssc = Collection.StaticCollectionConfig.newBuilder(); JsonValue j = jsonConfig.get(i); if (j.getValueType() != JsonValue.ValueType.OBJECT) { throw new ChaincodeCollectionConfigurationException(format( "Expected StaticCollectionConfig to be Object type but got: %s", j.getValueType().name())); }// w w w . jav a 2s . c o m JsonObject jsonObject = j.asJsonObject(); JsonObject scf = getJsonObject(jsonObject, "StaticCollectionConfig"); // oneof .. may have different values in the future ssc.setName(getJsonString(scf, "name")).setBlockToLive(getJsonLong(scf, "blockToLive")) .setMaximumPeerCount(getJsonInt(scf, "maximumPeerCount")) .setMemberOrgsPolicy(Collection.CollectionPolicyConfig.newBuilder() .setSignaturePolicy(parseSignaturePolicyEnvelope(scf)).build()) .setRequiredPeerCount(getJsonInt(scf, "requiredPeerCount")); colcofbuilder .addConfig(Collection.CollectionConfig.newBuilder().setStaticCollectionConfig(ssc).build()); } return colcofbuilder.build(); }
From source file:org.hyperledger.fabric_ca.sdk.HFCAAffiliation.java
private void generateResponse(JsonObject result) { if (result.containsKey("name")) { this.name = result.getString("name"); }//from w w w. ja v a 2s.c o m if (result.containsKey("affiliations")) { JsonArray affiliations = result.getJsonArray("affiliations"); if (affiliations != null && !affiliations.isEmpty()) { for (int i = 0; i < affiliations.size(); i++) { JsonObject aff = affiliations.getJsonObject(i); this.childHFCAAffiliations.add(new HFCAAffiliation(aff)); } } } if (result.containsKey("identities")) { JsonArray ids = result.getJsonArray("identities"); if (ids != null && !ids.isEmpty()) { for (int i = 0; i < ids.size(); i++) { JsonObject id = ids.getJsonObject(i); HFCAIdentity hfcaID = new HFCAIdentity(id); this.identities.add(hfcaID); } } } }
From source file:org.hyperledger.fabric_ca.sdk.HFCAIdentity.java
/** * read retrieves a specific identity//www. jav a2 s .c om * * @param registrar The identity of the registrar (i.e. who is performing the registration). * @return statusCode The HTTP status code in the response * @throws IdentityException if retrieving an identity fails. * @throws InvalidArgumentException Invalid (null) argument specified */ public int read(User registrar) throws IdentityException, InvalidArgumentException { if (registrar == null) { throw new InvalidArgumentException("Registrar should be a valid member"); } String readIdURL = ""; try { readIdURL = HFCA_IDENTITY + "/" + enrollmentID; logger.debug(format("identity url: %s, registrar: %s", readIdURL, registrar.getName())); JsonObject result = client.httpGet(readIdURL, registrar); statusCode = result.getInt("statusCode"); if (statusCode < 400) { type = result.getString("type"); maxEnrollments = result.getInt("max_enrollments"); affiliation = result.getString("affiliation"); JsonArray attributes = result.getJsonArray("attrs"); Collection<Attribute> attrs = new ArrayList<Attribute>(); if (attributes != null && !attributes.isEmpty()) { for (int i = 0; i < attributes.size(); i++) { JsonObject attribute = attributes.getJsonObject(i); Attribute attr = new Attribute(attribute.getString("name"), attribute.getString("value"), attribute.getBoolean("ecert", false)); attrs.add(attr); } } this.attrs = attrs; logger.debug(format("identity url: %s, registrar: %s done.", readIdURL, registrar)); } this.deleted = false; return statusCode; } catch (HTTPException e) { String msg = format("[Code: %d] - Error while getting user '%s' from url '%s': %s", e.getStatusCode(), getEnrollmentId(), readIdURL, e.getMessage()); IdentityException identityException = new IdentityException(msg, e); logger.error(msg); throw identityException; } catch (Exception e) { String msg = format("Error while getting user '%s' from url '%s': %s", enrollmentID, readIdURL, e.getMessage()); IdentityException identityException = new IdentityException(msg, e); logger.error(msg); throw identityException; } }
From source file:org.hyperledger.fabric_ca.sdk.HFCAIdentity.java
private void getHFCAIdentity(JsonObject result) { type = result.getString("type"); if (result.containsKey("secret")) { this.secret = result.getString("secret"); }//from w w w . j a v a 2 s . c o m maxEnrollments = result.getInt("max_enrollments"); affiliation = result.getString("affiliation"); JsonArray attributes = result.getJsonArray("attrs"); Collection<Attribute> attrs = new ArrayList<Attribute>(); if (attributes != null && !attributes.isEmpty()) { for (int i = 0; i < attributes.size(); i++) { JsonObject attribute = attributes.getJsonObject(i); Attribute attr = new Attribute(attribute.getString("name"), attribute.getString("value"), attribute.getBoolean("ecert", false)); attrs.add(attr); } } this.attrs = attrs; }
From source file:org.jboss.set.aphrodite.stream.services.json.StreamComponentJsonParser.java
private static List<String> getContacts(JsonObject json) { final JsonArray contactsArray = json.getJsonArray(JSON_CONTACTS); final List<String> contacts = new ArrayList<>(contactsArray.size()); for (int index = 0; index < contactsArray.size(); index++) { contacts.add(contactsArray.getString(index)); }// w ww .ja v a2 s .c o m return contacts; }