Example usage for com.fasterxml.jackson.databind ObjectMapper readValue

List of usage examples for com.fasterxml.jackson.databind ObjectMapper readValue

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper readValue.

Prototype

@SuppressWarnings("unchecked")
    public <T> T readValue(byte[] src, JavaType valueType)
            throws IOException, JsonParseException, JsonMappingException 

Source Link

Usage

From source file:mercury.core.JSONUtils.java

public static <T> List<T> mapFromJSONList(Object object) {

    List<T> list = new ArrayList<T>();

    ObjectMapper mapper = new ObjectMapper();
    try {/*from w  w  w.j ava  2  s.c o m*/
        return mapper.readValue(object.toString(), new TypeReference<List<T>>() {
        });
    } catch (IOException ex) {
        ImageJFX.getLogger().log(Level.SEVERE, null, ex);
        return list;
    }

}

From source file:org.apache.myriad.scheduler.resource.TestResourceOfferContainer.java

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    cfg = mapper.readValue(
            Thread.currentThread().getContextClassLoader().getResource("myriad-config-test-default.yml"),
            MyriadConfiguration.class);
}

From source file:eu.freme.common.persistence.model.OwnedResource.java

public static <T extends OwnedResource> T fromJson(String json, Class<T> entityClass) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    return entityClass.cast(mapper.readValue(json, entityClass));
}

From source file:com.google.openrtb.json.OpenRtbJsonResponseHelper.java

/**
 * Json generator method./*from   ww w  . j a v  a  2  s  .  c o m*/
 *
 * @param isFull true, if nearly all fields should be filled; just some selected fields otherwise
 * @param isRootNative true, if the "native" field should be included as root element
 * @param isNativeObject true, if the native part should be generated as Json object;
 *     String otherwise
 * @return not pretty printed String representation of Json
 */
private static String generateJson(boolean isFull, boolean isRootNative, boolean isNativeObject)
        throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    Object json = mapper.readValue(generateResponse(isFull, isRootNative, isNativeObject), Object.class);
    return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
}

From source file:ch.ralscha.extdirectspring_itest.RawJsonControllerTest.java

@SuppressWarnings("unchecked")
private static void testAndCheck(String action, String method, Integer total, boolean success)
        throws IOException, JsonParseException, JsonMappingException {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    CloseableHttpResponse response = null;
    try {//  w w  w . j av  a 2s  .com
        HttpPost post = new HttpPost("http://localhost:9998/controller/router");

        StringEntity postEntity = new StringEntity("{\"action\":\"" + action + "\",\"method\":\"" + method
                + "\",\"data\":[],\"type\":\"rpc\",\"tid\":1}", "UTF-8");
        post.setEntity(postEntity);
        post.setHeader("Content-Type", "application/json; charset=UTF-8");

        response = client.execute(post);
        HttpEntity entity = response.getEntity();
        assertThat(entity).isNotNull();
        String responseString = EntityUtils.toString(entity);

        assertThat(responseString).isNotNull();
        assertThat(responseString).startsWith("[").endsWith("]");

        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> rootAsMap = mapper
                .readValue(responseString.substring(1, responseString.length() - 1), Map.class);
        assertEquals(5, rootAsMap.size());

        assertEquals(method, rootAsMap.get("method"));
        assertEquals("rpc", rootAsMap.get("type"));
        assertEquals(action, rootAsMap.get("action"));
        assertEquals(1, rootAsMap.get("tid"));

        Map<String, Object> result = (Map<String, Object>) rootAsMap.get("result");
        if (total != null) {
            assertEquals(3, result.size());
            assertThat((Integer) result.get("total")).isEqualTo(total);
        } else {
            assertEquals(2, result.size());
        }
        assertThat((Boolean) result.get("success")).isEqualTo(success);

        List<Map<String, Object>> records = (List<Map<String, Object>>) result.get("records");
        assertEquals(2, records.size());

        assertEquals("4cf8e5b8924e23349fb99454", ((Map<String, Object>) records.get(0).get("_id")).get("$oid"));
        assertEquals("4cf8e5b8924e2334a0b99454", ((Map<String, Object>) records.get(1).get("_id")).get("$oid"));
    } finally {
        IOUtils.closeQuietly(response);
        IOUtils.closeQuietly(client);
    }
}

From source file:io.druid.embedded.QueryHelper.java

public static Query getQuery(InputStream queryInputStream)
        throws JsonParseException, JsonMappingException, IOException {
    ObjectMapper jsonMapper = new DefaultObjectMapper();
    return jsonMapper.readValue(queryInputStream, Query.class);
}

From source file:com.chiralBehaviors.slp.hive.configuration.BroadcastConfiguration.java

public static BroadcastConfiguration fromYaml(InputStream yaml)
        throws JsonParseException, JsonMappingException, IOException {
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    mapper.registerModule(new EngineModule());
    return mapper.readValue(yaml, BroadcastConfiguration.class);
}

From source file:it.jugtorino.one.msvc.way.rabbit.reference.utils.JSONUtils.java

public static Map<String, Object> toMap(byte[] bytes) {
    ObjectMapper mapper = new ObjectMapper();

    TypeReference<Map<String, Object>> typeRef = new TypeReference<Map<String, Object>>() {
    };/*from w w w  .  ja  va  2s .  co  m*/

    try {
        return mapper.readValue(bytes, typeRef);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:nats.codec.ConnectBody.java

public static ConnectBody parse(String body) {
    final ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    try {//from  ww w  .  ja v  a 2 s. c o m
        return mapper.readValue(body, ConnectBody.class);
    } catch (IOException e) {
        throw new NatsException(e);
    }
}

From source file:com.hellblazer.glassHouse.discovery.HubConfiguration.java

public static HubConfiguration fromYaml(InputStream yaml)
        throws JsonParseException, JsonMappingException, IOException {
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    mapper.registerModule(new DiscoveryModule());
    return mapper.readValue(yaml, HubConfiguration.class);
}