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:capital.scalable.restdocs.response.ArrayLimitingJsonContentModifierTest.java

@Test
public void testModifyJsonWithMoreThanThreeItems() throws Exception {
    // given// w w  w .ja v  a2 s . c o m
    ObjectMapper mapper = new ObjectMapper();
    ArrayLimitingJsonContentModifier modifier = new ArrayLimitingJsonContentModifier(mapper);
    WithArray object = WithArray.withItems(100);
    JsonNode root = mapper.readTree(mapper.writeValueAsString(object));
    // when
    modifier.modifyJson(root);
    // then
    assertThat(root.get("items").size(), is(3));
    assertThat(root.get("ignore").isNumber(), is(true));
}

From source file:io.github.microcks.util.postman.PostmanTestStepsRunner.java

/**
 * Build a new PostmanTestStepsRunner for a collection.
 * @param collectionFilePath The path to SoapUI project file
 * @throws java.io.IOException if file cannot be found or accessed.
 *///from  ww  w  . jav  a  2  s  .c  o  m
public PostmanTestStepsRunner(String collectionFilePath) throws IOException {
    try {
        // Read Json bytes.
        byte[] jsonBytes = Files.readAllBytes(Paths.get(collectionFilePath));
        // Convert them to Node using Jackson object mapper.
        ObjectMapper mapper = new ObjectMapper();
        collection = mapper.readTree(jsonBytes);
    } catch (Exception e) {
        throw new IOException("Postman collection file");
    }
}

From source file:jsonbrowse.JsonBrowse.java

/**
 * Update tree to reflect current JSON data in file.
 * /*  w w w .  j  a v a 2 s.c  o m*/
 * @param absoluteFileName *absolute* path of file.
 * @throws FileNotFoundException
 * @throws IOException 
 */
private void updateModel() throws FileNotFoundException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode rootJsonNode = mapper.readTree(new FileInputStream(jsonFilePath.toString()));

    // Construct tree model
    DefaultMutableTreeNode rootNode = buildTree("root", rootJsonNode);

    jTree.setModel(new DefaultTreeModel(rootNode));
}

From source file:it.tai.solr.http.HttpInvoker.java

public SolrResponse getSolrReport(REPORT_ACTION action) throws IOException, UnsupportedActionException {

    String url;/*  w w w  .jav  a  2  s.  co  m*/

    switch (action) {
    case SUMMARY:
        url = solrURL + "action=" + REPORT_ACTION.SUMMARY.getCode() + "&wt=json";
        break;
    case REPORT:
        url = solrURL + "action=" + REPORT_ACTION.REPORT.getCode() + "&wt=json";
        break;
    default:
        throw new UnsupportedActionException("Unsupported report action");
    }

    CloseableHttpClient httpclient = HttpClients.createDefault();

    try {
        HttpGet httpget = new HttpGet(url);
        logger.info("Excuting SOLR report request: " + httpget.getRequestLine());
        long currTime = System.currentTimeMillis();

        CloseableHttpResponse response = httpclient.execute(httpget);
        responseTime("SOLR report ", currTime);

        checkResponse(response);

        String data = EntityUtils.toString(response.getEntity());

        if (logger.isDebugEnabled()) {
            logger.debug("Response data \n" + data);
        }

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS, true);

        JsonNode root = mapper.readTree(data).path("Summary").path("alfresco").path("Searcher");

        SolrResponse toReturn = mapper.readValue(root.traverse(), SolrResponse.class);
        toReturn.setRawResponse(data);
        toReturn.setUrl(httpget.getRequestLine().getUri());

        return toReturn;

    } finally {
        httpclient.close();
    }
}

From source file:io.apicurio.hub.api.bitbucket.BitbucketSourceConnectorTest.java

@Test
@Ignore/*from  w  w w.  j a v a  2s .  c  om*/
public void testUpdateResourceContent() throws SourceConnectorException, BitbucketException, NotFoundException,
        UnirestException, JsonProcessingException, IOException {
    String repositoryUrl = "https://bitbucket.org/apicurio/apicurio-test/src/1b684236c6434bc5c6644cbf62c46bbd8d40f3d1/junit-apis/test-update-content.json?at=master&fileviewer=file-view-default";

    ResourceContent content = service.getResourceContent(repositoryUrl);
    Assert.assertTrue(content.getContent().contains("Animation API"));
    Assert.assertNotNull(content.getSha());

    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(content.getContent());
    ObjectNode info = (ObjectNode) root.get("info");
    long newVersion = System.currentTimeMillis();
    info.set("version", TextNode.valueOf(String.valueOf(newVersion)));

    System.out.println("Setting new version to: " + newVersion);

    String newContent = mapper.writeValueAsString(root);
    content.setContent(newContent);
    String newSha = service.updateResourceContent(repositoryUrl, "Unit Test: Update Content",
            "Updated the version of: " + repositoryUrl, content);
    System.out.println("New SHA: " + newSha);
}

From source file:org.springframework.data.rest.webmvc.json.DomainObjectReaderUnitTests.java

/**
 * @see DATAREST-705/*from w ww  . ja  v  a2s  .c  om*/
 */
@Test
public void doesNotWipeIdAndVersionPropertyForPut() throws Exception {

    VersionedType type = new VersionedType();
    type.id = 1L;
    type.version = 1L;
    type.firstname = "Dave";

    ObjectMapper mapper = new ObjectMapper();
    ObjectNode node = (ObjectNode) mapper.readTree("{ \"lastname\" : \"Matthews\" }");

    VersionedType result = reader.readPut(node, type, mapper);

    assertThat(result.lastname, is("Matthews"));
    assertThat(result.firstname, is(nullValue()));
    assertThat(result.id, is(1L));
    assertThat(result.version, is(1L));
}

From source file:com.amazonaws.service.apigateway.importer.impl.SchemaTransformer.java

JsonNode deserialize(String schemaText) {
    try {/*from w ww  .ja va 2  s  . c o  m*/
        if (StringUtils.isBlank(schemaText) || schemaText.equals("null")) {
            schemaText = "{}";
        }
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readTree(schemaText);
    } catch (IOException e) {
        throw new IllegalStateException("Invalid schema found. Could not deserialize schema: " + schemaText, e);
    }
}

From source file:fr.paris.lutece.portal.web.upload.UploadServletTest.java

public void testDoPost_Files_NoHandler() throws Exception {
    MockHttpServletRequest request = getMultipartRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    MultipartHttpServletRequest multipartRequest = MultipartUtil.convert(10000, 10000, false, request);

    new UploadServlet().doPost(multipartRequest, response);

    String strResponseJson = response.getContentAsString();
    System.out.println(strResponseJson);

    String strRefJson = "{\"files\":[{\"fileName\":\"file1\",\"fileSize\":3}]}";
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode objectNodeRef = objectMapper.readTree(strRefJson);
    JsonNode objectNodeJson = objectMapper.readTree(strResponseJson);

    assertEquals(objectNodeRef, objectNodeJson);
}

From source file:fr.paris.lutece.portal.web.upload.UploadServletTest.java

/**
 * Test of doPost method, of class fr.paris.lutece.portal.web.upload.UploadServlet.
 *//*from   w w  w .ja v  a  2 s . c  o m*/
public void testDoPost_NoFiles_NoHandler() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    MultipartHttpServletRequest multipartRequest = new MultipartHttpServletRequest(request, new HashMap(),
            new HashMap());

    new UploadServlet().doPost(multipartRequest, response);

    String strResponseJson = response.getContentAsString();
    System.out.println(strResponseJson);

    String strRefJson = "{\"files\":[]}";
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode objectNodeRef = objectMapper.readTree(strRefJson);
    JsonNode objectNodeJson = objectMapper.readTree(strResponseJson);

    assertEquals(objectNodeRef, objectNodeJson);
}