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;
}
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();
}
}
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;
}
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;
}
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;
}
}
Aggregations