Search in sources :

Example 66 with JsonString

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

the class APIClient method getListMapStringLongValue.

public Map<String, Long> getListMapStringLongValue(String string, MultivaluedMap<String, String> queryParams) {
    if (string.equals("")) {
        return null;
    }
    JsonReader reader = getReader(string, queryParams);
    JsonArray arr = reader.readArray();
    Map<String, Long> map = new HashMap<String, Long>();
    for (int i = 0; i < arr.size(); i++) {
        JsonObject obj = arr.getJsonObject(i);
        Iterator<String> it = obj.keySet().iterator();
        String key = "";
        long val = -1;
        while (it.hasNext()) {
            String k = it.next();
            if (obj.get(k) instanceof JsonString) {
                key = obj.getString(k);
            } else {
                val = obj.getJsonNumber(k).longValue();
            }
        }
        if (val > 0 && !key.equals("")) {
            map.put(key, val);
        }
    }
    reader.close();
    return map;
}
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) JsonString(javax.json.JsonString)

Example 67 with JsonString

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

the class APIClient method getMapStringDouble.

public Map<String, Double> getMapStringDouble(String string, MultivaluedMap<String, String> queryParams) {
    if (string.equals("")) {
        return null;
    }
    JsonReader reader = getReader(string, queryParams);
    JsonArray arr = reader.readArray();
    Map<String, Double> map = new HashMap<String, Double>();
    for (int i = 0; i < arr.size(); i++) {
        JsonObject obj = arr.getJsonObject(i);
        Iterator<String> it = obj.keySet().iterator();
        String key = "";
        double val = -1;
        while (it.hasNext()) {
            String k = it.next();
            if (obj.get(k) instanceof JsonString) {
                key = obj.getString(k);
            } else {
                val = obj.getJsonNumber(k).doubleValue();
            }
        }
        if (!key.equals("")) {
            map.put(key, val);
        }
    }
    reader.close();
    return map;
}
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) JsonString(javax.json.JsonString)

Example 68 with JsonString

use of javax.json.JsonString in project iobserve-analysis by research-iobserve.

the class SendHttpRequestUtils method post.

/**
 * Send change log updates to the visualization.
 *
 * @param modelData
 *            actual data that will be send
 * @param systemUrl
 *            Url for sending a system element to the visualization
 * @param changelogUrl
 *            Url for sending changelogs to the visualization
 * @throws IOException
 *             if the input stram could not be read
 */
public static void post(final JsonObject modelData, final URL systemUrl, final URL changelogUrl) throws IOException {
    final HttpURLConnection connection;
    // prepare data for changelog constraint of deployment visualization
    final JsonArray dataArray = Json.createArrayBuilder().add(modelData).build();
    final JsonString type = (JsonString) modelData.get("type");
    if ("system".equals(type.getString())) {
        connection = (HttpURLConnection) systemUrl.openConnection();
    } else {
        connection = (HttpURLConnection) changelogUrl.openConnection();
    }
    // add request header
    connection.setRequestMethod("POST");
    connection.setRequestProperty("content-type", "application/json; charset=utf-8");
    connection.setRequestProperty("User-Agent", SendHttpRequestUtils.USER_AGENT);
    connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
    // Send post request
    connection.setDoOutput(true);
    final JsonWriter jsonWriter = Json.createWriter(connection.getOutputStream());
    if ("system".equals(type.getString())) {
        jsonWriter.write(modelData);
        SendHttpRequestUtils.LOGGER.debug("Sending 'POST' request to URL : {}", systemUrl);
        SendHttpRequestUtils.LOGGER.debug("Post parameters : {}", modelData);
    } else {
        // work in progress
        jsonWriter.writeArray(dataArray);
        SendHttpRequestUtils.LOGGER.debug("Sending 'POST' request to URL : {}", changelogUrl);
        SendHttpRequestUtils.LOGGER.debug("Post parameters : {}", dataArray);
    }
    jsonWriter.close();
    final int responseCode = connection.getResponseCode();
    SendHttpRequestUtils.LOGGER.debug("Response Code : {}", responseCode);
    if (responseCode != 204) {
        final BufferedReader err = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
        String errLine;
        final StringBuffer error = new StringBuffer();
        while ((errLine = err.readLine()) != null) {
            error.append(errLine);
        }
        err.close();
        SendHttpRequestUtils.LOGGER.error("error:", error);
    }
    final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String inputLine;
    final StringBuffer response = new StringBuffer();
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    // print result
    SendHttpRequestUtils.LOGGER.debug(response.toString());
}
Also used : JsonArray(javax.json.JsonArray) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) JsonString(javax.json.JsonString) JsonString(javax.json.JsonString) JsonWriter(javax.json.JsonWriter)

Example 69 with JsonString

use of javax.json.JsonString in project iaf by ibissource.

the class Json2Xml method processChildElement.

@Override
protected void processChildElement(JsonValue node, String parentName, XSElementDeclaration childElementDeclaration, boolean mandatory, Set<String> processedChildren) throws SAXException {
    String childElementName = childElementDeclaration.getName();
    if (log.isTraceEnabled())
        log.trace("parentName [" + parentName + "] childElementName [" + childElementName + "] node [" + node + "] isParentOfSingleMultipleOccurringChildElement [" + isParentOfSingleMultipleOccurringChildElement() + "]");
    if (isParentOfSingleMultipleOccurringChildElement()) {
        if (node instanceof JsonArray) {
            if (log.isTraceEnabled())
                log.trace("array child node is JsonArray, handling each of the elements as a [" + childElementName + "]");
            JsonArray ja = (JsonArray) node;
            for (JsonValue child : ja) {
                handleElement(childElementDeclaration, child);
            }
            // mark that we have processed the array elements
            processedChildren.add(childElementName);
            return;
        }
        if (node instanceof JsonString) {
            // support normal (non list) parameters to supply array element values
            if (log.isTraceEnabled())
                log.trace("array child node is JsonString, handling as a [" + childElementName + "]");
            handleElement(childElementDeclaration, node);
            // mark that we have processed the array element
            processedChildren.add(childElementName);
            return;
        }
    }
    super.processChildElement(node, parentName, childElementDeclaration, mandatory, processedChildren);
}
Also used : JsonArray(javax.json.JsonArray) JsonValue(javax.json.JsonValue) JsonString(javax.json.JsonString) JsonString(javax.json.JsonString)

Example 70 with JsonString

use of javax.json.JsonString in project wring by yegor256.

the class IgnoreEvents method pattern.

/**
 * Make regex from a JSON config.
 * @param json JSON config
 * @return Pattern
 * @throws Agent.UserException If fails
 */
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private static Pattern pattern(final JsonObject json) throws Agent.UserException {
    final Collection<Pattern> ptns = new LinkedList<>();
    final JsonValue value = json.get("ignore");
    if (value == null) {
        ptns.add(Pattern.compile("never^"));
    } else {
        if (!(value instanceof JsonArray)) {
            throw new Agent.UserException("Element 'ignore' must be an array");
        }
        final JsonArray ignore = value.asJsonArray();
        ptns.addAll(ignore.getValuesAs(JsonString.class).stream().map(JsonString::getString).map(IgnoreEvents::pattern).collect(Collectors.toList()));
    }
    return Pattern.compile(String.format("(%s)", ptns.stream().map(Pattern::toString).collect(Collectors.joining(")|("))));
}
Also used : JsonArray(javax.json.JsonArray) Pattern(java.util.regex.Pattern) JsonValue(javax.json.JsonValue) JsonString(javax.json.JsonString) LinkedList(java.util.LinkedList)

Aggregations

JsonString (javax.json.JsonString)78 JsonObject (javax.json.JsonObject)54 JsonReader (javax.json.JsonReader)36 StringReader (java.io.StringReader)34 Test (org.junit.Test)30 JsonArray (javax.json.JsonArray)24 HashMap (java.util.HashMap)17 JsonValue (javax.json.JsonValue)16 LinkedList (java.util.LinkedList)10 ArrayList (java.util.ArrayList)8 Map (java.util.Map)8 JsonNumber (javax.json.JsonNumber)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 JsonException (javax.json.JsonException)5 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)5 ProcessGroupStatus (org.apache.nifi.controller.status.ProcessGroupStatus)5 RemoteProcessGroupStatus (org.apache.nifi.controller.status.RemoteProcessGroupStatus)5 POST (javax.ws.rs.POST)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3