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

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

Introduction

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

Prototype

@SuppressWarnings("unchecked")
@Override
public <T> T treeToValue(TreeNode n, Class<T> valueType) throws JsonProcessingException 

Source Link

Document

Convenience conversion method that will bind data given JSON tree contains into specific value (usually bean) type.

Usage

From source file:com.ignorelist.kassandra.steam.scraper.JsonApiTagLoader.java

private static Data loadAppData(byte[] data) throws MalformedURLException, IOException {
    ObjectMapper mapper = buildMapper();
    final JsonNode tree = mapper.readTree(data);
    return mapper.treeToValue(tree.findValue("data"), Data.class);
}

From source file:com.ignorelist.kassandra.steam.scraper.JsonApiTagLoader.java

private static Data loadAppData(InputStream data) throws MalformedURLException, IOException {
    ObjectMapper mapper = buildMapper();
    final JsonNode tree = mapper.readTree(data);
    return mapper.treeToValue(tree.findValue("data"), Data.class);
}

From source file:com.amazonaws.services.iot.client.shadow.AwsIotJsonDeserializer.java

private static void updateDeviceProperty(ObjectMapper jsonObjectMapper, JsonNode node,
        AbstractAwsIotDevice device, Field field) throws IOException {
    Object value = jsonObjectMapper.treeToValue(node, field.getType());
    invokeSetterMethod(device, field.getName(), field.getType(), value);
}

From source file:io.orchestrate.client.ResponseConverterUtil.java

@SuppressWarnings("unchecked")
public static <T> T jsonToDomainObject(ObjectMapper mapper, JsonNode json, Class<T> clazz) throws IOException {
    if (clazz == null || clazz == Void.class || json == null || json.isNull()) {
        return null;
    }/*  w ww  . jav a  2s  .  c  o m*/

    if (clazz.equals(String.class)) {
        return (T) mapper.writeValueAsString(json);
    }
    return mapper.treeToValue(json, clazz);
}

From source file:org.jsonschema2pojo.integration.MediaIT.java

public static void roundTripAssertions(ObjectMapper objectMapper, String propertyName, String jsonValue,
        Object javaValue) throws Exception {

    ObjectNode node = objectMapper.createObjectNode();
    node.put(propertyName, jsonValue);//w w w.j av  a 2s.  c om

    Object pojo = objectMapper.treeToValue(node, classWithMediaProperties);

    Method getter = new PropertyDescriptor(propertyName, classWithMediaProperties).getReadMethod();

    assertThat(getter.invoke(pojo), is(equalTo(javaValue)));

    JsonNode jsonVersion = objectMapper.valueToTree(pojo);

    assertThat(jsonVersion.get(propertyName).asText(), is(equalTo(jsonValue)));
}

From source file:io.orchestrate.client.ResponseConverterUtil.java

@SuppressWarnings("unchecked")
static <T> T jsonToDomainObject(ObjectMapper mapper, JsonNode json, String rawValue, Class<T> clazz)
        throws IOException {
    if (clazz == null || clazz == Void.class || json == null || json.isNull()) {
        return null;
    }/*from  w  w w .  j a va 2 s.com*/

    if (clazz.equals(String.class)) {
        if (rawValue != null) {
            return (T) rawValue;
        }
        return (T) mapper.writeValueAsString(json);
    }
    return mapper.treeToValue(json, clazz);
}

From source file:com.ikanow.aleph2.data_model.utils.BeanTemplateUtils.java

/** Returns a template builder of the designated type from the JSON (note: not very high performance, should only be used for management-type operations)
 * @param json/*w w w . ja  v a 2  s. c o m*/
 * @param bean_clazz
 * @return
 * @throws JsonParseException
 * @throws JsonMappingException
 * @throws IOException
 */
static public <T> TemplateHelper<T> build(final JsonNode json, final Class<T> bean_clazz)
        throws JsonParseException, JsonMappingException, IOException {
    ObjectMapper object_mapper = BeanTemplateUtils.configureMapper(Optional.empty());
    return build(object_mapper.treeToValue(json, bean_clazz));
}

From source file:com.ikanow.aleph2.data_model.utils.BeanTemplateUtils.java

/** Returns a template builder of the designated type from the JSON (note: not very high performance, should only be used for management-type operations)
 * @param json_str/*ww w  .j a  v  a2s.  c o  m*/
 * @param bean_clazz
 * @return
 * @throws JsonParseException
 * @throws JsonMappingException
 * @throws IOException
 */
static public <T> TemplateHelper<T> build(final String json_str, final Class<T> bean_clazz)
        throws JsonParseException, JsonMappingException, IOException {
    ObjectMapper object_mapper = BeanTemplateUtils.configureMapper(Optional.empty());
    return build(object_mapper.treeToValue(object_mapper.readTree(json_str.getBytes()), bean_clazz));
}

From source file:com.ikanow.aleph2.data_model.utils.BeanTemplateUtils.java

/** Converts a JsonNode to a bean template of the specified type
 * (note: not very high performance, should only be used for management-type operations)
 * @param bean - the JSON node to convert to a bean 
 * @return - the bean template/*w  w w . ja  v a2 s . com*/
 */
static public <T> BeanTemplate<T> from(final JsonNode bean_json, final Class<T> clazz) {
    try {
        ObjectMapper object_mapper = BeanTemplateUtils.configureMapper(Optional.empty());
        return BeanTemplate.of(object_mapper.treeToValue(bean_json, clazz));
    } catch (Exception e) { // on fail returns an unchecked error
        throw new RuntimeException(e); // (this can only happen due to "static" code type issues, so unchecked exception is fine
    }
}

From source file:io.coala.json.JsonUtil.java

/**
 * @param om the {@link ObjectMapper} used to parse/deserialize/unmarshal
 * @param tree the partially parsed JSON {@link TreeNode}
 * @param resultType the type of result {@link Object}
 * @param imports the {@link Properties} instances for default values, etc.
 * @return the parsed/deserialized/unmarshalled {@link Object}
 *///from w w  w  .  j a va 2  s  .  co  m
public static <T> T valueOf(final ObjectMapper om, final TreeNode tree, final Class<T> resultType,
        final Properties... imports) {
    if (tree == null)
        return null;
    // TODO add work-around for Wrapper sub-types?
    if (resultType.isMemberClass() && !Modifier.isStatic(resultType.getModifiers()))
        return Thrower.throwNew(IllegalArgumentException.class, "Unable to deserialize non-static member: {}",
                resultType);

    try {
        return (T) om.treeToValue(tree, checkRegistered(om, resultType, imports));
    } catch (final Exception e) {
        return Thrower.rethrowUnchecked(e);
    }
}