Search in sources :

Example 61 with JsonArray

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

the class StreamManager method getState.

public Set<StreamState> getState() {
    JsonArray arr = client.getJsonArray("/stream_manager/");
    Set<StreamState> res = new HashSet<StreamState>();
    for (int i = 0; i < arr.size(); i++) {
        JsonObject obj = arr.getJsonObject(i);
        res.add(new StreamState(obj.getString("plan_id"), obj.getString("description"), SessionInfo.fromJsonArr(obj.getJsonArray("sessions"))));
    }
    return res;
}
Also used : JsonArray(javax.json.JsonArray) JsonObject(javax.json.JsonObject) HashSet(java.util.HashSet)

Example 62 with JsonArray

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

the class OpenAPIDefinitionGenerator method translateToSchema.

private static JsonObjectBuilder translateToSchema(final JsonValue v) {
    final JsonObjectBuilder node = Json.createObjectBuilder();
    if (v instanceof JsonObject) {
        node.add("type", "object");
        final JsonObjectBuilder properties = Json.createObjectBuilder();
        for (Map.Entry<String, JsonValue> e : ((JsonObject) v).entrySet()) {
            final JsonObjectBuilder child = translateToSchema(e.getValue());
            properties.add(e.getKey(), child);
        }
        node.add("properties", properties);
    } else if (v instanceof JsonNumber) {
        final JsonNumber number = (JsonNumber) v;
        if (number.isIntegral()) {
            node.add("type", "integer");
            node.add("default", number.longValue());
        } else {
            node.add("type", "number");
            node.add("default", number.doubleValue());
        }
    } else if (v instanceof JsonString) {
        node.add("type", "string");
        node.add("default", ((JsonString) v).getString());
    } else if ((v.getValueType() == ValueType.FALSE) || (v.getValueType() == ValueType.TRUE)) {
        node.add("type", "boolean");
        node.add("default", v.toString());
    } else if (v instanceof JsonArray) {
        node.add("type", "array");
        if (!((JsonArray) v).isEmpty()) {
            JsonObjectBuilder child = translateToSchema(((JsonArray) v).get(0));
            node.add("items", child);
        }
    }
    return node;
}
Also used : JsonArray(javax.json.JsonArray) JsonNumber(javax.json.JsonNumber) JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonString(javax.json.JsonString) JsonObjectBuilder(javax.json.JsonObjectBuilder) Map(java.util.Map)

Example 63 with JsonArray

use of javax.json.JsonArray in project cxf by apache.

the class JsrJsonpProviderTest method testPostAndGetBooks.

@Test
public void testPostAndGetBooks() {
    testPostSimpleJsonObject();
    final Response r = createWebClient("/bookstore/books").get();
    assertEquals(Status.OK.getStatusCode(), r.getStatus());
    final JsonArray obj = r.readEntity(JsonArray.class);
    assertThat(obj.size(), equalTo(1));
    assertThat(obj.get(0), instanceOf(JsonObject.class));
    assertThat(((JsonObject) obj.get(0)).getInt("id"), equalTo(1));
    assertThat(((JsonObject) obj.get(0)).getString("name"), equalTo("Book 1"));
}
Also used : Response(javax.ws.rs.core.Response) JsonArray(javax.json.JsonArray) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 64 with JsonArray

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

the class RemoteRestAdminCommand method parseMetadata.

/**
 * Parse the JSon metadata for the command.
 *
 * @param str the string
 * @return the etag to compare the command cache model
 */
private CachedCommandModel parseMetadata(String str, String etag) {
    if (logger.isLoggable(Level.FINER)) {
        // XXX - assume "debug" == "FINER"
        logger.finer("------- RAW METADATA RESPONSE ---------");
        logger.log(Level.FINER, "ETag: {0}", etag);
        logger.finer(str);
        logger.finer("------- RAW METADATA RESPONSE ---------");
    }
    if (str == null) {
        return null;
    }
    try {
        boolean sawFile = false;
        JsonParser parser = Json.createParser(new StringReader(str));
        parser.next();
        JsonObject obj = parser.getObject();
        obj = obj.getJsonObject("command");
        CachedCommandModel cm = new CachedCommandModel(obj.getString("@name"), etag);
        cm.dashOk = parseBoolean(obj, "@unknown-options-are-operands", false);
        cm.managedJob = parseBoolean(obj, "@managed-job", false);
        cm.setUsage(obj.getString("usage", null));
        JsonValue optns = obj.get("option");
        if (!JsonValue.NULL.equals(optns) && optns != null) {
            JsonArray jsonOptions;
            if (optns instanceof JsonArray) {
                jsonOptions = (JsonArray) optns;
            } else {
                JsonArrayBuilder optBuilder = Json.createArrayBuilder();
                optBuilder.add(optns);
                jsonOptions = optBuilder.build();
            }
            for (int i = 0; i < jsonOptions.size(); i++) {
                JsonObject jsOpt = jsonOptions.getJsonObject(i);
                String type = jsOpt.getString("@type");
                ParamModelData opt = new ParamModelData(jsOpt.getString("@name"), typeOf(type), parseBoolean(jsOpt, "@optional", false), jsOpt.getString("@default", null), jsOpt.getString("@short", null), parseBoolean(jsOpt, "@obsolete", false), jsOpt.getString("@alias", null));
                opt.param._acceptableValues = jsOpt.getString("@acceptable-values", "");
                if ("PASSWORD".equals(type)) {
                    opt.param._password = true;
                    opt.prompt = jsOpt.getString("@prompt", null);
                    opt.promptAgain = jsOpt.getString("@prompt-again", null);
                } else if ("FILE".equals(type)) {
                    sawFile = true;
                }
                if (parseBoolean(jsOpt, "@primary", false)) {
                    opt.param._primary = true;
                }
                if (parseBoolean(jsOpt, "@multiple", false)) {
                    if (opt.type == File.class) {
                        opt.type = File[].class;
                    } else {
                        opt.type = List.class;
                    }
                    opt.param._multiple = true;
                }
                cm.add(opt);
            }
        }
        if (sawFile) {
            cm.add(new ParamModelData("upload", Boolean.class, true, null));
            addedUploadOption = true;
            cm.setAddedUploadOption(true);
        }
        if (notify) {
            cm.add(new ParamModelData("notify", Boolean.class, false, "false"));
        }
        this.usage = cm.getUsage();
        return cm;
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Can not parse command metadata", ex);
        return null;
    }
}
Also used : JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) ParamModelData(com.sun.enterprise.admin.util.CommandModelData.ParamModelData) CachedCommandModel(com.sun.enterprise.admin.util.CachedCommandModel) SSLException(javax.net.ssl.SSLException) JsonArray(javax.json.JsonArray) JsonArrayBuilder(javax.json.JsonArrayBuilder) SmartFile(com.sun.enterprise.universal.io.SmartFile) JsonParser(javax.json.stream.JsonParser)

Example 65 with JsonArray

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

the class ActionReportJsonProprietaryReader method fillActionReport.

/**
 * Turns a {@link JsonObject} into an {@link ActionReport}
 * <p>
 * The Json Object must contain a valid {@code exit_code} at the top level.
 * @param ar
 * @param json
 * @throws JsonException
 */
public static void fillActionReport(final ActionReport ar, final JsonObject json) throws JsonException {
    ar.setActionExitCode(ActionReport.ExitCode.valueOf(json.getString("exit_code")));
    ar.setActionDescription(json.getString("command", null));
    String failure = json.getString("failure_cause", null);
    if (failure != null && !failure.isEmpty()) {
        ar.setFailureCause(new Exception(failure));
    }
    ar.setExtraProperties((Properties) extractMap(json.getJsonObject("extraProperties"), new Properties()));
    ar.getTopMessagePart().setMessage(json.getString("top_message", json.getString("message", null)));
    Properties props = (Properties) extractMap(json.getJsonObject("properties"), new Properties());
    for (Map.Entry entry : props.entrySet()) {
        Object entryValue = entry.getValue();
        // See difference between JsonString.toString and JsonString.getValue
        if (entryValue instanceof JsonString) {
            ar.getTopMessagePart().addProperty(String.valueOf(entry.getKey()), ((JsonString) entry.getValue()).getString());
        } else {
            ar.getTopMessagePart().addProperty(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
        }
    }
    // Sub messages
    fillSubMessages(ar.getTopMessagePart(), json.getJsonArray("children"));
    // Sub action reports
    JsonArray subJsons = json.getJsonArray("subReports");
    if (subJsons != null) {
        for (int i = 0; i < subJsons.size(); i++) {
            JsonObject subJson = subJsons.getJsonObject(i);
            fillActionReport(ar.addSubActionsReport(), subJson);
        }
    }
}
Also used : JsonArray(javax.json.JsonArray) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonString(javax.json.JsonString) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map) JsonException(javax.json.JsonException)

Aggregations

JsonArray (javax.json.JsonArray)128 JsonObject (javax.json.JsonObject)97 Test (org.junit.Test)42 ArrayList (java.util.ArrayList)32 JsonReader (javax.json.JsonReader)31 HashMap (java.util.HashMap)29 JsonString (javax.json.JsonString)28 HashSet (java.util.HashSet)21 NameValuePair (org.apache.commons.httpclient.NameValuePair)20 Credentials (org.apache.commons.httpclient.Credentials)19 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)19 StringReader (java.io.StringReader)18 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)17 LinkedHashMap (java.util.LinkedHashMap)14 JsonValue (javax.json.JsonValue)11 Map (java.util.Map)10 JsonException (javax.json.JsonException)9 Response (javax.ws.rs.core.Response)9 IOException (java.io.IOException)7 JerseyTest (org.glassfish.jersey.test.JerseyTest)7