Example usage for com.fasterxml.jackson.databind.node ArrayNode size

List of usage examples for com.fasterxml.jackson.databind.node ArrayNode size

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.node ArrayNode size.

Prototype

public int size() 

Source Link

Usage

From source file:easyrpc.server.serialization.jsonrpc.JSONCallee.java

@Override
public byte[] matchMethod(Object object, byte[] callInfo) {
    try {/*  w w w  .j av  a  2 s  .  com*/
        Object returnedObject = null;
        ObjectNode call = (ObjectNode) MAPPER.readTree(callInfo);

        String jsonrpc = call.get("jsonrpc").textValue();
        if (jsonrpc == null || !"2.0".equals(jsonrpc)) {
            throw new SerializationException(
                    "'jsonrpc' value must be '2.0' and actually is: '" + jsonrpc + "'");
        }

        String methodName = call.get("method").textValue();
        if (methodName == null)
            throw new SerializationException("The 'method' field must not be null: " + call.toString());

        Class iface = object.getClass();
        for (Method m : iface.getMethods()) {
            if (methodName.equals(m.getName())) {
                ArrayNode jsParams = (ArrayNode) call.get("params");
                if (jsParams == null || jsParams.size() == 0) {
                    try {
                        returnedObject = m.invoke(object);
                        //                            System.out.println("returnedObject = " + returnedObject);
                    } catch (Exception e) {
                        e.printStackTrace();
                        return returnJsonRpcError(call.get("id"), e);
                    }
                } else {
                    //                        System.out.println("methodName = " + methodName);
                    Object[] params = new Object[jsParams.size()];
                    for (int i = 0; i < params.length; i++) {
                        params[i] = MAPPER.convertValue(jsParams.get(i), m.getParameters()[i].getType());
                        //                            System.out.println("params[i] = " + params[i] + "("+ params[i].getClass().getName() +")");
                    }
                    try {
                        returnedObject = m.invoke(object, params);
                        //                            System.out.println("returnedObject = " + returnedObject);
                    } catch (Exception e) {
                        e.printStackTrace();
                        return returnJsonRpcError(call.get("id"), e);
                    }
                }
                break;
            }
        }
        ObjectNode jsret = JsonNodeFactory.instance.objectNode();
        jsret.put("jsonrpc", "2.0");
        jsret.put("id", call.get("id").toString());
        if (returnedObject != null) {
            addResult(jsret, returnedObject);
        }

        //            System.out.println("jsret.toString() = " + jsret.toString());
        return jsret.toString().getBytes();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:enmasse.queue.scheduler.Artemis.java

@Override
public Set<String> getQueueNames() {
    Set<String> queues = new LinkedHashSet<>();
    Message message = createMessage("getQueueNames");
    message.setBody(new AmqpValue("[]"));

    // TODO: Make this method less ugly
    Message response = doRequest(message);
    if (response == null) {
        log.warn("Timed out getting response from broker");
        return queues;
    }//from  w  w  w  .  j a  va  2 s  .  c om
    AmqpValue value = (AmqpValue) response.getBody();
    try {
        ArrayNode root = (ArrayNode) mapper.readTree((String) value.getValue());
        ArrayNode elements = (ArrayNode) root.get(0);
        for (int i = 0; i < elements.size(); i++) {
            String queueName = elements.get(i).asText();
            if (!queueName.equals(replyTo)) {
                queues.add(queueName);
            }
        }
    } catch (IOException e) {
        log.error("Error decoding queue names", e);
    }
    return queues;
}

From source file:org.activiti.editor.language.json.converter.SequenceFlowJsonConverter.java

private String lookForSourceRef(String flowId, JsonNode childShapesNode) {
    String sourceRef = null;// ww  w . j  a v a2 s  .  c  o  m

    if (childShapesNode != null) {

        for (JsonNode childNode : childShapesNode) {
            ArrayNode outgoingNode = (ArrayNode) childNode.get("outgoing");
            if (outgoingNode != null && outgoingNode.size() > 0) {
                for (JsonNode outgoingChildNode : outgoingNode) {
                    JsonNode resourceNode = outgoingChildNode.get(EDITOR_SHAPE_ID);
                    if (resourceNode != null && flowId.equals(resourceNode.asText())) {
                        sourceRef = BpmnJsonConverterUtil.getElementId(childNode);
                        break;
                    }
                }

                if (sourceRef != null) {
                    break;
                }
            }
            sourceRef = lookForSourceRef(flowId, childNode.get(EDITOR_CHILD_SHAPES));

            if (sourceRef != null) {
                break;
            }
        }
    }
    return sourceRef;
}

From source file:com.redhat.lightblue.eval.UnsetExpressionEvaluator.java

@Override
public boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath) {
    boolean ret = false;
    MutablePath p = new MutablePath();
    for (AbsPath x : fields) {
        Path fld = new Path(contextPath, x.field);
        LOGGER.debug("Removing {}", fld);
        if (doc.modify(fld, null, false) != null) {
            ret = true;/*w  w  w. ja va2s. c o  m*/
            if (x.absArrayField != null) {
                // This is an array
                p.set(x.absArrayField);
                p.rewriteIndexes(fld);
                ArrayNode node = (ArrayNode) doc.get(p);
                p.setLast(p.getLast() + "#");
                doc.modify(p, factory.numberNode(node.size()), false);
            }
        }
    }
    return ret;
}

From source file:de.jlo.talendcomp.json.JsonComparator.java

/**
 * Collects the differences between the both arrays 
 * @param array1//from w  ww. ja  v  a 2  s .c o  m
 * @param array2
 * @return an array which contains the difference between both arrays
 */
public ArrayNode difference(ArrayNode array1, ArrayNode array2) {
    ArrayNode result = objectMapper.createArrayNode();
    for (int i1 = 0, n1 = array1.size(); i1 < n1; i1++) {
        JsonNode node1 = array1.get(i1);
        boolean found = false;
        for (int i2 = 0, n2 = array2.size(); i2 < n2; i2++) {
            JsonNode node2 = array2.get(i2);
            if (node1.equals(node2)) {
                found = true;
                break;
            }
        }
        if (found == false) {
            result.add(node1);
        }
    }
    // exchange the arrays
    ArrayNode x = array1;
    array1 = array2;
    array2 = x;
    for (int i1 = 0, n1 = array1.size(); i1 < n1; i1++) {
        JsonNode node1 = array1.get(i1);
        boolean found = false;
        for (int i2 = 0, n2 = array2.size(); i2 < n2; i2++) {
            JsonNode node2 = array2.get(i2);
            if (node1.equals(node2)) {
                found = true;
                break;
            }
        }
        if (found == false) {
            result.add(node1);
        }
    }
    return result;
}

From source file:org.apache.solr.kelvin.testcases.SimpleCondition.java

public void configure(JsonNode condition) throws Exception {
    parseLen(condition);//from   w  w w.j  ava  2  s.  co  m
    parseReverseConditions(condition);
    List<String> values = new LinkedList<String>();
    mandatory(condition, "field");
    ArrayList<String> realFiels = new ArrayList<String>();
    ArrayNode fieldsArray = ConfigurableLoader.assureArray(condition.get("field"));
    for (int i = 0; i < fieldsArray.size(); i++) {
        if (fieldsArray.get(i).asText().contains("+")) {
            //legacy
            for (String f : fieldsArray.get(i).asText().split("[+]"))
                realFiels.add(f);
        } else {
            realFiels.add(fieldsArray.get(i).asText());
        }
    }
    if (condition.has("args"))
        readStringArrayOpt(condition.get("args"), values); //legacy
    if (condition.has("values"))
        readStringArrayOpt(condition.get("values"), values);
    if (values.size() == 0)
        throw new Exception("missing condition values");
    init(this.length, realFiels, values);
}

From source file:org.springframework.data.rest.webmvc.json.patch.JsonPatchPatchConverter.java

/**
 * Constructs a {@link Patch} object given a JsonNode.
 * //from w  w w.  ja  va2  s . c  o  m
 * @param jsonNode a JsonNode containing the JSON Patch
 * @return a {@link Patch}
 */
public Patch convert(JsonNode jsonNode) {
    if (!(jsonNode instanceof ArrayNode)) {
        throw new IllegalArgumentException("JsonNode must be an instance of ArrayNode");
    }

    ArrayNode opNodes = (ArrayNode) jsonNode;
    List<PatchOperation> ops = new ArrayList<PatchOperation>(opNodes.size());
    for (Iterator<JsonNode> elements = opNodes.elements(); elements.hasNext();) {
        JsonNode opNode = elements.next();

        String opType = opNode.get("op").textValue();
        String path = opNode.get("path").textValue();

        JsonNode valueNode = opNode.get("value");
        Object value = valueFromJsonNode(path, valueNode);
        String from = opNode.has("from") ? opNode.get("from").textValue() : null;

        if (opType.equals("test")) {
            ops.add(new TestOperation(path, value));
        } else if (opType.equals("replace")) {
            ops.add(new ReplaceOperation(path, value));
        } else if (opType.equals("remove")) {
            ops.add(new RemoveOperation(path));
        } else if (opType.equals("add")) {
            ops.add(new AddOperation(path, value));
        } else if (opType.equals("copy")) {
            ops.add(new CopyOperation(path, from));
        } else if (opType.equals("move")) {
            ops.add(new MoveOperation(path, from));
        } else {
            throw new PatchException("Unrecognized operation type: " + opType);
        }
    }

    return new Patch(ops);
}

From source file:com.redhat.lightblue.eval.ArrayQueryProjectorTest.java

@Test
public void array_query_projection_only_one_match_others_should_be_excluded() throws Exception {
    Projection p = EvalTestContext.projectionFromJson(
            "{'field':'field12.nf1.nnf1.*.nnnf1.arr','match':{'field':'id','op':'=','rvalue':1}}");
    Projector projector = Projector.getInstance(p, md);
    JsonDoc pdoc = projector.project(jsonDoc, JSON_NODE_FACTORY);

    System.out.println("doc:" + pdoc);
    ArrayNode node = (ArrayNode) pdoc.get(new Path("field12.nf1.nnf1.0.nnnf1.arr"));
    Assert.assertEquals(1, node.size());
    node = (ArrayNode) pdoc.get(new Path("field12.nf1.nnf1"));
    Assert.assertEquals(1, node.size());
}

From source file:com.turn.shapeshifter.NamedSchemaParser.java

/**
 * Parses a repeated field.//from  w w w . j a  v a  2s. com
 *
 * @param registry a registry of schemas, used for parsing enclosed objects
 * @param builder the builder in which the parsed field should be set
 * @param fieldName the JSON name of the field
 * @param field the descriptor of the repeated field being parsed
 * @param valueNode the JSON node being parsed
 * @throws ParsingException
 */
private void parseRepeatedField(ReadableSchemaRegistry registry, Message.Builder builder, String fieldName,
        FieldDescriptor field, JsonNode valueNode) throws ParsingException {
    if (!valueNode.isArray()) {
        throw new IllegalArgumentException(
                "Field '" + fieldName + "' is expected to be an array, but was " + valueNode.asToken());
    }
    ArrayNode array = (ArrayNode) valueNode;
    if (array.size() != 0) {
        for (JsonNode item : array) {
            Object value = parseValue(item, field, registry);
            if (value != null) {
                builder.addRepeatedField(field, value);
            }
        }
    }
}

From source file:org.springframework.social.vkontakte.api.impl.AbstractVKontakteOperations.java

protected <T> VKArray<T> deserializeArray(VKGenericResponse response, Class<T> itemClass) {
    checkForError(response);//  w ww .  j a  v a2 s.c o  m

    Assert.isTrue(response.getResponse().isArray());
    ArrayNode items = (ArrayNode) response.getResponse();
    int count = items.get(0).asInt();
    List<T> elements = new ArrayList<T>();
    for (int i = 1; i < items.size(); i++) {
        elements.add(objectMapper.convertValue(items.get(i), itemClass));
    }

    return new VKArray<T>(count, elements);
}