Search in sources :

Example 36 with ArrayNode

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);
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) ObjectNode(org.codehaus.jackson.node.ObjectNode) Message(org.apache.helix.model.Message) ArrayNode(org.codehaus.jackson.node.ArrayNode) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 37 with ArrayNode

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);
    }
}
Also used : ValueSerializationException(org.qi4j.api.value.ValueSerializationException) JsonNode(org.codehaus.jackson.JsonNode) ArrayNode(org.codehaus.jackson.node.ArrayNode)

Example 38 with ArrayNode

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);
        }
    }
}
Also used : ValueSerializationException(org.qi4j.api.value.ValueSerializationException) JsonNode(org.codehaus.jackson.JsonNode) ArrayNode(org.codehaus.jackson.node.ArrayNode)

Example 39 with ArrayNode

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;
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) ArrayNode(org.codehaus.jackson.node.ArrayNode)

Example 40 with ArrayNode

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;
}
Also used : ObjectNode(org.codehaus.jackson.node.ObjectNode) ArrayNode(org.codehaus.jackson.node.ArrayNode) Span(org.apache.stanbol.enhancer.nlp.model.Span)

Aggregations

ArrayNode (org.codehaus.jackson.node.ArrayNode)44 ObjectNode (org.codehaus.jackson.node.ObjectNode)29 JsonNode (org.codehaus.jackson.JsonNode)17 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)11 GET (javax.ws.rs.GET)10 Test (org.junit.Test)10 Path (javax.ws.rs.Path)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Date (java.util.Date)4 List (java.util.List)4 HelixDataAccessor (org.apache.helix.HelixDataAccessor)4 Produces (javax.ws.rs.Produces)3 RegressionTestHelper (org.openmrs.module.htmlformentry.RegressionTestHelper)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 LinkedHashMap (java.util.LinkedHashMap)2