org.fasterxml.jackson.tc.TreeTest.java Source code

Java tutorial

Introduction

Here is the source code for org.fasterxml.jackson.tc.TreeTest.java

Source

/******************************************************************************
 * * This data and information is proprietary to, and a valuable trade secret
 * * of, Basis Technology Corp.  It is given in confidence by Basis Technology
 * * and may only be used as permitted under the license agreement under which
 * * it has been distributed, and in no other way.
 * *
 * * Copyright (c) 2015 Basis Technology Corporation All rights reserved.
 * *
 * * The technical data and information provided herein are provided with
 * * `limited rights', and the computer software provided herein is provided
 * * with `restricted rights' as those terms are defined in DAR and ASPR
 * * 7-104.9(a).
 ******************************************************************************/

package org.fasterxml.jackson.tc;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.google.common.collect.Maps;
import org.junit.Test;

import java.io.IOException;
import java.util.Map;

import static junit.framework.TestCase.assertNotNull;

/**
 *
 */
public class TreeTest {

    @Test
    public void tree() throws Exception {

        Map<KeyEnum, Object> inputMap = Maps.newHashMap();
        Map<TestEnum, Map<String, String>> replacements = Maps.newHashMap();
        Map<String, String> reps = Maps.newHashMap();
        reps.put("1", "one");
        replacements.put(TestEnum.GREEN, reps);
        inputMap.put(KeyEnum.replacements, replacements);

        Bean bean = transcodeFromMap(inputMap, new TypeReference<Bean>() {
        });
        assertNotNull(bean);
    }

    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 {
            return mapper.readerFor(configurationType).readValue(tree);
        } catch (IOException e) {
            throw new RuntimeException(
                    "Failed to map configuration map to object of type " + configurationType.toString(), e);
        }
    }

}