Search in sources :

Example 6 with JsonValue

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

the class JsonReader method createProperty.

protected void createProperty(String name, Object value, ContentCreator contentCreator) throws RepositoryException {
    // assume simple value
    if (value instanceof JsonArray) {
        // multivalue
        final JsonArray array = (JsonArray) value;
        if (array.size() > 0) {
            final String[] values = new String[array.size()];
            for (int i = 0; i < values.length; i++) {
                values[i] = unbox(array.get(i)).toString();
            }
            final int propertyType = getType(name, unbox(array.get(0)));
            contentCreator.createProperty(getName(name), propertyType, values);
        } else {
            contentCreator.createProperty(getName(name), PropertyType.STRING, new String[0]);
        }
    } else if (value instanceof JsonValue) {
        // single value
        value = unbox(value);
        final int propertyType = getType(name, value);
        contentCreator.createProperty(getName(name), propertyType, value.toString());
    }
}
Also used : JsonArray(javax.json.JsonArray) JsonValue(javax.json.JsonValue) JsonString(javax.json.JsonString)

Example 7 with JsonValue

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

the class JsonResponseTest method testSetError.

@SuppressWarnings({ "ThrowableInstanceNeverThrown" })
public void testSetError() throws IOException {
    String errMsg = "Dummy error";
    res.setError(new Error(errMsg));
    MockSlingHttpServletResponse resp = new MockSlingHttpServletResponse();
    res.send(resp, true);
    JsonObject json = res.getJson();
    JsonValue error = assertProperty(json, "error");
    assertProperty((JsonObject) error, "class", Error.class.getName());
    assertProperty((JsonObject) error, "message", errMsg);
}
Also used : JsonValue(javax.json.JsonValue) MockSlingHttpServletResponse(org.apache.sling.commons.testing.sling.MockSlingHttpServletResponse) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString)

Example 8 with JsonValue

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

the class SimpleDistributionQueueProvider method enableQueueProcessing.

public void enableQueueProcessing(@Nonnull DistributionQueueProcessor queueProcessor, String... queueNames) {
    if (checkpoint) {
        // recover from checkpoints
        log.debug("recovering from checkpoints if needed");
        for (final String queueName : queueNames) {
            log.debug("recovering for queue {}", queueName);
            DistributionQueue queue = getQueue(queueName);
            FilenameFilter filenameFilter = new FilenameFilter() {

                @Override
                public boolean accept(File file, String name) {
                    return name.equals(queueName + "-checkpoint");
                }
            };
            for (File qf : checkpointDirectory.listFiles(filenameFilter)) {
                log.info("recovering from checkpoint {}", qf);
                try {
                    LineIterator lineIterator = IOUtils.lineIterator(new FileReader(qf));
                    while (lineIterator.hasNext()) {
                        String s = lineIterator.nextLine();
                        String[] split = s.split(" ");
                        String id = split[0];
                        String infoString = split[1];
                        Map<String, Object> info = new HashMap<String, Object>();
                        JsonReader reader = Json.createReader(new StringReader(infoString));
                        JsonObject jsonObject = reader.readObject();
                        for (Map.Entry<String, JsonValue> entry : jsonObject.entrySet()) {
                            if (entry.getValue().getValueType().equals(JsonValue.ValueType.ARRAY)) {
                                JsonArray value = jsonObject.getJsonArray(entry.getKey());
                                String[] a = new String[value.size()];
                                for (int i = 0; i < a.length; i++) {
                                    a[i] = value.getString(i);
                                }
                                info.put(entry.getKey(), a);
                            } else if (JsonValue.NULL.equals(entry.getValue())) {
                                info.put(entry.getKey(), null);
                            } else {
                                info.put(entry.getKey(), ((JsonString) entry.getValue()).getString());
                            }
                        }
                        queue.add(new DistributionQueueItem(id, info));
                    }
                    log.info("recovered {} items from queue {}", queue.getStatus().getItemsCount(), queueName);
                } catch (FileNotFoundException e) {
                    log.warn("could not read checkpoint file {}", qf.getAbsolutePath());
                } catch (JsonException e) {
                    log.warn("could not parse info from checkpoint file {}", qf.getAbsolutePath());
                }
            }
        }
        // enable checkpointing
        for (String queueName : queueNames) {
            ScheduleOptions options = scheduler.NOW(-1, 15).canRunConcurrently(false).name(getJobName(queueName + "-checkpoint"));
            scheduler.schedule(new SimpleDistributionQueueCheckpoint(getQueue(queueName), checkpointDirectory), options);
        }
    }
    // enable processing
    for (String queueName : queueNames) {
        ScheduleOptions options = scheduler.NOW(-1, 1).canRunConcurrently(false).name(getJobName(queueName));
        scheduler.schedule(new SimpleDistributionQueueProcessor(getQueue(queueName), queueProcessor), options);
    }
}
Also used : JsonException(javax.json.JsonException) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FileNotFoundException(java.io.FileNotFoundException) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) LineIterator(org.apache.commons.io.LineIterator) FilenameFilter(java.io.FilenameFilter) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) FileReader(java.io.FileReader) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) JsonValue(javax.json.JsonValue) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) JsonArray(javax.json.JsonArray) ScheduleOptions(org.apache.sling.commons.scheduler.ScheduleOptions) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 9 with JsonValue

use of javax.json.JsonValue in project torodb by torodb.

the class TableRefConverter method fromJsonArray.

public static TableRef fromJsonArray(TableRefFactory tableRefFactory, JsonArray tableRefJsonArray) {
    TableRef tableRef = tableRefFactory.createRoot();
    for (JsonValue tableRefNameValue : tableRefJsonArray) {
        String tableRefName = ((JsonString) tableRefNameValue).getString();
        tableRef = createChild(tableRefFactory, tableRef, tableRefName);
    }
    return tableRef;
}
Also used : JsonValue(javax.json.JsonValue) JsonString(javax.json.JsonString) JsonString(javax.json.JsonString) TableRef(com.torodb.core.TableRef)

Example 10 with JsonValue

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

the class JsonRenderer method skipChildObject.

/** Decide whether o must be skipped and added to a, when rendering a JSONObject */
private boolean skipChildObject(JsonArrayBuilder a, Options opt, String key, Object value) {
    if (opt.arraysForChildren && (value instanceof JsonObject)) {
        JsonObjectBuilder builder = Json.createObjectBuilder();
        builder.add(opt.childNameKey, key);
        for (Map.Entry<String, JsonValue> entry : ((JsonObject) value).entrySet()) {
            builder.add(entry.getKey(), entry.getValue());
        }
        a.add(builder);
        return true;
    }
    return false;
}
Also used : JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonObjectBuilder(javax.json.JsonObjectBuilder) Map(java.util.Map)

Aggregations

JsonValue (javax.json.JsonValue)14 JsonString (javax.json.JsonString)9 JsonObject (javax.json.JsonObject)8 JsonArray (javax.json.JsonArray)7 Map (java.util.Map)5 HashMap (java.util.HashMap)3 JsonException (javax.json.JsonException)3 JsonObjectBuilder (javax.json.JsonObjectBuilder)3 JsonReader (javax.json.JsonReader)3 KvValue (com.torodb.kvdocument.values.KvValue)2 ListKvArray (com.torodb.kvdocument.values.heap.ListKvArray)2 File (java.io.File)2 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 ArrayConverter (com.torodb.backend.converters.array.ArrayConverter)1 TableRef (com.torodb.core.TableRef)1 ClassicCounter (edu.stanford.nlp.stats.ClassicCounter)1 Counter (edu.stanford.nlp.stats.Counter)1 TwoDimensionalCounter (edu.stanford.nlp.stats.TwoDimensionalCounter)1