Search in sources :

Example 81 with JsonReader

use of javax.json.JsonReader in project scylla-jmx by scylladb.

the class APIClient method getMapStringLongValue.

public Map<String, Long> getMapStringLongValue(String string, MultivaluedMap<String, String> queryParams) {
    Map<String, Long> res = new HashMap<String, Long>();
    JsonReader reader = getReader(string, queryParams);
    JsonArray arr = reader.readArray();
    JsonObject obj = null;
    for (int i = 0; i < arr.size(); i++) {
        obj = arr.getJsonObject(i);
        res.put(obj.getString("key"), obj.getJsonNumber("value").longValue());
    }
    return res;
}
Also used : JsonArray(javax.json.JsonArray) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString)

Example 82 with JsonReader

use of javax.json.JsonReader in project scylla-jmx by scylladb.

the class APIClient method getJsonArray.

public JsonArray getJsonArray(String string, MultivaluedMap<String, String> queryParams) {
    if (string.equals("")) {
        return null;
    }
    JsonReader reader = getReader(string, queryParams);
    JsonArray res = reader.readArray();
    reader.close();
    return res;
}
Also used : JsonArray(javax.json.JsonArray) JsonReader(javax.json.JsonReader)

Example 83 with JsonReader

use of javax.json.JsonReader in project knime-core by knime.

the class BugAP7806_WorkflowSaveHooks method testOpenAPIGeneration.

/**
 * Checks if the OpenAPI definition for the input parameters is generated correctly.
 *
 * @throws Exception if an error occurs
 */
@Test
public void testOpenAPIGeneration() throws Exception {
    getManager().setDirty();
    getManager().save(m_workflowDir, new ExecutionMonitor(), true);
    Path openApiFragmentFile = m_workflowDir.toPath().resolve(WorkflowSaveHook.ARTIFACTS_FOLDER_NAME).resolve(OpenAPIDefinitionGenerator.INPUT_PARAMETERS_FILE);
    String actualInputParameters;
    try (JsonReader reader = Json.createReader(Files.newInputStream(openApiFragmentFile))) {
        JsonObject o = reader.readObject();
        try (StringWriter sw = new StringWriter();
            JsonWriter writer = Json.createWriter(sw)) {
            writer.write(o);
            actualInputParameters = sw.toString();
        }
    }
    String expectedInputParameters;
    try (JsonReader reader = Json.createReader(Files.newInputStream(m_workflowDir.toPath().resolve("openapi-input-parameters-reference.json")))) {
        JsonObject o = reader.readObject();
        try (StringWriter sw = new StringWriter();
            JsonWriter writer = Json.createWriter(sw)) {
            writer.write(o);
            expectedInputParameters = sw.toString();
        }
    }
    assertThat("Unexpected input parameters definition written", actualInputParameters, is(expectedInputParameters));
}
Also used : Path(java.nio.file.Path) StringWriter(java.io.StringWriter) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) JsonWriter(javax.json.JsonWriter) Test(org.junit.Test)

Example 84 with JsonReader

use of javax.json.JsonReader in project Payara by payara.

the class JerseyRestResponse method getResponse.

/**
 * <p> This method abstracts the physical response to return a consistent
 * data structure.</p>
 */
@Override
public Map<String, Object> getResponse() {
    // Prepare the result object
    Map<String, Object> result = new HashMap<String, Object>(5);
    // Add the Response Code
    result.put("responseCode", getResponseCode());
    // Add the Response Body
    // FIXME: Do not put responseBody into the Map... too big, not needed
    result.put("responseBody", getResponseBody());
    String contentType = response.getHeaderString("Content-type");
    if (contentType != null) {
        String responseBody = getResponseBody();
        contentType = contentType.toLowerCase(GuiUtil.guiLocale);
        if (contentType.startsWith("application/xml")) {
            InputStream input = null;
            try {
                XMLInputFactory inputFactory = XMLInputFactory.newInstance();
                inputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false);
                input = new ByteArrayInputStream(responseBody.trim().getBytes("UTF-8"));
                XMLStreamReader parser = inputFactory.createXMLStreamReader(input);
                while (parser.hasNext()) {
                    int event = parser.next();
                    switch(event) {
                        case XMLStreamConstants.START_ELEMENT:
                            {
                                if ("map".equals(parser.getLocalName())) {
                                    result.put("data", processXmlMap(parser));
                                }
                                break;
                            }
                        default:
                            break;
                    }
                }
            } catch (Exception ex) {
                Logger.getLogger(RestResponse.class.getName()).log(Level.SEVERE, null, ex);
                throw new RuntimeException(ex);
            } finally {
                try {
                    if (input != null) {
                        input.close();
                    }
                } catch (IOException ex) {
                    Logger.getLogger(RestResponse.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        // // If XML...
        // Document document = MiscUtil.getDocument(getResponseBody());
        // Element root = document.getDocumentElement();
        // if ("action-report".equalsIgnoreCase(root.getNodeName())) {
        // // Default XML document type...
        // // Add the Command Description
        // result.put("description", root.getAttribute("description"));
        // result.put("exit-code", root.getAttribute("exit-code"));
        // 
        // // Add the messages
        // List<Map<String, Object>> messages = new ArrayList<Map<String, Object>>(2);
        // result.put("messages", messages);
        // 
        // // Iterate over each node looking for message-part
        // NodeList nl = root.getChildNodes();
        // int len = nl.getLength();
        // Node child;
        // for (int idx = 0; idx < len; idx++) {
        // child = nl.item(idx);
        // if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getNodeName().equals("message-part"))) {
        // messages.add(processMessagePart(child));
        // }
        // }
        // } else {
        // // Generate a generic Java structure from the XML
        // result.put("data", getJavaFromXML(root));
        // }
        } else if (contentType.startsWith("application/json")) {
            // Decode JSON
            JsonReader reader = Json.createReader(new StringReader(responseBody));
            JsonObject object = reader.readObject();
            result.put("data", JsonUtil.jsonObjectToMap(object));
        } else {
            // Unsupported Response Format!
            System.out.println("Unsupported Response Format: '" + contentType + "'!");
        }
    }
    // Return the populated result data structure
    return result;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JsonObject(javax.json.JsonObject) IOException(java.io.IOException) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 85 with JsonReader

use of javax.json.JsonReader in project Payara by payara.

the class RequestTraceTest method testJSONParse.

@Test
public void testJSONParse() {
    RequestTraceSpan re = new RequestTraceSpan(EventType.TRACE_START, "Start");
    trace.addEvent(re);
    for (int i = 0; i < 1000; i++) {
        re = new RequestTraceSpan("Event" + i);
        trace.addEvent(re);
    }
    trace.endTrace();
    String jsonString = trace.toString();
    JsonReader jsonReader = Json.createReader(new StringReader(jsonString));
    jsonReader.readObject();
}
Also used : StringReader(java.io.StringReader) RequestTraceSpan(fish.payara.notification.requesttracing.RequestTraceSpan) JsonReader(javax.json.JsonReader) Test(org.junit.Test)

Aggregations

JsonReader (javax.json.JsonReader)130 JsonObject (javax.json.JsonObject)110 StringReader (java.io.StringReader)78 Test (org.junit.Test)47 JsonArray (javax.json.JsonArray)44 JsonString (javax.json.JsonString)42 HashMap (java.util.HashMap)21 IOException (java.io.IOException)17 ArrayList (java.util.ArrayList)13 File (java.io.File)10 LinkedHashMap (java.util.LinkedHashMap)10 JsonParser (edu.harvard.iq.dataverse.util.json.JsonParser)9 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)8 ByteArrayInputStream (java.io.ByteArrayInputStream)8 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)8 InputStream (java.io.InputStream)7 Gson (com.google.gson.Gson)6 AsyncCompletionHandler (com.ning.http.client.AsyncCompletionHandler)6 Response (com.ning.http.client.Response)6 JsonParseException (edu.harvard.iq.dataverse.util.json.JsonParseException)5