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.jboss.pnc.buildagent.client.BuildAgentClient.java

private Client connectStatusListenerClient(String webSocketBaseUrl,
        Consumer<TaskStatusUpdateEvent> onStatusUpdate, String commandContext) {
    Client client = initializeDefault();
    Consumer<String> responseConsumer = (text) -> {
        log.trace("Decoding response: {}", text);

        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonObject = null;/*from w w  w . ja v  a  2 s.  co m*/
        try {
            jsonObject = mapper.readTree(text);
        } catch (IOException e) {
            log.error("Cannot read JSON string: " + text, e);
        }
        try {
            TaskStatusUpdateEvent taskStatusUpdateEvent = TaskStatusUpdateEvent
                    .fromJson(jsonObject.get("event").toString());
            onStatusUpdate.accept(taskStatusUpdateEvent);
        } catch (IOException e) {
            log.error("Cannot deserialize TaskStatusUpdateEvent.", e);
        }
    };
    client.onStringMessage(responseConsumer);

    client.onClose(closeReason -> {
    });

    commandContext = formatCommandContext(commandContext);

    try {
        client.connect(stripEndingSlash(webSocketBaseUrl) + Client.WEB_SOCKET_LISTENER_PATH + commandContext);
    } catch (Exception e) {
        throw new AssertionError("Failed to connect to remote client.", e);
    }
    return client;
}

From source file:com.apteligent.ApteligentJavaClient.java

/**
 * @param hash The crash hash to retrieve
 * @param diagnostics include detailed diagnostics information for crash
 * @param getOtherCrashes include other crashes and legacy crash groups now part of this group
 * @return Crash object//from   www. j  a va 2 s  . c  om
 */
public Crash getCrash(String hash, boolean diagnostics, boolean getOtherCrashes) {
    String params = "?diagnostics=" + diagnostics + "&get_other_crashes=" + getOtherCrashes;
    Crash crash = null;
    try {
        HttpsURLConnection conn = sendGetRequest(API_CRASH_DETAILS.replace("{hash}", hash), params);
        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jp = jsonFactory.createParser(conn.getInputStream());
        ObjectMapper mapper = getObjectMapper();
        TreeNode node = mapper.readTree(jp);
        crash = mapper.treeToValue(node, Crash.class);
    } catch (IOException ioex) {
        ioex.printStackTrace();
    }
    return crash;
}

From source file:com.gsma.mobileconnect.utils.JsonUtilsTest.java

@Test
public void parseOperatorIdentifiedDiscoveryResult_withInvalidOperatorIdentifiedDiscoveryResult_shouldReturnNull()
        throws IOException {
    // GIVEN//w  ww. j  a  v  a  2s  . com
    String jsonStr = "{ \"response\": {" + " \"client_id\": \"XXX\", " + " \"client_secret\": \"XXX\", "
            + " \"apis\": {" + " \"operatorid\": {" + " \"not-link\": [" + " { \"rel\": \"authorization\", "
            + " \"href\": \"XXX\"}, " + " { \"rel\": \"token\", " + " \"href\": \"XXX\"}, "
            + " { \"rel\": \"userinfo\", " + " \"href\": \"XXX\"}, " + " { \"rel\": \"premiuminfo\", "
            + " \"href\": \"XXX\"} " + "]}}}}";
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(jsonStr);

    // WHEN
    ParsedOperatorIdentifiedDiscoveryResult parsedOperatorIdentifiedDiscoveryResult = JsonUtils
            .parseOperatorIdentifiedDiscoveryResult(root);

    // THEN
    assertNull(parsedOperatorIdentifiedDiscoveryResult);
}

From source file:org.createnet.raptor.models.data.RecordSet.java

public RecordSet(Stream stream, String body) {
    this(stream);
    ObjectMapper mapper = ServiceObject.getMapper();
    try {//from   w ww .j  a  va2 s.  c o m
        parseJson(stream, mapper.readTree(body));
    } catch (IOException ex) {
        throw new RecordsetException(ex);
    }
}

From source file:com.gsma.mobileconnect.utils.JsonUtilsTest.java

@Test
public void getErrorResponse_withNoErrorDescription_shouldReturnDescription() throws IOException {
    // GIVEN/* w  w w.j a v a2 s . c o  m*/
    String expectedError = "EXPECTED ERROR";
    String expectedDescription = "EXPECTED DESCRIPTION";
    String expectedErrorUri = "EXPECTED ERROR_URI";
    String json = "{ \"error\": \"" + expectedError + "\", " + "\"description\": \"" + expectedDescription
            + "\", " + "\"error_uri\": \"" + expectedErrorUri + "\" }";
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(json);

    // WHEN
    ErrorResponse errorResponse = JsonUtils.getErrorResponse(root);

    // THEN
    assertNotNull(errorResponse);
    assertEquals(expectedError, errorResponse.get_error());
    assertEquals(expectedDescription, errorResponse.get_error_description());
    assertEquals(expectedErrorUri, errorResponse.get_error_uri());
}

From source file:com.gsma.mobileconnect.utils.JsonUtilsTest.java

@Test
public void getErrorResponse_withNoDescription_shouldReturnErrorDescription() throws IOException {
    // GIVEN/*from   ww w .  jav a  2s .  com*/
    String expectedError = "EXPECTED ERROR";
    String expectedErrorDescription = "EXPECTED ERROR_DESCRIPTION";
    String expectedErrorUri = "EXPECTED ERROR_URI";
    String json = "{ \"error\": \"" + expectedError + "\", " + "\"error_description\": \""
            + expectedErrorDescription + "\", " + "\"error_uri\": \"" + expectedErrorUri + "\" }";
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(json);

    // WHEN
    ErrorResponse errorResponse = JsonUtils.getErrorResponse(root);

    // THEN
    assertNotNull(errorResponse);
    assertEquals(expectedError, errorResponse.get_error());
    assertEquals(expectedErrorDescription, errorResponse.get_error_description());
    assertEquals(expectedErrorUri, errorResponse.get_error_uri());
}

From source file:com.gsma.mobileconnect.utils.JsonUtilsTest.java

@Test
public void getErrorResponse_withErrorFields_shouldReturnErrorResponse() throws IOException {
    // GIVEN// w w  w.ja v  a2  s . c  o m
    String expectedError = "EXPECTED ERROR";
    String expectedErrorDescription = "EXPECTED ERROR_DESCRIPTION";
    String expectedDescription = "EXPECTED DESCRIPTION";
    String expectedErrorUri = "EXPECTED ERROR_URI";
    String json = "{ \"error\": \"" + expectedError + "\", " + "\"error_description\": \""
            + expectedErrorDescription + "\", " + "\"description\": \"" + expectedDescription + "\", "
            + "\"error_uri\": \"" + expectedErrorUri + "\" }";
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(json);

    // WHEN
    ErrorResponse errorResponse = JsonUtils.getErrorResponse(root);

    // THEN
    assertNotNull(errorResponse);
    assertEquals(expectedError, errorResponse.get_error());
    assertEquals(expectedErrorDescription + " " + expectedDescription, errorResponse.get_error_description());
    assertEquals(expectedErrorUri, errorResponse.get_error_uri());
}

From source file:org.opendaylight.sfc.sbrest.json.SfstateExporterTest.java

private boolean testExportSfstateJson(String expectedResultFile, boolean nameOnly) throws IOException {
    ServiceFunctionState serviceFunctionState;
    String exportedSfstateString;
    SfstateExporterFactory sfstateExporterFactory = new SfstateExporterFactory();

    if (nameOnly) {
        serviceFunctionState = this.buildServiceFunctionStateNameOnly();
        exportedSfstateString = sfstateExporterFactory.getExporter().exportJsonNameOnly(serviceFunctionState);
    } else {//from   w  w  w .  j a  va2s  . c o m
        serviceFunctionState = this.buildServiceFunctionState();
        exportedSfstateString = sfstateExporterFactory.getExporter().exportJson(serviceFunctionState);
    }

    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode expectedSfstateJson = objectMapper
            .readTree(this.gatherServiceFunctionStateJsonStringFromFile(expectedResultFile));
    JsonNode exportedSfstateJson = objectMapper.readTree(exportedSfstateString);

    System.out.println("EXPECTED: " + expectedSfstateJson);
    System.out.println("CREATED:  " + exportedSfstateJson);

    return expectedSfstateJson.equals(exportedSfstateJson);
}

From source file:com.apteligent.ApteligentJavaClient.java

/**
 * @param appID appId (string, optional): The app to retrieve data about,
 * @param metricType The metric to retrieve
 * @param duration can only be 1440 (24 hours) or 43200 (1 month)
 * @param groupBy TODO FILL IN THIS COMMENT
 * @return/* www . ja v a2  s .  c o  m*/
 */
public CrashSummary getErrorPie(String appID, CrashSummary.MetricType metricType, int duration,
        Pie.GroupBy groupBy) {

    String params = "{ \"params\": " + "{\"duration\": " + duration + "," + " \"graph\": \""
            + metricType.toString() + "\"," + " \"appId\": \"" + appID + "\"" + ",\"groupBy\": \""
            + groupBy.toString() + "\"}" + "}";

    CrashSummary crashSummary = null;
    try {
        HttpsURLConnection conn = sendPostRequest(API_ERROR_PIE, params);
        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jp = jsonFactory.createParser(conn.getInputStream());
        ObjectMapper mapper = getObjectMapper();
        TreeNode node = mapper.readTree(jp);
        crashSummary = mapper.treeToValue(node.get("data"), CrashSummary.class);
        if (crashSummary != null) {
            crashSummary.setParams(appID, metricType, duration);
        }
    } catch (IOException ioex) {
        ioex.printStackTrace();
    }
    return crashSummary;
}

From source file:com.apteligent.ApteligentJavaClient.java

/**
 * @param appID appId (string, optional): The app to retrieve data about,
 * @param metricType The metric to retrieve
 * @param duration can only be 1440 (24 hours) or 43200 (1 month)
 * @param groupBy TODO FILL IN THIS COMMENT
 * @return/*from  w  ww. ja va 2  s .com*/
 */
public CrashSummary getErrorSparklines(String appID, CrashSummary.MetricType metricType, int duration,
        Sparklines.GroupBy groupBy) {

    String params = "{ \"params\": " + "{\"duration\": " + duration + "," + " \"graph\": \""
            + metricType.toString() + "\"," + " \"appId\": \"" + appID + "\"" + ",\"groupBy\": \""
            + groupBy.toString() + "\"}" + "}";

    CrashSummary crashSummary = null;
    try {
        HttpsURLConnection conn = sendPostRequest(API_ERROR_SPARKLINES, params);
        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jp = jsonFactory.createParser(conn.getInputStream());
        ObjectMapper mapper = getObjectMapper();
        TreeNode node = mapper.readTree(jp);
        crashSummary = mapper.treeToValue(node.get("data"), CrashSummary.class);
        if (crashSummary != null) {
            crashSummary.setParams(appID, metricType, duration);
        }
    } catch (IOException ioex) {
        ioex.printStackTrace();
    }
    return crashSummary;
}