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

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

Introduction

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

Prototype

public int size() 

Source Link

Usage

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

/**
 * Parses a field comparison or value comparison expression from the given
 * json object//from  w w  w .j  a  v  a 2s  .  c o m
 */
public static BinaryRelationalExpression fromJson(ObjectNode node) {
    if (node.size() == 3) {
        JsonNode x = node.get("op");
        if (x != null) {
            BinaryComparisonOperator op = BinaryComparisonOperator.fromString(x.asText());
            if (op != null) {
                x = node.get("field");
                if (x != null) {
                    Path field = new Path(x.asText());
                    x = node.get("rfield");
                    if (x != null) {
                        return new FieldComparisonExpression(field, op, new Path(x.asText()));
                    } else {
                        x = node.get("rvalue");
                        if (x != null) {
                            return new ValueComparisonExpression(field, op, Value.fromJson(x));
                        }
                    }
                }
            }
        }
    }
    throw Error.get(QueryConstants.ERR_INVALID_COMPARISON_EXPRESSION, node.toString());
}

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

/**
 * Parses an n-ary relational expression from the given json object
 *//*from   w  w  w.ja  va  2s. c  o m*/
public static NaryRelationalExpression fromJson(ObjectNode node) {
    if (node.size() == 3) {
        if (node.get("rfield") != null) {
            return NaryFieldRelationalExpression.fromJson(node);
        } else if (node.get("values") != null) {
            return NaryValueRelationalExpression.fromJson(node);
        }
    }
    throw Error.get(QueryConstants.ERR_INVALID_COMPARISON_EXPRESSION, node.toString());
}

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

/**
 * Parses a unary logical expression using the given object node
 *//*from  w ww.j  a  va2s. co  m*/
public static UnaryLogicalExpression fromJson(ObjectNode node) {
    if (node.size() != 1) {
        throw Error.get(QueryConstants.ERR_INVALID_LOGICAL_EXPRESSION, node.toString());
    }
    String fieldName = node.fieldNames().next();
    UnaryLogicalOperator op = UnaryLogicalOperator.fromString(fieldName);
    if (op == null) {
        throw Error.get(QueryConstants.ERR_INVALID_LOGICAL_EXPRESSION, node.toString());
    }
    QueryExpression q = QueryExpression.fromJson(node.get(fieldName));
    return new UnaryLogicalExpression(op, q);
}

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

/**
 * Parses an n-ary relational expression from the given json object
 *//*from   w w  w . j a  v  a  2 s  . com*/
public static NaryFieldRelationalExpression fromJson(ObjectNode node) {
    if (node.size() == 3) {
        JsonNode x = node.get("op");
        if (x != null) {
            NaryRelationalOperator op = NaryRelationalOperator.fromString(x.asText());
            if (op != null) {
                x = node.get("field");
                if (x != null) {
                    Path field = new Path(x.asText());
                    x = node.get("rfield");
                    if (x != null) {
                        return new NaryFieldRelationalExpression(field, op, new Path(x.asText()));
                    }
                }
            }
        }
    }
    throw Error.get(QueryConstants.ERR_INVALID_COMPARISON_EXPRESSION, node.toString());
}

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

public static SortKey fromJson(ObjectNode node) {
    if (node.size() != 1) {
        throw Error.get(QueryConstants.ERR_INVALID_SORT, node.toString());
    }//from   w ww.j a v  a  2s . c  o  m
    String fieldString = node.fieldNames().next();
    String dir = node.get(fieldString).asText();
    Path field = new Path(fieldString);
    boolean desc = false;
    switch (dir) {
    case "$asc":
        desc = false;
        break;
    case "$desc":
        desc = true;
        break;
    default:
        throw Error.get(QueryConstants.ERR_INVALID_SORT, node.toString());
    }
    return new SortKey(field, desc);
}

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

/**
 * Parses an n-ary logical expression from the given json object
 *///from ww  w.  j av  a 2s .  co m
public static NaryLogicalExpression fromJson(ObjectNode node) {
    if (node.size() != 1) {
        throw Error.get(QueryConstants.ERR_INVALID_LOGICAL_EXPRESSION, node.toString());
    }
    String fieldName = node.fieldNames().next();
    NaryLogicalOperator op = NaryLogicalOperator.fromString(fieldName);
    if (op == null) {
        throw Error.get(QueryConstants.ERR_INVALID_LOGICAL_EXPRESSION, node.toString());
    }
    JsonNode x = node.get(fieldName);
    if (x instanceof ArrayNode) {
        ArrayList<QueryExpression> list = new ArrayList<>(((ArrayNode) x).size());
        for (Iterator<JsonNode> itr = ((ArrayNode) x).elements(); itr.hasNext();) {
            list.add(QueryExpression.fromJson(itr.next()));
        }
        return new NaryLogicalExpression(op, list);
    } else {
        throw Error.get(QueryConstants.ERR_INVALID_LOGICAL_EXPRESSION, node.toString());
    }
}

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

/**
 * Parses an n-ary relational expression from the given json object
 *///from   w w w  .  j  a  va  2 s . c o m
public static NaryValueRelationalExpression fromJson(ObjectNode node) {
    if (node.size() == 3) {
        JsonNode x = node.get("op");
        if (x != null) {
            NaryRelationalOperator op = NaryRelationalOperator.fromString(x.asText());
            if (op != null) {
                x = node.get("field");
                if (x != null) {
                    Path field = new Path(x.asText());
                    x = node.get("values");
                    if (x instanceof ArrayNode) {
                        ArrayList<Value> values = new ArrayList<>(((ArrayNode) x).size());
                        for (Iterator<JsonNode> itr = ((ArrayNode) x).elements(); itr.hasNext();) {
                            values.add(Value.fromJson(itr.next()));
                        }
                        return new NaryValueRelationalExpression(field, op, values);
                    }
                }
            }
        }
    }
    throw Error.get(QueryConstants.ERR_INVALID_COMPARISON_EXPRESSION, node.toString());
}

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

/**
 * Parses an unset expression using the given json object
 *///from   www  .ja v  a 2 s  .com
public static UnsetExpression fromJson(ObjectNode node) {
    if (node.size() == 1) {
        JsonNode val = node.get(UpdateOperator._unset.toString());
        if (val != null) {
            List<Path> fields = new ArrayList<>();
            if (val instanceof ArrayNode) {
                for (Iterator<JsonNode> itr = ((ArrayNode) val).elements(); itr.hasNext();) {
                    fields.add(new Path(itr.next().asText()));
                }
            } else if (val.isValueNode()) {
                fields.add(new Path(val.asText()));
            }
            return new UnsetExpression(fields);
        }
    }
    throw Error.get(QueryConstants.ERR_INVALID_UNSET_EXPRESSION, node.toString());
}

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

/**
 * Parses a set expression using the given json object
 *///from w  w  w  .ja  va2  s.  c o m
public static SetExpression fromJson(ObjectNode node) {
    if (node.size() == 1) {
        UpdateOperator op = null;
        if (node.has(UpdateOperator._add.toString())) {
            op = UpdateOperator._add;
        } else if (node.has(UpdateOperator._set.toString())) {
            op = UpdateOperator._set;
        }
        if (op != null) {
            ObjectNode arg = (ObjectNode) node.get(op.toString());
            List<FieldAndRValue> list = new ArrayList<>();
            for (Iterator<Map.Entry<String, JsonNode>> itr = arg.fields(); itr.hasNext();) {
                Map.Entry<String, JsonNode> entry = itr.next();
                Path field = new Path(entry.getKey());
                RValueExpression rvalue = RValueExpression.fromJson(entry.getValue());
                list.add(new FieldAndRValue(field, rvalue));
            }
            return new SetExpression(op, list);
        }
    }
    throw Error.get(QueryConstants.ERR_INVALID_SET_EXPRESSION, node.toString());
}

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

/**
 * Parses a foreach expression from the given json object
 *//* www.jav a  2s .c o  m*/
public static ForEachExpression fromJson(ObjectNode node) {
    if (node.size() == 1) {
        JsonNode argNode = node.get("$foreach");
        if (argNode instanceof ObjectNode) {
            ObjectNode objArg = (ObjectNode) argNode;
            if (objArg.size() == 2) {
                JsonNode updateNode = null;
                JsonNode queryNode = null;
                Path field = null;
                for (Iterator<Map.Entry<String, JsonNode>> itr = objArg.fields(); itr.hasNext();) {
                    Map.Entry<String, JsonNode> entry = itr.next();
                    if ("$update".equals(entry.getKey())) {
                        updateNode = entry.getValue();
                    } else {
                        field = new Path(entry.getKey());
                        queryNode = entry.getValue();
                    }
                }
                if (queryNode != null && updateNode != null && field != null) {
                    return new ForEachExpression(field, UpdateQueryExpression.fromJson(queryNode),
                            ForEachUpdateExpression.fromJson(updateNode));
                }
            }
        }
    }
    throw Error.get(QueryConstants.ERR_INVALID_ARRAY_UPDATE_EXPRESSION, node.toString());
}