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());
}
}
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);
}
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);
}
}
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;
}
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;
}
Aggregations