Search in sources :

Example 56 with JsonNode

use of org.codehaus.jackson.JsonNode in project tdi-studio-se by Talend.

the class ExchangeUtils method parseJsonObject.

public static List parseJsonObject(String jsonContent, Class clazz) throws Exception {
    // need factory for creating parser to use
    List objList = new ArrayList();
    // for 4.1.0 the is no json param on server ,so jsonContent is "wrong parameters for version"
    if (!jsonContent.startsWith("[")) {
        return objList;
    }
    JsonFactory jf = new JsonFactory();
    JsonNode node = new ObjectMapper().reader().readTree(jf.createJsonParser(new StringReader(jsonContent)));
    List<Object> list = new ObjectMapper().readValue(node.traverse(), List.class);
    for (Object source : list) {
        Object obj = clazz.newInstance();
        BeanUtils.copyProperties(obj, source);
        objList.add(obj);
    }
    return objList;
}
Also used : ArrayList(java.util.ArrayList) JsonFactory(org.codehaus.jackson.JsonFactory) StringReader(java.io.StringReader) List(java.util.List) ArrayList(java.util.ArrayList) JsonNode(org.codehaus.jackson.JsonNode) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 57 with JsonNode

use of org.codehaus.jackson.JsonNode in project perun by CESNET.

the class JsonDeserializer method readInt.

@Override
public int readInt(String name) throws RpcException {
    JsonNode node;
    if (name == null) {
        // The object is not under root, but directly in the response
        node = root;
        name = "root";
    } else {
        node = root.get(name);
    }
    if (node == null) {
        throw new RpcException(RpcException.Type.MISSING_VALUE, name);
    }
    if (!node.isInt()) {
        if (!node.isTextual()) {
            throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as int");
        } else {
            try {
                return Integer.parseInt(node.getTextValue());
            } catch (NumberFormatException ex) {
                throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as int", ex);
            }
        }
    }
    return node.getIntValue();
}
Also used : RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException) JsonNode(org.codehaus.jackson.JsonNode)

Example 58 with JsonNode

use of org.codehaus.jackson.JsonNode in project perun by CESNET.

the class OIDC method getUserinfo.

private ObjectNode getUserinfo(final PerunSession sess, final User user, Collection<String> allowedScopes, Map<String, String> properties) throws InternalErrorException, PrivilegeException, UserNotExistsException, WrongAttributeAssignmentException {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode userinfo = mapper.createObjectNode();
    for (final String scope : allowedScopes) {
        String property = properties.get(scope);
        if (property == null || property.isEmpty()) {
            log.info("No values are mapped to scope " + scope + ". Configure them in " + CONFIG_FILE);
            continue;
        }
        try {
            JsonNode claims = mapper.readTree(property);
            if (!claims.isObject()) {
                throw new InternalErrorException("Config file " + CONFIG_FILE + " is wrongly configured. Values have to be valid JSON objects.");
            }
            claims = replaceAttrsInJsonTree(sess, user, claims);
            if (claims != null && claims.isObject()) {
                userinfo.putAll((ObjectNode) claims);
            }
        } catch (IOException e) {
            throw new InternalErrorException("Config file " + CONFIG_FILE + " is wrongly configured. Values have to be valid JSON objects.", e);
        }
    }
    return userinfo;
}
Also used : ObjectNode(org.codehaus.jackson.node.ObjectNode) JsonNode(org.codehaus.jackson.JsonNode) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) IOException(java.io.IOException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 59 with JsonNode

use of org.codehaus.jackson.JsonNode in project perun by CESNET.

the class JsonDeserializer method readList.

@Override
public <T> List<T> readList(String name, Class<T> valueType) throws RpcException {
    JsonNode node;
    if (name == null) {
        // The object is not under root, but directly in the response
        node = root;
        name = "root";
    } else {
        node = root.get(name);
    }
    if (node == null) {
        throw new RpcException(RpcException.Type.MISSING_VALUE, name);
    }
    if (node.isNull()) {
        return null;
    }
    if (!node.isArray()) {
        throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as List<" + valueType.getSimpleName() + "> - not an array");
    }
    try {
        List<T> list = new ArrayList<>(node.size());
        for (JsonNode e : node) {
            list.add(mapper.readValue(e, valueType));
        }
        return list;
    } catch (IOException ex) {
        throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as List<" + valueType.getSimpleName() + ">", ex);
    }
}
Also used : RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException) ArrayList(java.util.ArrayList) JsonNode(org.codehaus.jackson.JsonNode) IOException(java.io.IOException)

Example 60 with JsonNode

use of org.codehaus.jackson.JsonNode in project perun by CESNET.

the class JsonDeserializer method readArrayOfInts.

@Override
public int[] readArrayOfInts(String name) throws RpcException {
    JsonNode node = root.get(name);
    if (node == null) {
        throw new RpcException(RpcException.Type.MISSING_VALUE, name);
    }
    if (node.isNull()) {
        return null;
    }
    if (!node.isArray()) {
        throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as int[] - not an array");
    }
    int[] array = new int[node.size()];
    for (int i = 0; i < node.size(); ++i) {
        JsonNode value = node.get(i);
        if (!value.isInt()) {
            throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as int");
        }
        array[i] = node.get(i).getIntValue();
    }
    return array;
}
Also used : RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException) JsonNode(org.codehaus.jackson.JsonNode)

Aggregations

JsonNode (org.codehaus.jackson.JsonNode)200 Test (org.junit.Test)55 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)50 IOException (java.io.IOException)40 ArrayList (java.util.ArrayList)20 HashMap (java.util.HashMap)20 HTTP (org.neo4j.test.server.HTTP)18 Resource (com.netflix.simianarmy.Resource)17 AWSResource (com.netflix.simianarmy.aws.AWSResource)17 ObjectNode (org.codehaus.jackson.node.ObjectNode)15 Response (org.neo4j.test.server.HTTP.Response)12 Map (java.util.Map)9 ArrayNode (org.codehaus.jackson.node.ArrayNode)9 RpcException (cz.metacentrum.perun.core.api.exceptions.RpcException)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 JsonParseException (org.neo4j.server.rest.domain.JsonParseException)7 Date (java.util.Date)6 List (java.util.List)6 Description (org.hamcrest.Description)6 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)6