Search in sources :

Example 51 with JsonString

use of javax.json.JsonString in project felix by apache.

the class JSONUtil method getValue.

public static Object getValue(final JsonValue value) {
    switch(value.getValueType()) {
        // type NULL -> return null
        case NULL:
            return null;
        // type TRUE or FALSE -> return boolean
        case FALSE:
            return false;
        case TRUE:
            return true;
        // type String -> return String
        case STRING:
            return ((JsonString) value).getString();
        // type Number -> return long or double
        case NUMBER:
            final JsonNumber num = (JsonNumber) value;
            if (num.isIntegral()) {
                return num.longValue();
            }
            return num.doubleValue();
        // type ARRAY -> return list and call this method for each value
        case ARRAY:
            final List<Object> array = new ArrayList<>();
            for (final JsonValue x : ((JsonArray) value)) {
                array.add(getValue(x));
            }
            return array;
        // type OBJECT -> return map
        case OBJECT:
            final Map<String, Object> map = new HashMap<>();
            final JsonObject obj = (JsonObject) value;
            for (final Map.Entry<String, JsonValue> entry : obj.entrySet()) {
                map.put(entry.getKey(), getValue(entry.getValue()));
            }
            return map;
    }
    return null;
}
Also used : HashMap(java.util.HashMap) JsonNumber(javax.json.JsonNumber) ArrayList(java.util.ArrayList) JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonString(javax.json.JsonString) HashMap(java.util.HashMap) Map(java.util.Map)

Example 52 with JsonString

use of javax.json.JsonString in project dataverse by IQSS.

the class Dataverses method setFacets.

@POST
@Path("{identifier}/facets")
@Produces(MediaType.APPLICATION_JSON)
public /**
 * (not publicly documented) API endpoint for assigning facets to a dataverse.
 * `curl -X POST -H "X-Dataverse-key: $ADMIN_KEY" http://localhost:8088/api/dataverses/$dv/facets --upload-file foo.json`; where foo.json contains a list of datasetField names,
 * works as expected (judging by the UI).
 * This triggers a 500 when '-d @foo.json' is used.
 */
Response setFacets(@PathParam("identifier") String dvIdtf, String facetIds) {
    List<DatasetFieldType> facets = new LinkedList<>();
    for (JsonString facetId : Util.asJsonArray(facetIds).getValuesAs(JsonString.class)) {
        DatasetFieldType dsfType = findDatasetFieldType(facetId.getString());
        if (dsfType == null) {
            return error(Response.Status.BAD_REQUEST, "Can't find dataset field type '" + facetId + "'");
        } else if (!dsfType.isFacetable()) {
            return error(Response.Status.BAD_REQUEST, "Dataset field type '" + facetId + "' is not facetable");
        }
        facets.add(dsfType);
    }
    try {
        Dataverse dataverse = findDataverseOrDie(dvIdtf);
        // by passing null for Featured Dataverses and DataverseFieldTypeInputLevel, those are not changed
        execCommand(new UpdateDataverseCommand(dataverse, facets, null, createDataverseRequest(findUserOrDie()), null));
        return ok("Facets of dataverse " + dvIdtf + " updated.");
    } catch (WrappedResponse ex) {
        return ex.getResponse();
    }
}
Also used : UpdateDataverseCommand(edu.harvard.iq.dataverse.engine.command.impl.UpdateDataverseCommand) JsonString(javax.json.JsonString) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) Dataverse(edu.harvard.iq.dataverse.Dataverse) LinkedList(java.util.LinkedList) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 53 with JsonString

use of javax.json.JsonString in project dataverse by IQSS.

the class JsonParser method parseIpGroup.

public IpGroup parseIpGroup(JsonObject obj) {
    IpGroup retVal = new IpGroup();
    if (obj.containsKey("id")) {
        retVal.setId(Long.valueOf(obj.getInt("id")));
    }
    retVal.setDisplayName(obj.getString("name", null));
    retVal.setDescription(obj.getString("description", null));
    retVal.setPersistedGroupAlias(obj.getString("alias", null));
    if (obj.containsKey("ranges")) {
        obj.getJsonArray("ranges").stream().filter(jv -> jv.getValueType() == JsonValue.ValueType.ARRAY).map(jv -> (JsonArray) jv).forEach(rr -> {
            retVal.add(IpAddressRange.make(IpAddress.valueOf(rr.getString(0)), IpAddress.valueOf(rr.getString(1))));
        });
    }
    if (obj.containsKey("addresses")) {
        obj.getJsonArray("addresses").stream().map(jsVal -> IpAddress.valueOf(((JsonString) jsVal).getString())).map(addr -> IpAddressRange.make(addr, addr)).forEach(retVal::add);
    }
    return retVal;
}
Also used : Date(java.util.Date) FieldDTO(edu.harvard.iq.dataverse.api.dto.FieldDTO) SettingsServiceBean(edu.harvard.iq.dataverse.settings.SettingsServiceBean) JsonValue(javax.json.JsonValue) Gson(com.google.gson.Gson) Map(java.util.Map) DatasetField(edu.harvard.iq.dataverse.DatasetField) ParseException(java.text.ParseException) IpGroup(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup) JsonObject(javax.json.JsonObject) MetadataBlockServiceBean(edu.harvard.iq.dataverse.MetadataBlockServiceBean) DatasetFieldValue(edu.harvard.iq.dataverse.DatasetFieldValue) Timestamp(java.sql.Timestamp) Set(java.util.Set) DataFile(edu.harvard.iq.dataverse.DataFile) Logger(java.util.logging.Logger) ValueType(javax.json.JsonValue.ValueType) DataverseTheme(edu.harvard.iq.dataverse.DataverseTheme) DataverseContact(edu.harvard.iq.dataverse.DataverseContact) List(java.util.List) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) DataFileCategory(edu.harvard.iq.dataverse.DataFileCategory) OptionalFileParams(edu.harvard.iq.dataverse.datasetutility.OptionalFileParams) DatasetFieldConstant(edu.harvard.iq.dataverse.DatasetFieldConstant) Util(edu.harvard.iq.dataverse.api.Util) HashMap(java.util.HashMap) IpAddress(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.ip.IpAddress) ArrayList(java.util.ArrayList) ControlledVocabularyValue(edu.harvard.iq.dataverse.ControlledVocabularyValue) DatasetFieldCompoundValue(edu.harvard.iq.dataverse.DatasetFieldCompoundValue) HashSet(java.util.HashSet) IpAddressRange(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.ip.IpAddressRange) Workflow(edu.harvard.iq.dataverse.workflow.Workflow) Json(javax.json.Json) LinkedList(java.util.LinkedList) Dataverse(edu.harvard.iq.dataverse.Dataverse) JsonReader(javax.json.JsonReader) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) HarvestingClient(edu.harvard.iq.dataverse.harvest.client.HarvestingClient) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) Dataset(edu.harvard.iq.dataverse.Dataset) JsonArray(javax.json.JsonArray) TermsOfUseAndAccess(edu.harvard.iq.dataverse.TermsOfUseAndAccess) DatasetFieldServiceBean(edu.harvard.iq.dataverse.DatasetFieldServiceBean) JsonString(javax.json.JsonString) License(edu.harvard.iq.dataverse.TermsOfUseAndAccess.License) WorkflowStepData(edu.harvard.iq.dataverse.workflow.step.WorkflowStepData) StringReader(java.io.StringReader) Collections(java.util.Collections) JsonArray(javax.json.JsonArray) IpGroup(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup) JsonString(javax.json.JsonString)

Example 54 with JsonString

use of javax.json.JsonString in project dataverse by IQSS.

the class JsonParser method parsePrimitiveValue.

public List<DatasetFieldValue> parsePrimitiveValue(JsonObject json) throws JsonParseException {
    List<DatasetFieldValue> vals = new LinkedList<>();
    if (json.getBoolean("multiple")) {
        for (JsonString val : json.getJsonArray("value").getValuesAs(JsonString.class)) {
            DatasetFieldValue datasetFieldValue = new DatasetFieldValue();
            datasetFieldValue.setDisplayOrder(vals.size() - 1);
            datasetFieldValue.setValue(val.getString().trim());
            vals.add(datasetFieldValue);
        }
    } else {
        DatasetFieldValue datasetFieldValue = new DatasetFieldValue();
        datasetFieldValue.setValue(json.getString("value", "").trim());
        vals.add(datasetFieldValue);
    }
    return vals;
}
Also used : DatasetFieldValue(edu.harvard.iq.dataverse.DatasetFieldValue) JsonString(javax.json.JsonString) LinkedList(java.util.LinkedList)

Example 55 with JsonString

use of javax.json.JsonString in project activemq-artemis by apache.

the class JsonUtil method convertJsonValue.

public static Object convertJsonValue(Object jsonValue, Class desiredType) {
    if (jsonValue instanceof JsonNumber) {
        JsonNumber number = (JsonNumber) jsonValue;
        if (desiredType == null || desiredType == Long.class || desiredType == Long.TYPE) {
            return number.longValue();
        } else if (desiredType == Integer.class || desiredType == Integer.TYPE) {
            return number.intValue();
        } else if (desiredType == Double.class || desiredType == Double.TYPE) {
            return number.doubleValue();
        } else {
            return number.longValue();
        }
    } else if (jsonValue instanceof JsonString) {
        return ((JsonString) jsonValue).getString();
    } else if (jsonValue instanceof JsonValue) {
        if (jsonValue == JsonValue.TRUE) {
            return true;
        } else if (jsonValue == JsonValue.FALSE) {
            return false;
        } else {
            return jsonValue.toString();
        }
    } else if (jsonValue instanceof Number) {
        Number jsonNumber = (Number) jsonValue;
        if (desiredType == Integer.TYPE || desiredType == Integer.class) {
            return jsonNumber.intValue();
        } else if (desiredType == Long.TYPE || desiredType == Long.class) {
            return jsonNumber.longValue();
        } else if (desiredType == Double.TYPE || desiredType == Double.class) {
            return jsonNumber.doubleValue();
        } else if (desiredType == Short.TYPE || desiredType == Short.class) {
            return jsonNumber.shortValue();
        } else {
            return jsonValue;
        }
    } else if (jsonValue instanceof Object[]) {
        Object[] array = (Object[]) jsonValue;
        Object[] result;
        if (desiredType != null) {
            result = (Object[]) Array.newInstance(desiredType, array.length);
        } else {
            result = array;
        }
        for (int i = 0; i < array.length; i++) {
            result[i] = convertJsonValue(array[i], desiredType);
        }
        return result;
    } else {
        return jsonValue;
    }
}
Also used : JsonNumber(javax.json.JsonNumber) JsonNumber(javax.json.JsonNumber) JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString)

Aggregations

JsonString (javax.json.JsonString)78 JsonObject (javax.json.JsonObject)54 JsonReader (javax.json.JsonReader)36 StringReader (java.io.StringReader)34 Test (org.junit.Test)30 JsonArray (javax.json.JsonArray)24 HashMap (java.util.HashMap)17 JsonValue (javax.json.JsonValue)16 LinkedList (java.util.LinkedList)10 ArrayList (java.util.ArrayList)8 Map (java.util.Map)8 JsonNumber (javax.json.JsonNumber)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 JsonException (javax.json.JsonException)5 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)5 ProcessGroupStatus (org.apache.nifi.controller.status.ProcessGroupStatus)5 RemoteProcessGroupStatus (org.apache.nifi.controller.status.RemoteProcessGroupStatus)5 POST (javax.ws.rs.POST)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3