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

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

Introduction

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

Prototype

public Iterator<JsonNode> elements() 

Source Link

Usage

From source file:com.hpcloud.util.config.Configurations.java

private static void buildConfigFor(String path, Map<String, String> config, JsonNode node) {
    for (Iterator<Map.Entry<String, JsonNode>> i = node.fields(); i.hasNext();) {
        Map.Entry<String, JsonNode> field = i.next();
        if (field.getValue() instanceof ValueNode) {
            ValueNode valueNode = (ValueNode) field.getValue();
            config.put(DOT_JOINER.join(path, field.getKey()), valueNode.asText());
        } else if (field.getValue() instanceof ArrayNode) {
            StringBuilder combinedValue = new StringBuilder();
            ArrayNode arrayNode = (ArrayNode) field.getValue();
            for (Iterator<JsonNode> it = arrayNode.elements(); it.hasNext();) {
                String value = it.next().asText().replaceAll("^\"|\"$", "");
                if (combinedValue.length() > 0)
                    combinedValue.append(',');
                combinedValue.append(value);
            }/*  w ww.  j  av a 2s. c  o  m*/

            config.put(DOT_JOINER.join(path, field.getKey()), combinedValue.toString());
        }

        buildConfigFor(DOT_JOINER.join(path, field.getKey()), config, field.getValue());
    }
}

From source file:com.redhat.lightblue.query.ProjectionList.java

public static ProjectionList fromJson(ArrayNode node) {
    ArrayList<Projection> list = new ArrayList<>(node.size());
    for (Iterator<JsonNode> itr = node.elements(); itr.hasNext();) {
        list.add(BasicProjection.fromJson((ObjectNode) itr.next()));
    }// w w  w . ja  v a 2s.c  o m
    return new ProjectionList(list);
}

From source file:com.redhat.lightblue.query.CompositeSortKey.java

/**
 * Parses a composite sort key using the json array node
 *//* w w  w  .  j a  v a 2s  .c om*/
public static CompositeSortKey fromJson(ArrayNode node) {
    ArrayList<SortKey> l = new ArrayList<>(node.size());
    for (Iterator<JsonNode> itr = node.elements(); itr.hasNext();) {
        l.add(SortKey.fromJson((ObjectNode) itr.next()));
    }
    return new CompositeSortKey(l);
}

From source file:com.redhat.lightblue.query.UpdateExpressionList.java

/**
 * Parses an update expression list using the given json object
 */// w  w w  .ja  v  a  2 s.c om
public static UpdateExpressionList fromJson(ArrayNode node) {
    ArrayList<PartialUpdateExpression> list = new ArrayList<>(node.size());
    for (Iterator<JsonNode> itr = node.elements(); itr.hasNext();) {
        list.add(PartialUpdateExpression.fromJson((ObjectNode) itr.next()));
    }
    return new UpdateExpressionList(list);
}

From source file:org.apache.usergrid.java.client.utils.JsonUtils.java

@NotNull
public static ArrayList<Object> convertToArrayList(@NotNull final ArrayNode arrayNode) {
    ArrayList<Object> arrayList = new ArrayList<>();
    Iterator<JsonNode> iterator = arrayNode.elements();
    while (iterator.hasNext()) {
        arrayList.add(iterator.next());/*from   w w w.j a v a  2  s  . c o m*/
    }
    return arrayList;
}

From source file:com.redhat.lightblue.metadata.PredefinedFields.java

public static void updateArraySizes(JsonNodeFactory factory, ArrayNode node) {
    for (Iterator<JsonNode> itr = node.elements(); itr.hasNext();) {
        updateArraySizes(factory, itr.next());
    }//from   w  ww .j a v a  2s . com
}

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

static FindReplaceProperties fromJSON(String findReplaceJSON) {
    List<String> findHistory = new ArrayList<String>();
    List<String> replaceHistory = new ArrayList<String>();
    Map<String, Boolean> optionsMap = new HashMap<String, Boolean>();

    if (StringUtils.isNotBlank(findReplaceJSON)) {
        try {//w  w w .  j a  va2s.  c  om
            ObjectMapper mapper = new ObjectMapper();
            ObjectNode rootNode = (ObjectNode) mapper.readTree(findReplaceJSON);

            ArrayNode findHistoryNode = (ArrayNode) rootNode.get("findHistory");
            for (Iterator<JsonNode> it = findHistoryNode.elements(); it.hasNext();) {
                findHistory.add(it.next().asText());
            }

            ArrayNode replaceHistoryNode = (ArrayNode) rootNode.get("replaceHistory");
            for (Iterator<JsonNode> it = replaceHistoryNode.elements(); it.hasNext();) {
                replaceHistory.add(it.next().asText());
            }

            ObjectNode optionsNode = (ObjectNode) rootNode.get("options");
            for (Iterator<Entry<String, JsonNode>> it = optionsNode.fields(); it.hasNext();) {
                Entry<String, JsonNode> entry = it.next();

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

    return new FindReplaceProperties(findHistory, replaceHistory, optionsMap);
}

From source file:mobile.service.SelfInfoService.java

/**
 * ????/*from  w  ww .j a  v  a 2  s .c  o m*/
 * 
 * @param jobExpArray
 * @return
 */
public static ServiceResult saveJobExp(ArrayNode jobExpArray) {
    ArrayNode newJobExpArray = jobExpArray.deepCopy();
    Iterator<JsonNode> elements = newJobExpArray.elements();
    while (elements.hasNext()) {
        ObjectNode next = (ObjectNode) elements.next();
        if (next.hasNonNull("endYear") && next.get("endYear").asText().equals("-1")) {
            next.put("endYear", "");
        }
    }

    ObjectNode jobExpNode = Json.newObject();
    jobExpNode.set("jobExp", newJobExpArray);
    ObjectNodeResult objectNodeResult = Expert.saveExpertByJson(Context.current().session(), jobExpNode);

    return ServiceResult.create(objectNodeResult);
}

From source file:de.thingweb.typesystem.jsonschema.JsonSchemaType.java

public static JsonType getJsonType(JsonNode jsonSchemaNode) throws JsonSchemaException {
    JsonType jtype = null;/*from w w w.j a va  2  s.com*/

    if (jsonSchemaNode != null) {
        JsonNode typeNode = jsonSchemaNode.findValue("type");
        if (typeNode != null) {
            switch (typeNode.asText()) {
            case "boolean":
                jtype = new JsonBoolean();
                break;
            case "integer":
            case "number":
                boolean exclusiveMinimum = false;
                JsonNode exclusiveMinimumNode = jsonSchemaNode.findValue("exclusiveMinimum");
                if (exclusiveMinimumNode != null
                        && exclusiveMinimumNode.getNodeType() == JsonNodeType.BOOLEAN) {
                    exclusiveMinimum = exclusiveMinimumNode.asBoolean();
                }
                boolean exclusiveMaximum = false;
                JsonNode exclusiveMaximumNode = jsonSchemaNode.findValue("exclusiveMaximum");
                if (exclusiveMaximumNode != null
                        && exclusiveMaximumNode.getNodeType() == JsonNodeType.BOOLEAN) {
                    exclusiveMaximum = exclusiveMaximumNode.asBoolean();
                }

                if ("integer".equals(typeNode.asText())) {
                    jtype = new JsonInteger();
                    JsonNode minimumNode = jsonSchemaNode.findValue("minimum");
                    if (minimumNode != null && minimumNode.getNodeType() == JsonNodeType.NUMBER) {
                        ((JsonInteger) jtype).setMinimum(minimumNode.asLong());
                    }
                    JsonNode maximumNode = jsonSchemaNode.findValue("maximum");
                    if (maximumNode != null && maximumNode.getNodeType() == JsonNodeType.NUMBER) {
                        ((JsonInteger) jtype).setMaximum(maximumNode.asLong());
                    }
                } else {
                    assert ("number".equals(typeNode.asText()));

                    jtype = new JsonNumber();
                    JsonNode minimumNode = jsonSchemaNode.findValue("minimum");
                    if (minimumNode != null && minimumNode.getNodeType() == JsonNodeType.NUMBER) {
                        ((JsonNumber) jtype).setMinimum(minimumNode.asDouble());
                    }
                    JsonNode maximumNode = jsonSchemaNode.findValue("maximum");
                    if (maximumNode != null && maximumNode.getNodeType() == JsonNodeType.NUMBER) {
                        ((JsonNumber) jtype).setMaximum(maximumNode.asDouble());
                    }
                }

                ((AbstractJsonNumeric) jtype).setExclusiveMinimum(exclusiveMinimum);
                ((AbstractJsonNumeric) jtype).setExclusiveMaximum(exclusiveMaximum);

                break;
            case "null":
                jtype = new JsonNull();
                break;
            case "string":
                jtype = new JsonString();
                break;
            case "array":
                JsonNode itemsNode = jsonSchemaNode.findValue("items");
                if (itemsNode != null && JsonNodeType.OBJECT == itemsNode.getNodeType()) {
                    jtype = new JsonArray(getJsonType(itemsNode));
                } else {
                    throw new JsonSchemaException("items not object");
                }

                break;
            case "object":
                JsonNode propertiesNode = jsonSchemaNode.findValue("properties");
                if (propertiesNode != null && JsonNodeType.OBJECT == propertiesNode.getNodeType()) {
                    Iterator<Entry<String, JsonNode>> iter = propertiesNode.fields();
                    Map<String, JsonType> properties = new HashMap<String, JsonType>();
                    while (iter.hasNext()) {
                        Entry<String, JsonNode> e = iter.next();
                        JsonNode nodeProp = e.getValue();
                        properties.put(e.getKey(), getJsonType(nodeProp));
                    }
                    jtype = new JsonObject(properties);
                } else {
                    throw new JsonSchemaException("Properties not object");
                }
                // required
                JsonNode requiredNode = jsonSchemaNode.findValue("required");
                if (requiredNode != null && JsonNodeType.ARRAY == requiredNode.getNodeType()) {
                    ArrayNode an = (ArrayNode) requiredNode;
                    Iterator<JsonNode> iterReq = an.elements();
                    while (iterReq.hasNext()) {
                        JsonNode nreq = iterReq.next();
                        if (JsonNodeType.STRING == nreq.getNodeType()) {
                            ((JsonObject) jtype).addRequired(nreq.asText());
                        } else {
                            throw new JsonSchemaException("Unexpected required node: " + nreq);
                        }
                    }
                }
                break;
            }
        }
    }

    return jtype;
}

From source file:com.baasbox.dao.PermissionsHelper.java

/***
 * Set th ACL for a given document/*from  w w w  . j  av a  2 s  .c  o m*/
 * @param doc
 * @param aclJson the json representing the acl to set.
 * {"_allowRead":
 *   [
 *      {"name":"registered","isRole":true},
 *      {"name":"johnny", "isRole":false}
 *    ]
 * }
 * Keys are: _allowRead,_allowWrite,_allowDelete,_allowUpdate
 * @throws Exception
 */
public static void setAcl(ODocument doc, PermissionJsonWrapper acl) throws AclNotValidException {
    if (acl.getAclJson() == null)
        return;
    DbHelper.requestTransaction();
    try {
        revokeAll(doc);
        HashMap<Permissions, ArrayNode> hmp = new HashMap<Permissions, ArrayNode>();
        if (acl.getAllowRead() != null)
            hmp.put(Permissions.ALLOW_READ, acl.getAllowRead());
        if (acl.getAllowUpdate() != null)
            hmp.put(Permissions.ALLOW_UPDATE, acl.getAllowUpdate());
        if (acl.getAllowDelete() != null)
            hmp.put(Permissions.ALLOW_DELETE, acl.getAllowDelete());

        Iterator<Entry<Permissions, ArrayNode>> itAllows = hmp.entrySet().iterator();
        while (itAllows.hasNext()) {
            Entry<Permissions, ArrayNode> allows = itAllows.next();
            ArrayNode allow = allows.getValue();
            Permissions perm = allows.getKey();
            Iterator<JsonNode> itElements = allow.elements();
            while (itElements.hasNext()) {
                JsonNode elem = itElements.next();
                if (!elem.isObject())
                    throw new AclNotValidException(Type.ACL_NOT_OBJECT,
                            perm + " must contains array of objects");
                String name = ((ObjectNode) elem).get("name").asText();
                if (StringUtils.isEmpty(name))
                    throw new AclNotValidException(Type.ACL_KEY_NOT_VALID,
                            "An element of the " + perm + " field has no name");
                boolean isRole = isARole(elem);
                if (!isRole) {
                    grant(doc, perm, UserService.getOUserByUsername(name));
                } else
                    grant(doc, perm, RoleService.getORole(name));
            }
        }
        DbHelper.commitTransaction();
    } catch (AclNotValidException e) {
        DbHelper.rollbackTransaction();
        throw e;
    } catch (Exception e) {
        DbHelper.rollbackTransaction();
        throw e;
    }
}