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

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

Introduction

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

Prototype

@SuppressWarnings({ "unchecked", "resource" })
public <T extends JsonNode> T valueToTree(Object fromValue) throws IllegalArgumentException 

Source Link

Document

Reverse of #treeToValue ; given a value (usually bean), will construct equivalent JSON Tree representation.

Usage

From source file:org.fasterxml.jackson.tc.TreeTest.java

static <T extends Enum<T>, K> K transcodeFromMap(Map<T, Object> configuration,
        TypeReference<K> configurationType) {
    ObjectMapper mapper = TestEnumModule.setupObjectMapper(new ObjectMapper(new YAMLFactory()));
    JsonNode tree = mapper.valueToTree(configuration);
    try {//from  w w  w  . ja  v  a  2s . c  om
        return mapper.readerFor(configurationType).readValue(tree);
    } catch (IOException e) {
        throw new RuntimeException(
                "Failed to map configuration map to object of type " + configurationType.toString(), e);
    }
}

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

/**
 * @param profile// w  ww  . ja  va 2s  . co m
 * @return
 */
public static JsonNode toTree(final Object object) {
    final ObjectMapper om = getJOM();
    try {
        // checkRegistered(om, object.getClass());
        return om.valueToTree(object);
        //         return om.readTree( stringify( object ) );
    } catch (final Exception e) {
        return Thrower.rethrowUnchecked(e);
    }
}

From source file:com.flipkart.foxtrot.core.TestUtils.java

public static Document getDocument(String id, long timestamp, Object[] args, ObjectMapper mapper) {
    Map<String, Object> data = new HashMap<String, Object>();
    for (int i = 0; i < args.length; i += 2) {
        data.put((String) args[i], args[i + 1]);
    }//w  w  w.  j  a  va  2  s .com
    return new Document(id, timestamp, mapper.valueToTree(data));
}

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

/** Converts a bean to its JsonNode representation (not high performance)
 * @param bean - the bean to convert to JSON
 * @return - the JSON/*from  ww  w .jav  a  2 s .co m*/
 */
static public <T> JsonNode toJson(final T bean) {
    ObjectMapper object_mapper = BeanTemplateUtils.configureMapper(Optional.empty());
    return object_mapper.valueToTree(bean);
}

From source file:com.flipkart.foxtrot.core.TestUtils.java

public static List<Document> getMappingDocuments(ObjectMapper mapper) {
    List<Document> documents = new Vector<Document>();
    Map<String, Object> document = new HashMap<String, Object>();
    document.put("word", "1234");
    document.put("data", Collections.singletonMap("data", "d"));
    document.put("header", Collections.singletonList(Collections.singletonMap("hello", "world")));
    documents.add(new Document("Z", System.currentTimeMillis(), mapper.valueToTree(document)));

    document = new HashMap<String, Object>();
    document.put("word", "1234");
    document.put("data", Collections.singletonMap("data", "d"));
    document.put("head", Collections.singletonList(Collections.singletonMap("hello", 23)));
    documents.add(new Document("Y", System.currentTimeMillis(), mapper.valueToTree(document)));
    return documents;
}

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  ww .ja v a2 s  . c o m*/

    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:org.camunda.spin.impl.json.jackson.format.JacksonJsonDataFormatMapper.java

public Object mapJavaToInternal(Object parameter) {
    ObjectMapper mapper = format.getObjectMapper();
    try {// ww w. j a va 2s. c o m
        return mapper.valueToTree(parameter);
    } catch (IllegalArgumentException e) {
        throw LOG.unableToMapInput(parameter, e);
    }
}

From source file:com.opsie.opsiecomponent.test.ComponentTest.java

@Test
public void test_parse_json() {
    String testData = null;/*from   w  w w.  ja  v a 2 s. co m*/
    try {
        testData = IOUtils.toString(testPageWithTestsData.getInputStream());
    } catch (IOException ex) {
        Logger.getLogger(ComponentTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = mapper.valueToTree(testData);
    System.out.println("node here = " + node.toString());
}

From source file:com.digitalpebble.storm.crawler.filtering.BasicURLNormalizerTest.java

private JsonNode getArrayNode(List<String> queryElementsToRemove) {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.valueToTree(queryElementsToRemove);
}

From source file:com.arpnetworking.test.junitbenchmarks.GCShapshotSerializerTest.java

@Test
public void testSerialization() {
    final SimpleModule module = new SimpleModule();
    module.addSerializer(GCSnapshot.class, new GCSnapshotSerializer());
    final ObjectMapper mapper = ObjectMapperFactory.createInstance();
    mapper.registerModule(module);//from   w  w w  . jav a2 s  .  co m

    final GCSnapshot testSnapshot = DataCreator.createGCSnapshot();

    final JsonNode jsonNode = mapper.valueToTree(testSnapshot);
    Assert.assertTrue(jsonNode.isObject());
    Assert.assertThat(jsonNode.get("accumulatedInvocations").asLong(), Matchers.greaterThanOrEqualTo(0L));
    Assert.assertThat(jsonNode.get("accumulatedTime").asLong(), Matchers.greaterThanOrEqualTo(0L));
}