Example usage for com.fasterxml.jackson.core.type TypeReference TypeReference

List of usage examples for com.fasterxml.jackson.core.type TypeReference TypeReference

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core.type TypeReference TypeReference.

Prototype

protected TypeReference() 

Source Link

Usage

From source file:org.redisson.spring.cache.CacheConfigSupport.java

public Map<String, CacheConfig> fromYAML(File file) throws IOException {
    return yamlMapper.readValue(file, new TypeReference<Map<String, CacheConfig>>() {
    });//from   w  w w .  j  a  v a  2s  .  c o m
}

From source file:com.twitter.ambrose.model.DAGNode.java

public static DAGNode<? extends Job> fromJson(String json) throws IOException {
    return JSONUtil.toObject(json, new TypeReference<DAGNode<? extends Job>>() {
    });//w w w . ja v a 2 s. co  m
}

From source file:com.infinities.keystone4j.policy.Rules.java

public static Rules loadJson(String data, Object defaultRule) throws IOException {
    Map<String, String> map = JsonUtils.readJson(data, new TypeReference<LinkedHashMap<String, String>>() {
    });/*from w w w.j a  v a  2  s.c om*/

    Map<String, BaseCheck> rules = Maps.newHashMap();
    for (Entry<String, String> entry : map.entrySet()) {
        logger.debug("put rules key:{}, value:{} ", new Object[] { entry.getKey(), entry.getValue() });
        rules.put(entry.getKey(), parseRule(entry.getValue()));
    }

    logger.debug("defaultRule: {}", defaultRule);
    return new Rules(rules, defaultRule);
}

From source file:io.victoralbertos.jolyglot.JacksonSpeaker.java

/**
 * {@inheritDoc}/*  w w w.  j  a  v a2  s. c o m*/
 */
@Override
public <T> T fromJson(String json, final Type typeOfT) throws RuntimeException {
    try {
        TypeReference<T> referenceType = new TypeReference<T>() {
            @Override
            public Type getType() {
                return typeOfT;
            }
        };
        return mapper.readValue(json, referenceType);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:edu.usu.sdl.opencatalog.web.action.TestAction.java

@HandlesEvent("ConvertData")
public Resolution convertData() throws IOException {
    List<OldAsset> assets = objectMapper.readValue(
            new File("C:\\development\\storefront\\source\\old_data\\assets.json"),
            new TypeReference<List<OldAsset>>() {
            });/*from   w ww.  j ava  2s. com*/

    List<Asset> newAssets = new ArrayList<>();
    assets.forEach(oldAsset -> {

        Asset asset = new Asset();

        asset.setId(oldAsset.getId());
        asset.setName(oldAsset.getTitle());
        asset.setDescription(oldAsset.getDescription());
        asset.setOwner(oldAsset.getOrganization());

        asset.setShortDescription(
                oldAsset.getDescription().substring(0, oldAsset.getDescription().indexOf(".")));
        asset.getStats().setAverageRating(oldAsset.getAvgRate());
        asset.getStats().setComments(oldAsset.getTotalComments());
        asset.getStats().setNumberRatings(oldAsset.getTotalVotes());

        Map<String, String> typeMap = new HashMap<>();
        typeMap.put("4", "WIDGET");
        typeMap.put("9", "APPS");
        typeMap.put("10", "REFDOCS");
        typeMap.put("18", "SOFTLIB");

        String type = "TOOLS";
        String foundType = typeMap.get("" + oldAsset.getTypes().getId());
        if (StringUtils.isNotBlank(foundType)) {
            type = foundType;
        }

        asset.setType(type);
        asset.setConformanceState(oldAsset.getState().getTitle());

        oldAsset.getCategories().forEach(cat -> {
            AssetCategory category = new AssetCategory();
            category.setDesc(cat.getTitle());
            asset.getCategories().add(category);
        });

        newAssets.add(asset);
    });

    return streamResults(newAssets);
}

From source file:ch.ralscha.extdirectspring.controller.RouterControllerTreeLoadTest.java

@SuppressWarnings("unchecked")
@Test/*  w ww .  j a  v  a 2s.  c  om*/
public void testNoAdditionalParameters() {

    Map<String, Object> requestParameters = new LinkedHashMap<String, Object>();
    requestParameters.put("node", "root");

    List<Node> nodes = (List<Node>) ControllerUtil.sendAndReceive(mockMvc, "remoteProviderTreeLoad", "method1",
            new TypeReference<List<Node>>() {/* nothinghere */
            }, requestParameters);

    assertThat(nodes).hasSize(5).containsSequence(new Node("n1", "Node 1", false),
            new Node("n2", "Node 2", false), new Node("n3", "Node 3", false), new Node("n4", "Node 4", false),
            new Node("n5", "Node 5", false));

    requestParameters = new LinkedHashMap<String, Object>();
    requestParameters.put("node", "n1");

    nodes = (List<Node>) ControllerUtil.sendAndReceive(mockMvc, "remoteProviderTreeLoad", "method1",
            new TypeReference<List<Node>>() {/* nothinghere */
            }, requestParameters);

    assertThat(nodes).hasSize(5).containsSequence(new Node("id1", "Node 1.1", true),
            new Node("id2", "Node 1.2", true), new Node("id3", "Node 1.3", true),
            new Node("id4", "Node 1.4", true), new Node("id5", "Node 1.5", true));
}

From source file:com.yahoo.elide.jsonapi.models.PatchTest.java

@Test
public void testListDeserialization() throws Exception {
    ObjectMapper mapper = new ObjectMapper();

    String input = "[{\"op\":\"add\",\"path\":\"/foo/bar\",\"value\":\"stringValue\"}]";

    List<Patch> patches = mapper.readValue(input, new TypeReference<List<Patch>>() {
    });//from   w  w  w  . j  a  v a  2 s . c om
    Assert.assertEquals(patches.get(0).getOperation(), Patch.Operation.ADD,
            "Deserialized patch operation should match.");
    Assert.assertEquals(patches.get(0).getPath(), "/foo/bar", "Deserialized patch path should match.");

    JsonNode node = patches.get(0).getValue();

    String value = mapper.treeToValue(node, String.class);

    Assert.assertEquals(value, "stringValue", "Deserialized patch value should match");
}

From source file:tachyon.master.RawTablesTest.java

@Test
public void writeImageTest() throws IOException, TachyonException {
    // crate the RawTables, byte buffers, and output streams
    RawTables rt = new RawTables(new TachyonConf());
    ByteBuffer bb1 = ByteBuffer.allocate(1);
    ByteBuffer bb2 = ByteBuffer.allocate(1);
    ByteBuffer bb3 = ByteBuffer.allocate(1);

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(os);
    ObjectMapper mapper = JsonObject.createObjectMapper();
    ObjectWriter writer = mapper.writer();

    // add elements to the RawTables
    rt.addRawTable(0, 1, bb1);/*from w  w w.ja  v a  2  s  .  c  om*/
    rt.addRawTable(1, 1, bb2);
    rt.addRawTable(2, 1, bb3);

    // write the image
    rt.writeImage(writer, dos);

    List<Integer> ids = Arrays.asList(0, 1, 2);
    List<Integer> columns = Arrays.asList(1, 1, 1);
    List<ByteBuffer> data = Arrays.asList(bb1, bb2, bb3);

    // decode the written bytes
    ImageElement decoded = mapper.readValue(os.toByteArray(), ImageElement.class);

    // test the decoded ImageElement
    Assert.assertEquals(ids, decoded.get("ids", new TypeReference<List<Integer>>() {
    }));
    Assert.assertEquals(columns, decoded.get("columns", new TypeReference<List<Integer>>() {
    }));
    Assert.assertEquals(data, decoded.get("data", new TypeReference<List<ByteBuffer>>() {
    }));
}

From source file:com.codemacro.jcm.test.TestCluster.java

public void testStatus() throws IOException {
    Map<String, NodeStatus> statusList = Maps.newHashMap();
    statusList.put("127.0.0.1|http:8000", NodeStatus.TIMEOUT);
    statusList.put("127.0.0.1|http:9000", NodeStatus.NORMAL);
    String json = JsonUtil.toString(statusList);
    Map<String, NodeStatus> map = JsonUtil.fromString(json, new TypeReference<Map<String, NodeStatus>>() {
    });/*  w  w w.  java2s . c  o m*/
    System.out.println(map);
}

From source file:alfio.manager.EuVatChecker.java

private static VatDetail getVatDetail(Response resp, String vatNr, String countryCode,
        String organizerCountryCode) throws IOException {
    ResponseBody body = resp.body();/*from www  .j a v a2s. c  o m*/
    String jsonString = body != null ? body.string() : "{}";
    Map<String, String> json = Json.fromJson(jsonString, new TypeReference<Map<String, String>>() {
    });
    boolean isValid = Boolean.parseBoolean(json.get("isValid"));
    return new VatDetail(vatNr, countryCode, isValid, json.get("name"), json.get("address"),
            isValid && !organizerCountryCode.equals(countryCode));
}