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.jaqpot.core.service.resource.BibTeXResourceTest.java

@Test
public void testPATCH() throws Exception {

    String patch = "[{ \"op\": \"add\", \"path\": \"/key\", \"value\": \"foo\" }]";
    // 494d5837-1970-4cbb-8fde-a3c56218f240
    // [{ "op": "add", "path": "/key", "value": "foo" }]
    String origin = "{\n" + "  \"bibType\":\"Article\",\n" + "  \"title\":\"title goes here\",\n"
            + "  \"author\":\"A.N.Onymous\",\n" + "  \"journal\":\"Int. J. Biochem.\",\n" + "  \"year\":2010,\n"
            + "  \"meta\":{\"comments\":[\"default bibtex\"]}\n" + "}";

    final ObjectMapper mapper = new ObjectMapper();
    JsonPatch p = mapper.readValue(patch, JsonPatch.class);
    JsonNode result = p.apply(mapper.readTree(origin));
    System.out.println(result);//from   w w  w . j  a  va 2s.c  o m

}

From source file:net.mostlyharmless.jghservice.connector.github.CreateIssue.java

@Override
public Integer processResponse(String json) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(json);
    return root.get("number").asInt();

}

From source file:io.swagger.test.integration.responses.ComplexResponseTestIT.java

/**
 * verifies that the return value uses the schema example in an array
 *//* www  .  ja  v a2s  .  c  o m*/
//    @Test
public void complexArrayResponseWithExample() throws Exception {
    Map<String, String> queryParams = new HashMap<String, String>();

    String str = client.invokeAPI("/mockResponses/complexArrayResponseWithExample", "GET", queryParams, null,
            new HashMap<String, String>(), null, "application/json", null, new String[0]);
    ObjectMapper mapper = Json.mapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);

    JsonNode n1 = mapper.readTree(str);
    JsonNode n2 = mapper.readTree("[{\"foo\":\"bar\"}]");

    assertEquals(Json.pretty(n1), Json.pretty(n2));
}

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

/** {@inheritDoc} */
@Override//  www  .  java 2 s  . c  o m
Iterable<KvObject<String>> fromResponse(final int status, final HttpHeader httpHeader, final String json,
        final JacksonMapper mapper) throws IOException {
    assert (status == 200);

    final ObjectMapper objectMapper = mapper.getMapper();
    final JsonNode jsonNode = objectMapper.readTree(json);

    final int count = jsonNode.get("count").asInt();
    final List<KvObject<String>> relatedObjects = new ArrayList<KvObject<String>>(count);

    final Iterator<JsonNode> iter = jsonNode.get("results").elements();
    while (iter.hasNext()) {
        relatedObjects.add(jsonToKvObject(objectMapper, iter.next(), String.class));
    }

    return relatedObjects;
}

From source file:org.openepics.discs.ccdb.core.auditlog.PropertyEntityLoggerTest.java

@Test
public void testSerializeEntity() throws JsonProcessingException, IOException {
    final String RESULT = "{\"description\":\"Description of test Property\","
            + "\"typeAssociation\":true,\"slotAssociation\":false,\"deviceAssociation\":false,"
            + "\"alignmentAssociation\":false,\"dataType\":\"Float\",\"unit\":\"Ampere\"}";

    // create an ObjectMapper instance.
    ObjectMapper mapper = new ObjectMapper();
    // use the ObjectMapper to read the json string and create a tree
    JsonNode node = mapper.readTree(RESULT);

    assertEquals(node.get("description").asText(), "Description of test Property");
    assertEquals(node.get("typeAssociation").asBoolean(), true);
    assertEquals(node.get("slotAssociation").asBoolean(), false);
    assertEquals(node.get("deviceAssociation").asBoolean(), false);
    assertEquals(node.get("alignmentAssociation").asBoolean(), false);
    assertEquals(node.get("dataType").asText(), "Float");
    assertEquals(node.get("unit").asText(), "Ampere");
}

From source file:enmasse.controller.flavor.FlavorParserTest.java

@Test
public void testParser() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    File testFile = new File("src/test/resources/flavors.json");
    Map<String, Flavor> parsed = FlavorParser.parse(mapper.readTree(testFile));

    assertThat(parsed.size(), is(2));/*from   ww w.  j  av  a 2 s  . c o m*/
    assertNotNull(parsed.get("test-queue"));
    Flavor flavor = parsed.get("test-queue");
    assertThat(flavor.templateName(), is("queue-persisted"));
    assertThat(flavor.templateParameters().size(), is(1));
    assertThat(flavor.templateParameters().get("STORAGE_CAPACITY"), is("2Gi"));

    flavor = parsed.get("descriptive-queue");

    assertThat(flavor.templateName(), is("queue-inmemory"));
    assertThat(flavor.type(), is("queue"));
    assertThat(flavor.description(), is("This is a simple queue"));
}

From source file:org.switchyard.quickstarts.transform.datamapper.FileBindingsTest.java

private String jsonUnprettyPrint(String jsonString) throws JsonProcessingException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
    JsonNode node = mapper.readTree(jsonString);
    return node.toString();
}

From source file:org.apache.tika.language.translate.Lingo24TranslatorHack.java

@Override
public String translate(String text, String sourceLanguage, String targetLanguage)
        throws TikaException, IOException {
    if (!this.isAvailable()) {
        return text;
    }/*from   w w w .j a va  2 s. co  m*/
    WebClient client = WebClient.create(LINGO24_TRANSLATE_URL_BASE);

    Response response = client.accept(MediaType.APPLICATION_JSON).query("user_key", this.userKey)
            .query("source", sourceLanguage).query("target", targetLanguage).query("q", text).get();
    BufferedReader reader = new BufferedReader(
            new InputStreamReader((InputStream) response.getEntity(), UTF_8));
    String line = null;
    StringBuffer responseText = new StringBuffer();
    while ((line = reader.readLine()) != null) {
        responseText.append(line);
    }

    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonResp = mapper.readTree(responseText.toString());
        if (jsonResp.findValuesAsText("errors").isEmpty()) {
            return jsonResp.findValuesAsText("translation").get(0);
        } else {
            throw new TikaException(jsonResp.findValue("errors").get(0).asText());
        }
    } catch (JsonParseException e) {
        throw new TikaException(
                "Error requesting translation from '" + sourceLanguage + "' to '" + targetLanguage
                        + "', JSON response from Lingo24 is not well formatted: " + responseText.toString());
    }
}

From source file:net.sf.taverna.t2.activities.apiconsumer.ApiConsumerActivityFactory.java

@Override
public JsonNode getActivityConfigurationSchema() {
    ObjectMapper objectMapper = new ObjectMapper();
    try {/*w w w  . j a va 2s. c  om*/
        return objectMapper.readTree(getClass().getResource("/schema.json"));
    } catch (IOException e) {
        return objectMapper.createObjectNode();
    }
}

From source file:org.schedoscope.export.redis.RedisFullTableExportMRTest.java

@Test
public void testRedisFullExport3() throws Exception {

    setUpHiveServer("src/test/resources/test_struct_data.txt", "src/test/resources/test_struct.hql",
            "test_struct");

    final String KEY = "id";
    conf.set(RedisOutputFormat.REDIS_EXPORT_KEY_PREFIX, "export3");
    conf.set(RedisOutputFormat.REDIS_EXPORT_KEY_NAME, KEY);

    Job job = Job.getInstance(conf);//w ww .j  a va  2 s. c o  m

    job.setMapperClass(RedisFullTableExportMapper.class);
    job.setReducerClass(Reducer.class);
    job.setNumReduceTasks(1);
    job.setInputFormatClass(HCatInputFormat.class);
    job.setOutputFormatClass(RedisOutputFormat.class);

    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(RedisHashWritable.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(RedisHashWritable.class);

    assertTrue(job.waitForCompletion(true));
    ObjectMapper objMapper = new ObjectMapper();
    JsonNode node = objMapper.readTree(jedisAdapter
            .hget("export3_1438843758818ab9c238f-c715-4dcc-824f-26346233ccd5-2015-08-20-000030", "type"));
    assertEquals("\"value1\"", node.get("field1").toString());
}