Search in sources :

Example 16 with JsonArrayBuilder

use of javax.json.JsonArrayBuilder in project Payara by payara.

the class GetResultListJsonProvider method getResourcesLinks.

private JsonArray getResourcesLinks(List<Dom> proxyList) {
    JsonArrayBuilder array = Json.createArrayBuilder();
    String elementName;
    for (Map.Entry<String, String> link : getResourceLinks(proxyList).entrySet()) {
        array.add(link.getValue());
    }
    return array.build();
}
Also used : JsonArrayBuilder(javax.json.JsonArrayBuilder) Map(java.util.Map)

Example 17 with JsonArrayBuilder

use of javax.json.JsonArrayBuilder in project iaf by ibissource.

the class Json2Xml method getSubstitutedChild.

@Override
protected JsonValue getSubstitutedChild(JsonValue node, String childName) {
    if (!sp.hasSubstitutionsFor(getContext(), childName)) {
        return null;
    }
    Object substs = sp.getSubstitutionsFor(getContext(), childName);
    if (substs == null) {
        substs = "{}";
    }
    if (substs instanceof List) {
        JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
        for (Object item : (List) substs) {
            arrayBuilder.add(item.toString());
        }
        return arrayBuilder.build();
    }
    JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
    objectBuilder.add(childName, substs.toString());
    return objectBuilder.build().getJsonString(childName);
}
Also used : JsonObject(javax.json.JsonObject) List(java.util.List) LinkedList(java.util.LinkedList) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 18 with JsonArrayBuilder

use of javax.json.JsonArrayBuilder in project iaf by ibissource.

the class XmlTypeToJsonSchemaConverter method buildObject.

private void buildObject(JsonObjectBuilder builder, XSObjectList particles, XSObjectList attributeUses, String textAttribute, XSTypeDefinition baseType) {
    builder.add("type", "object");
    builder.add("additionalProperties", false);
    JsonObjectBuilder propertiesBuilder = Json.createObjectBuilder();
    List<String> requiredProperties = new ArrayList<String>();
    if (attributeUses != null) {
        for (int i = 0; i < attributeUses.getLength(); i++) {
            XSAttributeUse attributeUse = (XSAttributeUse) attributeUses.get(i);
            XSAttributeDeclaration attributeDecl = attributeUse.getAttrDeclaration();
            propertiesBuilder.add(attributePrefix + attributeDecl.getName(), getDefinition(attributeDecl.getTypeDefinition(), true));
        }
    }
    if (textAttribute != null && ((attributeUses != null && attributeUses.getLength() > 0) || (particles != null && particles.getLength() > 0))) {
        JsonObject elementType = baseType != null ? getDefinition(baseType, true) : Json.createObjectBuilder().add("type", "string").build();
        propertiesBuilder.add(textAttribute, elementType);
    }
    if (particles != null) {
        for (int i = 0; i < particles.getLength(); i++) {
            XSParticle childParticle = (XSParticle) particles.item(i);
            if (log.isTraceEnabled())
                log.trace("childParticle [" + i + "][" + ToStringBuilder.reflectionToString(childParticle, ToStringStyle.MULTI_LINE_STYLE) + "]");
            XSTerm childTerm = childParticle.getTerm();
            if (childTerm instanceof XSElementDeclaration) {
                XSElementDeclaration elementDeclaration = (XSElementDeclaration) childTerm;
                String elementName = elementDeclaration.getName();
                if (elementName != null && childParticle.getMinOccurs() != 0) {
                    requiredProperties.add(elementName);
                }
            }
            handleParticle(propertiesBuilder, childParticle, null, true);
        }
    }
    builder.add("properties", propertiesBuilder.build());
    if (requiredProperties.size() > 0) {
        JsonArrayBuilder requiredPropertiesBuilder = Json.createArrayBuilder();
        for (String requiredProperty : requiredProperties) {
            requiredPropertiesBuilder.add(requiredProperty);
        }
        builder.add("required", requiredPropertiesBuilder.build());
    }
}
Also used : XSParticle(org.apache.xerces.xs.XSParticle) XSTerm(org.apache.xerces.xs.XSTerm) ArrayList(java.util.ArrayList) XSElementDeclaration(org.apache.xerces.xs.XSElementDeclaration) XSAttributeDeclaration(org.apache.xerces.xs.XSAttributeDeclaration) JsonObject(javax.json.JsonObject) XSAttributeUse(org.apache.xerces.xs.XSAttributeUse) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 19 with JsonArrayBuilder

use of javax.json.JsonArrayBuilder in project iaf by ibissource.

the class XmlTypeToJsonSchemaConverter method applyFacet.

private void applyFacet(XSSimpleTypeDefinition simpleTypeDefinition, JsonObjectBuilder builder, String key, short facet) {
    if (simpleTypeDefinition.getFacet(facet) != null) {
        String lexicalFacetValue = simpleTypeDefinition.getLexicalFacetValue(facet);
        if (lexicalFacetValue != null) {
            switch(facet) {
                case XSSimpleTypeDefinition.FACET_MAXINCLUSIVE:
                case XSSimpleTypeDefinition.FACET_MININCLUSIVE:
                case XSSimpleTypeDefinition.FACET_MAXEXCLUSIVE:
                case XSSimpleTypeDefinition.FACET_MINEXCLUSIVE:
                case XSSimpleTypeDefinition.FACET_MAXLENGTH:
                case XSSimpleTypeDefinition.FACET_MINLENGTH:
                    /*
							Not sure about this.. 
	
							simpleTypeDefinition.getLexicalFacetValue(facet) returns a numeric value as string
							if value > MAX_INT, Integer.parseInt(value) will throw NumberFormatException
	
							currently this exception is catched and retried as Long.ParseLong(value)
							but what if this throws NumberFormatException?
	
							how to deal with this properly?
							-----
							UPDATE:
							Tried parsing as long and logging the value when couldn't parse, appears to be a 20 digit numeric value
							which would require to use BigInteger

							What is the best method to do this? Try and catch int, long & then bigint or directly to big int?
						*/
                    try {
                        builder.add(key, Integer.parseInt(lexicalFacetValue));
                    } catch (NumberFormatException nfe) {
                        log.warn("Couldn't parse value [" + lexicalFacetValue + "] as Integer... retrying as Long");
                        try {
                            builder.add(key, Long.parseLong(lexicalFacetValue));
                        } catch (NumberFormatException nfex) {
                            log.warn("Couldn't parse value [" + lexicalFacetValue + "] as Long... retrying as BigInteger");
                            try {
                                builder.add(key, new BigInteger(lexicalFacetValue));
                            } catch (NumberFormatException nfexx) {
                                log.warn("Couldn't parse value [" + lexicalFacetValue + "] as BigInteger");
                            }
                        }
                    }
                    break;
                default:
                    // hmm never reaches this block?
                    log.debug("Setting value [" + lexicalFacetValue + "] as String for facet [" + simpleTypeDefinition.getFacet(facet) + "]");
                    builder.add(key, lexicalFacetValue);
                    break;
            }
        } else if (facet == XSSimpleTypeDefinition.FACET_PATTERN || facet == XSSimpleTypeDefinition.FACET_ENUMERATION) {
            XSObjectList multiValuedFacets = simpleTypeDefinition.getMultiValueFacets();
            for (int i = 0; i < multiValuedFacets.getLength(); i++) {
                XSMultiValueFacet multiValuedFacet = (XSMultiValueFacet) multiValuedFacets.item(i);
                if (log.isTraceEnabled())
                    log.trace("Inspecting single multi valued facet [" + multiValuedFacet + "] which is named [" + multiValuedFacet.getName() + "] which is of type [" + multiValuedFacet.getType() + "]");
                if (log.isTraceEnabled())
                    log.trace("Inspecting multiValuedFacet.getLexicalFacetValues() for [" + multiValuedFacet.getName() + "] which has value of [" + multiValuedFacet.getLexicalFacetValues() + "]");
                if (log.isTraceEnabled())
                    log.trace("Inspecting multiValuedFacet.getEnumerationValues() for [" + multiValuedFacet.getName() + "] which has value of [" + multiValuedFacet.getEnumerationValues() + "]");
                if (log.isTraceEnabled())
                    log.trace("Inspecting multiValuedFacet.getFacetKind() == enum for [" + multiValuedFacet.getName() + "] which has value of [" + (multiValuedFacet.getFacetKind() == XSSimpleTypeDefinition.FACET_ENUMERATION) + "]");
                if (log.isTraceEnabled())
                    log.trace("Inspecting multiValuedFacet.getFacetKind() == pattern for [" + multiValuedFacet.getName() + "] which has value of [" + (multiValuedFacet.getFacetKind() == XSSimpleTypeDefinition.FACET_PATTERN) + "]");
                if (facet == multiValuedFacet.getFacetKind()) {
                    StringList lexicalFacetValues = multiValuedFacet.getLexicalFacetValues();
                    if (facet == XSSimpleTypeDefinition.FACET_ENUMERATION) {
                        JsonArrayBuilder enumBuilder = Json.createArrayBuilder();
                        for (int x = 0; x < lexicalFacetValues.getLength(); x++) {
                            lexicalFacetValue = lexicalFacetValues.item(x);
                            enumBuilder.add(lexicalFacetValue);
                        }
                        builder.add(key, enumBuilder.build());
                    } else if (facet == XSSimpleTypeDefinition.FACET_PATTERN) {
                        builder.add(key, lexicalFacetValues.item(0));
                    }
                }
            }
        }
    }
}
Also used : XSObjectList(org.apache.xerces.xs.XSObjectList) XSMultiValueFacet(org.apache.xerces.xs.XSMultiValueFacet) StringList(org.apache.xerces.xs.StringList) BigInteger(java.math.BigInteger) JsonArrayBuilder(javax.json.JsonArrayBuilder)

Example 20 with JsonArrayBuilder

use of javax.json.JsonArrayBuilder in project jcabi-github by jcabi.

the class RtIssueLabels method add.

@Override
public void add(final Iterable<String> labels) throws IOException {
    JsonArrayBuilder builder = Json.createArrayBuilder();
    for (final String label : labels) {
        builder = builder.add(label);
    }
    final JsonStructure json = builder.build();
    this.request.method(Request.POST).body().set(json).back().fetch().as(RestResponse.class).assertStatus(HttpURLConnection.HTTP_OK).as(JsonResponse.class).json().readArray();
}
Also used : JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonStructure(javax.json.JsonStructure)

Aggregations

JsonArrayBuilder (javax.json.JsonArrayBuilder)177 JsonObjectBuilder (javax.json.JsonObjectBuilder)103 JsonObject (javax.json.JsonObject)36 Map (java.util.Map)29 Path (javax.ws.rs.Path)26 GET (javax.ws.rs.GET)24 HashMap (java.util.HashMap)19 JsonArray (javax.json.JsonArray)18 ArrayList (java.util.ArrayList)15 List (java.util.List)15 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)12 IOException (java.io.IOException)12 Dataverse (edu.harvard.iq.dataverse.Dataverse)10 Dataset (edu.harvard.iq.dataverse.Dataset)9 User (edu.harvard.iq.dataverse.authorization.users.User)9 JsonValue (javax.json.JsonValue)9 StringWriter (java.io.StringWriter)8 JsonString (javax.json.JsonString)7 Date (java.util.Date)6 JsonException (javax.json.JsonException)6