Search in sources :

Example 46 with JsonString

use of javax.json.JsonString in project sling by apache.

the class OverrideStringParser method convertJsonValue.

/**
     * Convert single JSON-conformant value object
     * @param jsonValue JSON value
     * @return Object
     * @throws JSONException If JSON-parsing of value failed
     */
private static Object convertJsonValue(String jsonValue) {
    String jsonString = "{\"value\":" + jsonValue + "}";
    JsonObject json = toJson(jsonString);
    return convertJsonValue(json.get("value"));
}
Also used : JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString)

Example 47 with JsonString

use of javax.json.JsonString in project sling by apache.

the class JsonRenderer method valueToString.

/**
     * Make a JSON text of an Object value. 
     * <p>
     * Warning: This method assumes that the data structure is acyclical.
     * @param value The value to be serialized.
     * @return a printable, displayable, transmittable
     *  representation of the object, beginning
     *  with <code>{</code>&nbsp;<small>(left brace)</small> and ending
     *  with <code>}</code>&nbsp;<small>(right brace)</small>.
     * @throws JSONException If the value is or contains an invalid number.
     */
public String valueToString(Object value) {
    // TODO call the other valueToString instead
    if (value == null || value.equals(null)) {
        return "null";
    }
    if (value instanceof JsonString) {
        quote(((JsonString) value).getString());
    }
    if (value instanceof Number) {
        return numberToString((Number) value);
    }
    if (value instanceof Boolean) {
        return value.toString();
    }
    if (value instanceof JsonObject || value instanceof JsonArray) {
        StringWriter writer = new StringWriter();
        Json.createGenerator(writer).write((JsonValue) value).close();
        return writer.toString();
    }
    return quote(value.toString());
}
Also used : JsonArray(javax.json.JsonArray) StringWriter(java.io.StringWriter) JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString)

Example 48 with JsonString

use of javax.json.JsonString in project nifi by apache.

the class TestSiteToSiteStatusReportingTask method testComponentNameFilter_nested.

@Test
public void testComponentNameFilter_nested() throws IOException, InitializationException {
    final ProcessGroupStatus pgStatus = generateProcessGroupStatus("root", "Awesome", 2, 0);
    final Map<PropertyDescriptor, String> properties = new HashMap<>();
    properties.put(SiteToSiteStatusReportingTask.BATCH_SIZE, "4");
    properties.put(SiteToSiteStatusReportingTask.COMPONENT_NAME_FILTER_REGEX, "Awesome.*processor.*");
    properties.put(SiteToSiteStatusReportingTask.COMPONENT_TYPE_FILTER_REGEX, ".*");
    MockSiteToSiteStatusReportingTask task = initTask(properties, pgStatus);
    task.onTrigger(context);
    // 3 + (3 * 3) + (3 * 3 * 3) = 39, or 10 batches of 4
    assertEquals(10, task.dataSent.size());
    final String msg = new String(task.dataSent.get(0), StandardCharsets.UTF_8);
    JsonReader jsonReader = Json.createReader(new ByteArrayInputStream(msg.getBytes()));
    JsonString componentId = jsonReader.readArray().getJsonObject(0).getJsonString("componentId");
    assertEquals("root.1.1.processor.1", componentId.getString());
}
Also used : ProcessGroupStatus(org.apache.nifi.controller.status.ProcessGroupStatus) RemoteProcessGroupStatus(org.apache.nifi.controller.status.RemoteProcessGroupStatus) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) JsonReader(javax.json.JsonReader) JsonString(javax.json.JsonString) JsonString(javax.json.JsonString) Test(org.junit.Test)

Example 49 with JsonString

use of javax.json.JsonString in project nifi by apache.

the class TestSiteToSiteStatusReportingTask method testComponentTypeFilter.

@Test
public void testComponentTypeFilter() throws IOException, InitializationException {
    final ProcessGroupStatus pgStatus = generateProcessGroupStatus("root", "Awesome", 1, 0);
    final Map<PropertyDescriptor, String> properties = new HashMap<>();
    properties.put(SiteToSiteStatusReportingTask.BATCH_SIZE, "4");
    properties.put(SiteToSiteStatusReportingTask.COMPONENT_NAME_FILTER_REGEX, "Awesome.*");
    properties.put(SiteToSiteStatusReportingTask.COMPONENT_TYPE_FILTER_REGEX, "(ProcessGroup|RootProcessGroup)");
    MockSiteToSiteStatusReportingTask task = initTask(properties, pgStatus);
    task.onTrigger(context);
    // Only root pg and 3 child pgs
    assertEquals(1, task.dataSent.size());
    final String msg = new String(task.dataSent.get(0), StandardCharsets.UTF_8);
    JsonReader jsonReader = Json.createReader(new ByteArrayInputStream(msg.getBytes()));
    JsonString componentId = jsonReader.readArray().getJsonObject(0).getJsonString("componentId");
    assertEquals(pgStatus.getId(), componentId.getString());
}
Also used : ProcessGroupStatus(org.apache.nifi.controller.status.ProcessGroupStatus) RemoteProcessGroupStatus(org.apache.nifi.controller.status.RemoteProcessGroupStatus) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) JsonReader(javax.json.JsonReader) JsonString(javax.json.JsonString) JsonString(javax.json.JsonString) Test(org.junit.Test)

Example 50 with JsonString

use of javax.json.JsonString in project nifi by apache.

the class TestSiteToSiteStatusReportingTask method testComponentNameFilter.

@Test
public void testComponentNameFilter() throws IOException, InitializationException {
    final ProcessGroupStatus pgStatus = generateProcessGroupStatus("root", "Awesome", 1, 0);
    final Map<PropertyDescriptor, String> properties = new HashMap<>();
    properties.put(SiteToSiteStatusReportingTask.BATCH_SIZE, "4");
    properties.put(SiteToSiteStatusReportingTask.COMPONENT_NAME_FILTER_REGEX, "Awesome.*processor.*");
    properties.put(SiteToSiteStatusReportingTask.COMPONENT_TYPE_FILTER_REGEX, ".*");
    MockSiteToSiteStatusReportingTask task = initTask(properties, pgStatus);
    task.onTrigger(context);
    // 3 processors for each of 4 groups
    assertEquals(3, task.dataSent.size());
    final String msg = new String(task.dataSent.get(0), StandardCharsets.UTF_8);
    JsonReader jsonReader = Json.createReader(new ByteArrayInputStream(msg.getBytes()));
    JsonString componentId = jsonReader.readArray().getJsonObject(0).getJsonString("componentId");
    assertEquals("root.1.processor.1", componentId.getString());
}
Also used : ProcessGroupStatus(org.apache.nifi.controller.status.ProcessGroupStatus) RemoteProcessGroupStatus(org.apache.nifi.controller.status.RemoteProcessGroupStatus) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) JsonReader(javax.json.JsonReader) JsonString(javax.json.JsonString) JsonString(javax.json.JsonString) Test(org.junit.Test)

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