Example usage for com.fasterxml.jackson.databind.node JsonNodeFactory nullNode

List of usage examples for com.fasterxml.jackson.databind.node JsonNodeFactory nullNode

Introduction

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

Prototype

public NullNode nullNode() 

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 {/*from  www  .ja va 2 s. c om*/
            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.ikanow.aleph2.search_service.elasticsearch.utils.JsonNodeWritableUtils.java

/** Utility that goes from Writable to JsonNode
 * @param x/* ww  w  . ja v  a2  s  .  co  m*/
 * @return
 */
protected static JsonNode transform(Object x, JsonNodeFactory nc) {
    if (null == x) {
        return null; //(missing->missing)
    } else if (x instanceof NullWritable) {
        return nc.nullNode();
    } else if (x instanceof BooleanWritable) {
        return nc.booleanNode(((BooleanWritable) x).get());
    } else if (x instanceof Text) {
        return nc.textNode(((Text) x).toString());
    } else if (x instanceof ByteWritable) {
        return nc.binaryNode(new byte[] { ((ByteWritable) x).get() });
    } else if (x instanceof IntWritable) {
        return nc.numberNode(((IntWritable) x).get());
    } else if (x instanceof VIntWritable) {
        return nc.numberNode(((VIntWritable) x).get());
    } else if (x instanceof LongWritable) {
        return nc.numberNode(((LongWritable) x).get());
    } else if (x instanceof VLongWritable) {
        return nc.numberNode(((VLongWritable) x).get());
    } else if (x instanceof BytesWritable) {
        return nc.binaryNode(((BytesWritable) x).getBytes());
    } else if (x instanceof DoubleWritable) {
        return nc.numberNode(((DoubleWritable) x).get());
    } else if (x instanceof FloatWritable) {
        return nc.numberNode(((FloatWritable) x).get());
    } else if (x instanceof ArrayWritable) {
        Writable[] xx = ((ArrayWritable) x).get();
        // (don't do this lazily, construct entire thing once requested)
        return new ArrayNodeWrapper(nc, xx);
    } else if (x instanceof MapWritable) { // recurse! (ish)
        return new ObjectNodeWrapper(nc, (MapWritable) x);
    } else
        return nc.nullNode();
}

From source file:me.tfeng.play.avro.AvroHelper.java

private static JsonNode convertFromSimpleRecord(Schema schema, JsonNode json, JsonNodeFactory factory)
        throws IOException {
    if (json.isObject() && schema.getType() == Type.RECORD) {
        ObjectNode node = (ObjectNode) json;
        ObjectNode newNode = factory.objectNode();
        for (Field field : schema.getFields()) {
            String fieldName = field.name();
            if (node.has(fieldName)) {
                newNode.put(fieldName, convertFromSimpleRecord(field.schema(), node.get(fieldName), factory));
            } else if (field.defaultValue() != null) {
                newNode.put(fieldName, Json.parse(field.defaultValue().toString()));
            } else {
                newNode.put(fieldName, factory.nullNode());
            }//from w  ww.j a va2s .c  o m
        }
        return newNode;
    } else if (json.isObject() && schema.getType() == Type.MAP) {
        ObjectNode node = (ObjectNode) json;
        ObjectNode newNode = factory.objectNode();
        Schema valueType = schema.getValueType();
        Iterator<Entry<String, JsonNode>> entries = node.fields();
        while (entries.hasNext()) {
            Entry<String, JsonNode> entry = entries.next();
            newNode.put(entry.getKey(), convertFromSimpleRecord(valueType, entry.getValue(), factory));
        }
        return newNode;
    } else if (schema.getType() == Type.UNION) {
        Schema type = AvroHelper.getSimpleUnionType(schema);
        if (type == null) {
            if (json.isNull()) {
                return json;
            } else {
                ObjectNode node = (ObjectNode) json;
                Entry<String, JsonNode> entry = node.fields().next();
                for (Schema unionType : schema.getTypes()) {
                    if (unionType.getFullName().equals(entry.getKey())) {
                        ObjectNode newNode = factory.objectNode();
                        newNode.put(entry.getKey(),
                                convertFromSimpleRecord(unionType, entry.getValue(), factory));
                        return newNode;
                    }
                }
                throw new IOException("Unable to get schema for type " + entry.getKey() + " in union");
            }
        } else if (json.isNull()) {
            return json;
        } else {
            ObjectNode newNode = factory.objectNode();
            newNode.put(type.getFullName(), convertFromSimpleRecord(type, json, factory));
            return newNode;
        }
    } else if (json.isArray() && schema.getType() == Type.ARRAY) {
        ArrayNode node = (ArrayNode) json;
        ArrayNode newNode = factory.arrayNode();
        Iterator<JsonNode> iterator = node.elements();
        while (iterator.hasNext()) {
            newNode.add(convertFromSimpleRecord(schema.getElementType(), iterator.next(), factory));
        }
        return newNode;
    } else {
        return json;
    }
}

From source file:me.tfeng.toolbox.avro.AvroHelper.java

private static JsonNode convertFromSimpleRecord(Schema schema, JsonNode json, JsonNodeFactory factory)
        throws IOException {
    if (json.isObject() && schema.getType() == Type.RECORD) {
        ObjectNode node = (ObjectNode) json;
        ObjectNode newNode = factory.objectNode();
        for (Field field : schema.getFields()) {
            String fieldName = field.name();
            if (node.has(fieldName)) {
                newNode.set(fieldName, convertFromSimpleRecord(field.schema(), node.get(fieldName), factory));
            } else if (field.defaultValue() != null) {
                newNode.set(fieldName, MAPPER.readTree(field.defaultValue().toString()));
            } else {
                newNode.set(fieldName, factory.nullNode());
            }//from  ww w .j  av  a 2 s  .c o  m
        }
        return newNode;
    } else if (json.isObject() && schema.getType() == Type.MAP) {
        ObjectNode node = (ObjectNode) json;
        ObjectNode newNode = factory.objectNode();
        Schema valueType = schema.getValueType();
        Iterator<Entry<String, JsonNode>> entries = node.fields();
        while (entries.hasNext()) {
            Entry<String, JsonNode> entry = entries.next();
            newNode.set(entry.getKey(), convertFromSimpleRecord(valueType, entry.getValue(), factory));
        }
        return newNode;
    } else if (schema.getType() == Type.UNION) {
        Schema type = getSimpleUnionType(schema);
        if (type == null) {
            if (json.isNull()) {
                return json;
            } else {
                ObjectNode node = (ObjectNode) json;
                Entry<String, JsonNode> entry = node.fields().next();
                for (Schema unionType : schema.getTypes()) {
                    if (unionType.getFullName().equals(entry.getKey())) {
                        ObjectNode newNode = factory.objectNode();
                        newNode.set(entry.getKey(),
                                convertFromSimpleRecord(unionType, entry.getValue(), factory));
                        return newNode;
                    }
                }
                throw new IOException("Unable to get schema for type " + entry.getKey() + " in union");
            }
        } else if (json.isNull()) {
            return json;
        } else {
            ObjectNode newNode = factory.objectNode();
            newNode.set(type.getFullName(), convertFromSimpleRecord(type, json, factory));
            return newNode;
        }
    } else if (json.isArray() && schema.getType() == Type.ARRAY) {
        ArrayNode node = (ArrayNode) json;
        ArrayNode newNode = factory.arrayNode();
        Iterator<JsonNode> iterator = node.elements();
        while (iterator.hasNext()) {
            newNode.add(convertFromSimpleRecord(schema.getElementType(), iterator.next(), factory));
        }
        return newNode;
    } else {
        return json;
    }
}

From source file:com.redhat.lightblue.metadata.rdbms.converter.Range.java

@Override
public String toString() {
    JsonNodeFactory jsonNodeFactory = JsonNodeFactory.withExactBigDecimals(true);

    ObjectNode objectNode = jsonNodeFactory.objectNode();

    JsonNode jsonNode = from == null ? jsonNodeFactory.nullNode() : jsonNodeFactory.numberNode(from);
    objectNode.set("from", jsonNode);

    jsonNode = to == null ? jsonNodeFactory.nullNode() : jsonNodeFactory.numberNode(to);
    objectNode.set("to", jsonNode);

    return JsonUtils.prettyPrint(objectNode);
}

From source file:com.ikanow.aleph2.v1.document_db.utils.JsonNodeBsonUtils.java

/** Utility that goes from various MongoDB artefacts to JsonNode
 * @param x//from   ww w .  j av  a 2 s  .c o m
 * @return
 */
protected static JsonNode transform(Object x, JsonNodeFactory nc) {
    if (null == x) { // missing => missing
        return null;
    } else if (x instanceof ObjectId) {
        return nc.textNode(((ObjectId) x).toString());
    } else if (x instanceof Boolean) {
        return nc.booleanNode(((Boolean) x));
    } else if (x instanceof String) {
        return nc.textNode(((String) x));
    } else if (x instanceof Date) {
        return nc.numberNode(((Date) x).getTime());
    } else if (x instanceof Double) {
        return nc.numberNode(((Double) x));
    } else if (x instanceof Float) {
        return nc.numberNode(((Float) x));
    } else if (x instanceof Long) {
        return nc.numberNode(((Long) x));
    } else if (x instanceof Integer) {
        return nc.numberNode(((Integer) x));
    } else if (x instanceof byte[]) {
        return nc.binaryNode(((byte[]) x));
    } else if (x instanceof BasicBSONList) {
        // (don't do this lazily, construct entire thing once requested)
        return new ArrayNodeWrapper(nc, (BasicBSONList) x);
    } else if (x instanceof BSONObject) { // recurse! (ish)
        return new ObjectNodeWrapper(nc, (BSONObject) x);
    } else
        return nc.nullNode();
}

From source file:com.redhat.lightblue.metadata.rdbms.converter.RDBMSContext.java

@Override
public String toString() {
    JsonNodeFactory jsonNodeFactory1 = jsonNodeFactory;
    if (jsonNodeFactory == null) {
        jsonNodeFactory1 = JsonNodeFactory.withExactBigDecimals(true);
    }/*from  w  w  w. j a va2s. c om*/
    ObjectNode objectNode = jsonNodeFactory1.objectNode();
    NullNode nullNode = jsonNodeFactory1.nullNode();
    String nullNodeString = JsonUtils.prettyPrint(nullNode);
    String s = null;
    JsonNode j = null;
    try {

        s = fromToQueryRange == null ? nullNodeString : fromToQueryRange.toString();
        objectNode.set("fromToQueryRange", JsonUtils.json(s));

        j = dataSource == null ? nullNode : jsonNodeFactory1.textNode(dataSource.toString());
        objectNode.set("dataSource", j);

        j = dataSourceName == null ? nullNode : jsonNodeFactory1.textNode(dataSourceName.toString());
        objectNode.set("dataSourceName", j);

        j = connection == null ? nullNode : jsonNodeFactory1.textNode(connection.toString());
        objectNode.set("connection", j);

        j = preparedStatement == null ? nullNode : jsonNodeFactory1.textNode(preparedStatement.toString());
        objectNode.set("preparedStatement", j);

        j = resultBoolean == null ? nullNode : jsonNodeFactory1.booleanNode(resultBoolean);
        objectNode.set("resultBoolean", j);

        j = resultInteger == null ? nullNode : jsonNodeFactory1.numberNode(resultInteger);
        objectNode.set("resultInteger", j);

        j = rowMapper == null ? nullNode : jsonNodeFactory1.textNode(rowMapper.toString());
        objectNode.set("rowMapper", j);

        if (resultList != null) {
            ArrayNode jsonNodes = jsonNodeFactory1.arrayNode();
            for (T a : resultList) {
                jsonNodes.add(a.toString());
            }
            objectNode.set("resultList", jsonNodes);
        } else {
            objectNode.set("resultList", nullNode);
        }

        s = rdbms == null ? nullNodeString : rdbms.toString();
        objectNode.set("rdbms", JsonUtils.json(s));

        j = sql == null ? nullNode : jsonNodeFactory1.textNode(sql.toString());
        objectNode.set("sql", j);

        j = type == null ? nullNode : jsonNodeFactory1.textNode(type.toString());
        objectNode.set("type", j);

        j = entityMetadata == null ? nullNode : jsonNodeFactory1.textNode(entityMetadata.toString());
        objectNode.set("entityMetadata", j);

        j = queryExpression == null ? nullNode : jsonNodeFactory1.textNode(queryExpression.toString());
        objectNode.set("queryExpression", j);

        j = projection == null ? nullNode : jsonNodeFactory1.textNode(projection.toString());
        objectNode.set("projection", j);

        j = sort == null ? nullNode : jsonNodeFactory1.textNode(sort.toString());
        objectNode.set("sort", j);

        if (temporaryVariable != null) {
            ObjectNode jsonNodes = jsonNodeFactory1.objectNode();
            for (Map.Entry<String, Object> a : temporaryVariable.entrySet()) {
                jsonNodes.set(a.getKey(), jsonNodeFactory1.textNode(a.getValue().toString()));
            }
            objectNode.set("temporaryVariable", jsonNodes);
        } else {
            objectNode.set("temporaryVariable", nullNode);
        }

        if (in != null) {
            ArrayNode jsonNodes = jsonNodeFactory1.arrayNode();
            for (InOut a : in) {
                jsonNodes.add(a.toString());
            }
            objectNode.set("in", jsonNodes);
        } else {
            objectNode.set("in", nullNode);
        }

        s = out == null ? nullNodeString : out.toString();
        if (out != null) {
            ArrayNode jsonNodes = jsonNodeFactory1.arrayNode();
            for (InOut a : out) {
                jsonNodes.add(a.toString());
            }
            objectNode.set("out", jsonNodes);
        } else {
            objectNode.set("out", nullNode);
        }

        s = inVar == null ? nullNodeString : inVar.toString();
        objectNode.set("inVar", JsonUtils.json(s));

        s = outVar == null ? nullNodeString : outVar.toString();
        objectNode.set("outVar", JsonUtils.json(s));

        objectNode.set("initialInput", JsonUtils.json(Boolean.toString(initialInput)));

        if (inputMappedByField != null) {
            ObjectNode jsonNodes = jsonNodeFactory1.objectNode();
            for (Map.Entry<String, Object> a : inputMappedByField.entrySet()) {
                jsonNodes.set(a.getKey(), jsonNodeFactory1.textNode(a.getValue().toString()));
            }
            objectNode.set("inputMappedByField", jsonNodes);
        } else {
            objectNode.set("inputMappedByField", nullNode);
        }

        if (inputMappedByColumn != null) {
            ObjectNode jsonNodes = jsonNodeFactory1.objectNode();
            for (Map.Entry<String, Object> a : inputMappedByColumn.entrySet()) {
                jsonNodes.set(a.getKey(), jsonNodeFactory1.textNode(a.getValue().toString()));
            }
            objectNode.set("inputMappedByColumn", jsonNodes);
        } else {
            objectNode.set("inputMappedByColumn", nullNode);
        }

        s = jsonNodeFactory == null ? nullNodeString : jsonNodeFactory.toString();
        objectNode.set("jsonNodeFactory", jsonNodeFactory1.textNode(s));

        s = RDBMSDataSourceResolver == null ? nullNodeString : RDBMSDataSourceResolver.toString();
        objectNode.set("RDBMSDataSourceResolver", JsonUtils.json(s));

        s = fieldAccessRoleEvaluator == null ? nullNodeString : fieldAccessRoleEvaluator.toString();
        objectNode.set("fieldAccessRoleEvaluator", jsonNodeFactory1.textNode(s));

        s = CRUDOperationName == null ? nullNodeString : CRUDOperationName.toString();
        objectNode.set("CRUDOperationName", jsonNodeFactory1.textNode(s));

        s = crudOperationContext == null ? nullNodeString : crudOperationContext.toString();
        objectNode.set("crudOperationContext", jsonNodeFactory1.textNode(s));

        s = currentLoopOperator == null ? nullNodeString : currentLoopOperator.toString();
        objectNode.set("currentLoopOperator", jsonNodeFactory1.textNode(s));

        s = updateExpression == null ? nullNodeString : updateExpression.toString();
        objectNode.set("updateExpression", jsonNodeFactory1.textNode(s));

    } catch (IOException e) {
        throw new IllegalStateException(e);
    }

    return JsonUtils.prettyPrint(objectNode);
}

From source file:net.mostlyharmless.jghservice.connector.jira.CreateIssue.java

@Override
public String getJson() throws JsonProcessingException {
    JsonNodeFactory factory = JsonNodeFactory.instance;
    ObjectNode newNode = factory.objectNode();
    ObjectNode fields = factory.objectNode();

    if (projectKey != null) {
        ObjectNode project = factory.objectNode();
        project.put("key", projectKey);
        fields.put("project", project);
    }//from   w w  w .j  a v a  2 s  .  c o m

    if (issuetype != null) {
        ObjectNode iType = factory.objectNode();
        iType.put("name", issuetype);
        fields.put("issuetype", iType);
    }

    if (summary != null) {
        fields.put("summary", summary);
    }

    if (description != null) {
        fields.put("description", description);
    }

    if (!fixVersions.isEmpty()) {
        ArrayNode array = factory.arrayNode();
        for (String version : fixVersions) {
            ObjectNode node = factory.objectNode();
            node.put("name", version);
            array.add(node);
        }
        fields.put("fixVersions", array);
    }

    if (!affectsVersions.isEmpty()) {
        ArrayNode array = factory.arrayNode();
        for (String version : affectsVersions) {
            ObjectNode node = factory.objectNode();
            node.put("name", version);
            array.add(node);
        }
        fields.put("versions", array);
    }

    if (assignee != null) {
        ObjectNode node = factory.objectNode();
        if (assignee.isEmpty()) {
            node.put("name", factory.nullNode());
        } else {
            node.put("name", assignee);
        }
        fields.put("assignee", node);
    }

    for (Map.Entry<String, JsonNode> entry : customFields.entrySet()) {
        fields.put(entry.getKey(), entry.getValue());
    }

    newNode.put("fields", fields);

    return new ObjectMapper().writeValueAsString(newNode);

}