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:org.teavm.flavour.json.test.TeaVMJSONRunner.java

public static final JsonNode convert(JsonNodeFactory nf, Node node) {
    if (node.isNull()) {
        return nf.nullNode();
    } else if (node.isBoolean()) {
        BooleanNode booleanNode = (BooleanNode) node;
        return nf.booleanNode(booleanNode.getValue());
    } else if (node.isNumber()) {
        NumberNode numberNode = (NumberNode) node;
        if (numberNode.isInt()) {
            return nf.numberNode(numberNode.getIntValue());
        } else {// w  ww  . j ava  2 s .  co m
            return nf.numberNode(numberNode.getValue());
        }
    } else if (node.isString()) {
        StringNode stringNode = (StringNode) node;
        return nf.textNode(stringNode.getValue());
    } else if (node.isArray()) {
        ArrayNode result = nf.arrayNode();
        org.teavm.flavour.json.tree.ArrayNode source = (org.teavm.flavour.json.tree.ArrayNode) node;
        for (int i = 0; i < source.size(); ++i) {
            result.add(convert(nf, source.get(i)));
        }
        return result;
    } else if (node.isObject()) {
        com.fasterxml.jackson.databind.node.ObjectNode result = nf.objectNode();
        ObjectNode objectNode = (ObjectNode) node;
        for (String key : objectNode.allKeys()) {
            result.replace(key, convert(nf, objectNode.get(key)));
        }
        return result;
    } else {
        throw new IllegalArgumentException("Can't convert this JSON node");
    }
}

From source file:com.pros.jsontransform.sort.ArraySortAbstract.java

static void doSort(final ArrayNode arrayNode, final JsonNode sortNode, final ObjectTransformer transformer) {
    // move array nodes to sorted array
    int size = arrayNode.size();
    ArrayList<JsonNode> sortedArray = new ArrayList<JsonNode>(arrayNode.size());
    for (int i = 0; i < size; i++) {
        sortedArray.add(arrayNode.remove(0));
    }/*from w w  w  .  j  av  a 2s . c  o  m*/

    // sort array
    sortedArray.sort(new NodeComparator(sortNode, transformer));

    // move nodes back to targetArray
    for (int i = 0; i < sortedArray.size(); i++) {
        arrayNode.add(sortedArray.get(i));
    }
}

From source file:org.gitana.platform.client.util.DriverUtil.java

public static List<String> toStringList(Response response) {
    List<String> list = new ArrayList<String>();

    ObjectNode objectNode = response.getObjectNode();
    ArrayNode rows = (ArrayNode) objectNode.get("rows");
    for (int i = 0; i < rows.size(); i++) {
        list.add(rows.get(i).textValue());
    }//from   w w w.ja  v  a2  s . c  o m

    return list;
}

From source file:org.gitana.platform.client.util.DriverUtil.java

public static ACL toACL(Response response) {
    ACL acl = new ACL();

    ObjectNode objectNode = response.getObjectNode();
    ArrayNode rows = (ArrayNode) objectNode.get("rows");
    for (int x = 0; x < rows.size(); x++) {
        ObjectNode binding = (ObjectNode) rows.get(x);

        String principalId = binding.get(DomainPrincipal.FIELD_ID).textValue();
        String principalType = binding.get(DomainPrincipal.FIELD_TYPE).textValue();

        List<String> roles = new ArrayList<String>();
        ArrayNode array = (ArrayNode) binding.get("authorities");
        for (int i = 0; i < array.size(); i++) {
            roles.add(array.get(i).textValue());
        }// w w  w. j  a  v a  2 s  .  c o m

        ACLEntry entry = new ACLEntry();
        entry.setPrincipal(principalId);
        entry.setAuthorities(roles);

        acl.add(principalId, entry);
    }

    return acl;
}

From source file:com.github.fge.jsonpatch.diff.JsonDiff.java

private static void generateArrayDiffs(final DiffProcessor processor, final JsonPointer pointer,
        final ArrayNode source, final ArrayNode target) {
    final int firstSize = source.size();
    final int secondSize = target.size();
    final int size = Math.min(firstSize, secondSize);

    /*/*from   w  ww  . jav  a  2 s.c o  m*/
     * Source array is larger; in this case, elements are removed from the
     * target; the index of removal is always the original arrays's length.
     */
    for (int index = size; index < firstSize; index++)
        processor.valueRemoved(pointer.append(size), source.get(index));

    for (int index = 0; index < size; index++)
        generateDiffs(processor, pointer.append(index), source.get(index), target.get(index));

    // Deal with the destination array being larger...
    for (int index = size; index < secondSize; index++)
        processor.valueAdded(pointer.append("-"), target.get(index));
}

From source file:io.wcm.caravan.pipeline.extensions.hal.filter.HalResourceFilters.java

/**
 * @param jsonPath JSON path to check//from  www.j a  v a 2s . com
 * @return True if model has field(s) for given JSON path and all values are not null
 */
public static HalResourcePredicate hasPathNonNull(String jsonPath) {
    JsonPath compiledPath = JsonPath.compile(jsonPath);
    return new HalResourcePredicate() {

        @Override
        public String getId() {
            return "HAS(" + jsonPath + ")";
        }

        @Override
        public boolean apply(HalPath halPath, HalResource hal) {
            ObjectNode json = hal.getModel();
            ArrayNode matches = compiledPath.read(json, JSON_PATH_CONF);
            // check for empty result list
            if (matches == null || matches.size() == 0) {
                return false;
            }
            // check for null value
            for (JsonNode match : matches) {
                if (match == null || match.isNull()) {
                    return false;
                }
            }
            return true;
        }

    };
}

From source file:com.mirth.connect.client.ui.components.rsta.RSTAPreferences.java

static RSTAPreferences fromJSON(String keyStrokesJSON, String findReplaceJSON, String toggleOptionsJSON,
        String autoCompleteJSON) {
    ObjectMapper mapper = new ObjectMapper();

    Map<String, KeyStroke> keyStrokeMap = new HashMap<String, KeyStroke>();
    if (StringUtils.isNotBlank(keyStrokesJSON)) {
        try {/* w w  w  .  j  a  va 2  s  .c  o  m*/
            ObjectNode keyStrokesNode = (ObjectNode) mapper.readTree(keyStrokesJSON);

            for (Iterator<Entry<String, JsonNode>> it = keyStrokesNode.fields(); it.hasNext();) {
                Entry<String, JsonNode> entry = it.next();
                KeyStroke keyStroke = null;

                if (!entry.getValue().isNull()) {
                    ArrayNode arrayNode = (ArrayNode) entry.getValue();
                    if (arrayNode.size() > 1) {
                        keyStroke = KeyStroke.getKeyStroke(arrayNode.get(0).asInt(), arrayNode.get(1).asInt());
                    }
                }

                keyStrokeMap.put(entry.getKey(), keyStroke);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    FindReplaceProperties findReplaceProperties = FindReplaceProperties.fromJSON(findReplaceJSON);

    Map<String, Boolean> toggleOptions = new HashMap<String, Boolean>();
    if (StringUtils.isNotBlank(toggleOptionsJSON)) {
        try {
            ObjectNode toggleOptionsNode = (ObjectNode) mapper.readTree(toggleOptionsJSON);

            for (Iterator<Entry<String, JsonNode>> it = toggleOptionsNode.fields(); it.hasNext();) {
                Entry<String, JsonNode> entry = it.next();

                if (!entry.getValue().isNull()) {
                    toggleOptions.put(entry.getKey(), entry.getValue().asBoolean());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    AutoCompleteProperties autoCompleteProperties = AutoCompleteProperties.fromJSON(autoCompleteJSON);

    return new RSTAPreferences(keyStrokeMap, findReplaceProperties, toggleOptions, autoCompleteProperties);
}

From source file:ru.histone.deparser.Deparser.java

protected static ArrayNode removeHistoneAstSignature(ArrayNode ast) {
    if (ast.size() == 2 && ast.get(0).isArray() && ast.get(1).isArray()
            && "HISTONE".equals(ast.get(0).get(0).asText())) {
        return (ArrayNode) ast.get(1);
    } else {//from   ww w.ja v a 2 s .c om
        return ast;
    }
}

From source file:com.redhat.lightblue.util.JsonUtils.java

private static Object fromJson(ArrayNode json) {
    ArrayList ret = new ArrayList(json.size());
    for (Iterator<JsonNode> itr = json.elements(); itr.hasNext();) {
        ret.add(fromJson(itr.next()));/*from   w  ww .  ja  v a  2  s  . co  m*/
    }
    return ret;
}

From source file:com.pros.jsontransform.plugin.sort.ArraySortByAvgAge.java

public static void sort(final ArrayNode arrayNode, final JsonNode sortNode, final ObjectTransformer transformer)
        throws ObjectTransformerException {
    // target array does not contain age information so
    // we need to use the source array from transformer context to perform the sort
    ArrayNode sourceArray = (ArrayNode) transformer.getSourceNode();

    // move source and target array nodes to sorting array
    int size = arrayNode.size();
    ArrayList<JsonNode> sortingArray = new ArrayList<JsonNode>(arrayNode.size());
    for (int i = 0; i < size; i++) {
        ObjectNode sortingElement = transformer.mapper.createObjectNode();
        sortingElement.put("source", sourceArray.get(i));
        sortingElement.put("target", arrayNode.remove(0));
        sortingArray.add(sortingElement);
    }/*from   w  ww.j  a  v  a2s  .  c o  m*/

    // sort array
    sortingArray.sort(new AvgAgeComparator(sortNode));

    // move nodes back to targetArray
    for (int i = 0; i < sortingArray.size(); i++) {
        arrayNode.add(sortingArray.get(i).get("target"));
    }
}