use of org.codehaus.jackson.node.ArrayNode in project helix by apache.
the class InstanceAccessor method getMessagesOnInstance.
@GET
@Path("{instanceName}/messages")
public Response getMessagesOnInstance(@PathParam("clusterId") String clusterId, @PathParam("instanceName") String instanceName) throws IOException {
HelixDataAccessor accessor = getDataAccssor(clusterId);
ObjectNode root = JsonNodeFactory.instance.objectNode();
root.put(Properties.id.name(), instanceName);
ArrayNode newMessages = root.putArray(InstanceProperties.new_messages.name());
ArrayNode readMessages = root.putArray(InstanceProperties.read_messages.name());
List<String> messages = accessor.getChildNames(accessor.keyBuilder().messages(instanceName));
if (messages == null || messages.size() == 0) {
return notFound();
}
for (String messageName : messages) {
Message message = accessor.getProperty(accessor.keyBuilder().message(instanceName, messageName));
if (message.getMsgState() == Message.MessageState.NEW) {
newMessages.add(messageName);
}
if (message.getMsgState() == Message.MessageState.READ) {
readMessages.add(messageName);
}
}
root.put(InstanceProperties.total_message_count.name(), newMessages.size() + readMessages.size());
root.put(InstanceProperties.read_message_count.name(), readMessages.size());
return JSONRepresentation(root);
}
use of org.codehaus.jackson.node.ArrayNode in project qi4j-sdk by Qi4j.
the class JacksonValueDeserializer method putArrayNodeInCollection.
@Override
protected <T> void putArrayNodeInCollection(JsonNode inputNode, Function<JsonNode, T> deserializer, Collection<T> collection) throws Exception {
if (isNullOrMissing(inputNode)) {
return;
}
if (!inputNode.isArray()) {
throw new ValueSerializationException("Expected an array but got " + inputNode);
}
ArrayNode array = (ArrayNode) inputNode;
for (JsonNode item : array) {
T value = deserializer.map(item);
collection.add(value);
}
}
use of org.codehaus.jackson.node.ArrayNode in project qi4j-sdk by Qi4j.
the class JacksonValueDeserializer method putArrayNodeInMap.
@Override
protected <K, V> void putArrayNodeInMap(JsonNode inputNode, Function<JsonNode, K> keyDeserializer, Function<JsonNode, V> valueDeserializer, Map<K, V> map) throws Exception {
if (isNullOrMissing(inputNode)) {
return;
}
if (!inputNode.isArray()) {
throw new ValueSerializationException("Expected an array but got " + inputNode);
}
ArrayNode array = (ArrayNode) inputNode;
for (JsonNode item : array) {
if (!item.isObject()) {
throw new ValueSerializationException("Expected an object but got " + inputNode);
}
JsonNode keyNode = item.get("key");
JsonNode valueNode = item.get("value");
K key = keyDeserializer.map(keyNode);
V value = valueDeserializer.map(valueNode);
if (key != null) {
map.put(key, value);
}
}
}
use of org.codehaus.jackson.node.ArrayNode in project stanbol by apache.
the class JsonUtils method parseEnum.
/**
* @param jValue
* @param categories
*/
public static <T extends Enum<T>> EnumSet<T> parseEnum(ObjectNode jValue, String key, Class<T> type) {
final EnumSet<T> categories = EnumSet.noneOf(type);
JsonNode node = jValue.path(key);
if (node.isMissingNode()) {
// no values nothing to do
return categories;
}
if (node.isArray()) {
ArrayNode jLcs = (ArrayNode) node;
for (int i = 0; i < jLcs.size(); i++) {
JsonNode jLc = jLcs.get(i);
if (jLc.isTextual()) {
try {
categories.add(Enum.valueOf(type, jLc.getTextValue()));
} catch (IllegalArgumentException e) {
log.warn("unknown " + type.getSimpleName() + " '" + jLc + "'", e);
}
} else if (jLc.isInt()) {
categories.add(type.getEnumConstants()[jLc.asInt()]);
} else {
log.warn("unknow value in '{}' Array at index [{}]: {}", new Object[] { key, i, jLc });
}
}
} else if (node.isTextual()) {
try {
categories.add(Enum.valueOf(type, node.getTextValue()));
} catch (IllegalArgumentException e) {
log.warn("unknown " + type.getSimpleName() + " '" + node + "'", e);
}
} else if (node.isInt()) {
categories.add(type.getEnumConstants()[node.asInt()]);
} else {
log.warn("unknow value for key '{}': {}", key, node);
}
return categories;
}
use of org.codehaus.jackson.node.ArrayNode in project stanbol by apache.
the class CorefFeatureSupport method serialize.
@Override
public ObjectNode serialize(ObjectMapper mapper, CorefFeature coref) {
ObjectNode jCoref = mapper.createObjectNode();
jCoref.put(IS_REPRESENTATIVE_TAG, coref.isRepresentative());
Set<Span> mentions = coref.getMentions();
ArrayNode jMentions = mapper.createArrayNode();
for (Span mention : mentions) {
ObjectNode jMention = mapper.createObjectNode();
jMention.put(MENTION_TYPE_TAG, mention.getType().toString());
jMention.put(MENTION_START_TAG, mention.getStart());
jMention.put(MENTION_END_TAG, mention.getEnd());
jMentions.add(jMention);
}
jCoref.put(MENTIONS_TAG, jMentions);
return jCoref;
}
Aggregations