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

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

Introduction

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

Prototype

public JsonNode readTree(URL source) throws IOException, JsonProcessingException 

Source Link

Document

Method to deserialize JSON content as tree expressed using set of JsonNode instances.

Usage

From source file:org.camunda.spin.impl.json.jackson.format.JacksonJsonDataFormatReader.java

public Object readInput(Reader input) {
    ObjectMapper mapper = format.getObjectMapper();

    try {//from   www .j a  v  a 2  s  .com
        return mapper.readTree(input);
    } catch (JsonProcessingException e) {
        throw JSON_LOGGER.unableToParseInput(e);
    } catch (IOException e) {
        throw JSON_LOGGER.unableToParseInput(e);
    }
}

From source file:io.fabric8.che.starter.client.keycloak.KeycloakClient.java

@Override
public String getOpenShiftToken(String authHeader) throws JsonProcessingException, IOException {
    // {"access_token":"token","expires_in":86400,"scope":"user:full","token_type":"Bearer"}
    String responseBody = getResponseBody(KeycloakEndpoint.GET_OPENSHIFT_TOKEN, authHeader);
    ObjectMapper mapper = new ObjectMapper();
    JsonNode json = mapper.readTree(responseBody);
    JsonNode accessToken = json.get(ACCESS_TOKEN);
    return accessToken.asText();
}

From source file:com.dhenton9000.json.processing.FasterXMLJSONTests.java

@Test
public void testModifyTree() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode sampleTree = mapper.readTree(TEST_STRING);
    JsonNode jsonNodeValue = sampleTree.get("alpha");
    ObjectNode sampleObj = (ObjectNode) sampleTree;
    sampleObj.put("alpha", 99);

    assertEquals(sampleTree.get("alpha").intValue(), 99);

    // ob.put("alpha", 555); // or int, long, boolean etc
}

From source file:com.evrythng.java.wrapper.mapping.ActionDeserializerImpl.java

@Override
public Action deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException {

    ObjectCodec codec = jp.getCodec();/*from   ww  w .j  av  a  2  s .  c o  m*/
    ObjectMapper mapper = (ObjectMapper) codec;
    ObjectNode root = mapper.readTree(jp);
    JsonNode type = root.get(getTypeFieldName());
    if (type == null) {
        throw new IllegalArgumentException(this.getValueClass().getSimpleName() + " type cannot be empty.");
    }
    String sType = type.textValue();
    if (sType == null || sType.isEmpty()) {
        throw new IllegalArgumentException(this.getValueClass().getSimpleName() + " type cannot be empty.");
    }

    return codec.treeToValue(root, resolveClass(sType));
}

From source file:com.teradata.presto.yarn.test.slider.SliderStatus.java

public SliderStatus(String statusJson) {
    ObjectMapper objectMapper = new ObjectMapper();
    try {//  w  ww .  j a va 2s . c o  m
        this.status = objectMapper.readTree(statusJson);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.enitalk.configs.BotConfig.java

@Bean(name = "offsetMap")
public TreeMap<Long, String> timezones() throws IOException {
    ObjectMapper j = new ObjectMapper();
    JsonNode o = j.readTree(new ClassPathResource("dates/treeTz.json").getInputStream());
    TreeMap<Long, String> offsets = new TreeMap<>();
    Iterator<JsonNode> it = o.elements();
    while (it.hasNext()) {
        JsonNode el = it.next();//from  www .jav  a2 s. c o  m
        String key = el.fieldNames().next();
        offsets.put(Long.valueOf(key), el.path(key).iterator().next().toString());
    }

    return offsets;

}

From source file:org.comicwiki.serializers.YearDeserializer.java

@Override
public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) p.getCodec();
    JsonNode valueNode = mapper.readTree(p);
    String dateText = valueNode.asText();
    try {//  w w w  . java2 s  . c  o m
        Date time = new SimpleDateFormat("yyyy").parse(dateText);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.ERA, (dateText.charAt(0) == '+') ? GregorianCalendar.AD : GregorianCalendar.BC);
        return calendar.getTime();
    } catch (ParseException e) {
        throw new IOException(e);
    }
}

From source file:com.flipkart.zjsonpatch.ApiTest.java

@Test(expected = InvalidJsonPatchException.class)
public void validatingNonArrayPatchShouldThrowAnException() throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode invalid = objectMapper.readTree("{\"not\": \"a patch\"}");
    JsonPatch.validate(invalid);//from w w  w  .jav a 2s  .  c om
}

From source file:com.flipkart.zjsonpatch.ApiTest.java

@Test(expected = InvalidJsonPatchException.class)
public void validatingAnInvalidArrayShouldThrowAnException() throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode invalid = objectMapper.readTree("[1, 2, 3, 4, 5]");
    JsonPatch.validate(invalid);/*  w w  w  . java2  s. c o  m*/
}

From source file:com.flipkart.zjsonpatch.ApiTest.java

@Test(expected = InvalidJsonPatchException.class)
public void validatingAPatchWithAnInvalidOperationShouldThrowAnException() throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode invalid = objectMapper.readTree("[{\"op\": \"what\"}]");
    JsonPatch.validate(invalid);/*from   w  ww. ja  v  a  2s  .co m*/
}