Search in sources :

Example 26 with JsonString

use of javax.json.JsonString 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 : Entry(java.util.Map.Entry) 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)

Example 27 with JsonString

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

the class ActionReportJsonProprietaryReader method fillSubMessages.

/**
 * Fills all messages below top_message of an action report
 * @param mp
 * @param json
 * @throws JsonException
 */
private static void fillSubMessages(final ActionReport.MessagePart mp, final JsonArray json) throws JsonException {
    if (json == null) {
        return;
    }
    for (int i = 0; i < json.size(); i++) {
        JsonObject subJson = json.getJsonObject(i);
        MessagePart child = mp.addChild();
        child.setMessage(subJson.getString("message", null));
        Properties props = (Properties) extractMap(subJson.getJsonObject("properties"), new Properties());
        for (Map.Entry entry : props.entrySet()) {
            Object entryValue = entry.getValue();
            if (entryValue instanceof JsonString) {
                child.addProperty(String.valueOf(entry.getKey()), ((JsonString) entry.getValue()).getString());
            } else {
                child.addProperty(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
            }
            child.addProperty(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
        }
        fillSubMessages(child, subJson.getJsonArray("children"));
    }
}
Also used : Entry(java.util.Map.Entry) MessagePart(org.glassfish.api.ActionReport.MessagePart) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map)

Example 28 with JsonString

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

the class CompositeUtil method unmarshallClass.

/**
 * Convert the given <code>RestModel</code> encoded as Json to a live Java Object.
 *
 * @param locale
 * @param modelClass The target <code>RestModel</code> type
 * @param json       The json encoding of the object
 * @return
 */
public <T> T unmarshallClass(Locale locale, Class<T> modelClass, JsonObject json) throws JsonException {
    T model = getModel(modelClass);
    for (Method setter : getSetters(modelClass)) {
        String name = setter.getName();
        String attribute = name.substring(3, 4).toLowerCase(Locale.getDefault()) + name.substring(4);
        Type param0 = setter.getGenericParameterTypes()[0];
        Class class0 = setter.getParameterTypes()[0];
        if (json.containsKey(attribute)) {
            java.lang.Object o = json.get(attribute);
            if (JsonArray.class.isAssignableFrom(o.getClass())) {
                Object values = processJsonArray(locale, param0, (JsonArray) o);
                invoke(locale, setter, attribute, model, values);
            } else if (JsonObject.class.isAssignableFrom(o.getClass())) {
                invoke(locale, setter, attribute, model, unmarshallClass(locale, class0, (JsonObject) o));
            } else if (JsonString.class.isAssignableFrom(o.getClass())) {
                System.out.println(o);
                invoke(locale, setter, attribute, model, ((JsonString) o).getString());
            } else if (JsonNumber.class.isAssignableFrom(o.getClass())) {
                invoke(locale, setter, attribute, model, ((JsonNumber) o).numberValue());
            } else {
                if ("null".equals(o.toString())) {
                    o = null;
                } else if ("true".equals(o.toString())) {
                    o = true;
                } else if ("false".equals(o.toString())) {
                    o = false;
                }
                if (!isUnmodifiedConfidentialProperty(modelClass, name, o)) {
                    invoke(locale, setter, attribute, model, o);
                }
            }
        }
    }
    return model;
}
Also used : ValueType(javax.json.JsonValue.ValueType) Type(java.lang.reflect.Type) ParameterizedType(java.lang.reflect.ParameterizedType) JsonNumber(javax.json.JsonNumber) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject) Method(java.lang.reflect.Method) JsonString(javax.json.JsonString)

Example 29 with JsonString

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

the class MBeanAttributeReadHandlerTest method getValueObject_String.

@Test
public void getValueObject_String() throws Exception {
    MBeanAttributeReadHandler handler = new MBeanAttributeReadHandler(delegateMock, BEAN_NAME, BEAN_ATTRIBUTE);
    when(delegateMock.getMBeanAttribute(BEAN_NAME, BEAN_ATTRIBUTE)).thenReturn("Payara");
    JsonValue value = handler.getValueObject();
    assertThat(value.getValueType()).isEqualTo(JsonValue.ValueType.STRING);
    assertThat(((JsonString) value).getString()).isEqualTo("Payara");
}
Also used : JsonValue(javax.json.JsonValue) JsonString(javax.json.JsonString) Test(org.junit.Test)

Example 30 with JsonString

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

the class HubStatistics method getStatistics.

/**
 * Gets the statistics for the specified key.
 *
 * @param key The key of the statistics.
 * @return The value, or <code>null</code> if no such value exists.
 */
private static String getStatistics(final String key) {
    try {
        final JsonObject stats = readHubStats(getStatisticsLocation());
        final JsonValue val = stats.get(key);
        if (val instanceof JsonString) {
            return ((JsonString) val).getString();
        }
    } catch (Exception ex) {
    }
    return null;
}
Also used : JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

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