Search in sources :

Example 91 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 92 with JsonNode

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

the class JsonDeserializer method read.

@Override
public <T> T read(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.isObject()) {
        throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as " + valueType.getSimpleName());
    }
    try {
        return mapper.readValue(node, valueType);
    } catch (IOException ex) {
        throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as " + valueType.getSimpleName(), ex);
    }
}
Also used : RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException) JsonNode(org.codehaus.jackson.JsonNode) IOException(java.io.IOException)

Example 93 with JsonNode

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

the class JsonDeserializer method read.

@Override
public <T> T read(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.isObject()) {
        throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as " + valueType.getSimpleName());
    }
    try {
        return mapper.readValue(node, valueType);
    } catch (IOException ex) {
        throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as " + valueType.getSimpleName(), ex);
    }
}
Also used : RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException) JsonNode(org.codehaus.jackson.JsonNode) IOException(java.io.IOException)

Example 94 with JsonNode

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

the class JsonDeserializer method readListPerunBeans.

@Override
public List<PerunBean> readListPerunBeans(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.isNull()) {
        return null;
    }
    if (!node.isArray()) {
        throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as List<PerunBean> - not an array");
    }
    try {
        List<PerunBean> list = new ArrayList<>(node.size());
        for (JsonNode e : node) {
            String beanName = e.get("beanName").getTextValue();
            if (beanName == null) {
                throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as List<PerunBean> - missing beanName info");
            }
            list.add((PerunBean) mapper.readValue(e, Class.forName("cz.metacentrum.perun.core.api." + beanName)));
        }
        return list;
    } catch (ClassNotFoundException ex) {
        throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as List<PerunBean> - class not found");
    } catch (IOException ex) {
        throw new RpcException(RpcException.Type.CANNOT_DESERIALIZE_VALUE, node.toString() + " as List<PerunBean>", 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 95 with JsonNode

use of org.codehaus.jackson.JsonNode in project YCSB by brianfrankcooper.

the class TestMeasurementsExporter method testJSONArrayMeasurementsExporter.

@Test
public void testJSONArrayMeasurementsExporter() throws IOException {
    Properties props = new Properties();
    props.put(Measurements.MEASUREMENT_TYPE_PROPERTY, "histogram");
    Measurements mm = new Measurements(props);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JSONArrayMeasurementsExporter export = new JSONArrayMeasurementsExporter(out);
    long min = 5000;
    long max = 100000;
    ZipfianGenerator zipfian = new ZipfianGenerator(min, max);
    for (int i = 0; i < 1000; i++) {
        int rnd = zipfian.nextValue().intValue();
        mm.measure("UPDATE", rnd);
    }
    mm.exportMeasurements(export);
    export.close();
    ObjectMapper mapper = new ObjectMapper();
    JsonNode json = mapper.readTree(out.toString("UTF-8"));
    assertTrue(json.isArray());
    assertEquals(json.get(0).get("measurement").asText(), "Operations");
    assertEquals(json.get(4).get("measurement").asText(), "MaxLatency(us)");
    assertEquals(json.get(11).get("measurement").asText(), "4");
}
Also used : ZipfianGenerator(com.yahoo.ycsb.generator.ZipfianGenerator) Measurements(com.yahoo.ycsb.measurements.Measurements) JsonNode(org.codehaus.jackson.JsonNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.testng.annotations.Test)

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