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

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

Introduction

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

Prototype

JsonNodeFactory instance

To view the source code for com.fasterxml.jackson.databind.node JsonNodeFactory instance.

Click Source Link

Usage

From source file:org.apache.usergrid.android.sdk.utils.JsonUtils.java

public static void setObjectProperty(Map<String, JsonNode> properties, String name, Object value) {
    if (value == null) {
        properties.remove(name);//  ww  w .  j  a  v  a  2s. c  o m
    } else {
        properties.put(name, JsonNodeFactory.instance.textNode(value.toString()));
    }
}

From source file:com.microsoft.rest.serializer.FlatteningSerializer.java

@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    if (value == null) {
        jgen.writeNull();//from w  w w .  j a v  a2  s.co m
        return;
    }

    // BFS for all collapsed properties
    ObjectNode root = mapper.valueToTree(value);
    ObjectNode res = root.deepCopy();
    Queue<ObjectNode> source = new LinkedBlockingQueue<ObjectNode>();
    Queue<ObjectNode> target = new LinkedBlockingQueue<ObjectNode>();
    source.add(root);
    target.add(res);
    while (!source.isEmpty()) {
        ObjectNode current = source.poll();
        ObjectNode resCurrent = target.poll();
        Iterator<Map.Entry<String, JsonNode>> fields = current.fields();
        while (fields.hasNext()) {
            Map.Entry<String, JsonNode> field = fields.next();
            ObjectNode node = resCurrent;
            String key = field.getKey();
            JsonNode outNode = resCurrent.get(key);
            if (field.getKey().matches(".+[^\\\\]\\..+")) {
                String[] values = field.getKey().split("((?<!\\\\))\\.");
                for (int i = 0; i < values.length; ++i) {
                    values[i] = values[i].replace("\\.", ".");
                    if (i == values.length - 1) {
                        break;
                    }
                    String val = values[i];
                    if (node.has(val)) {
                        node = (ObjectNode) node.get(val);
                    } else {
                        ObjectNode child = new ObjectNode(JsonNodeFactory.instance);
                        node.put(val, child);
                        node = child;
                    }
                }
                node.set(values[values.length - 1], resCurrent.get(field.getKey()));
                resCurrent.remove(field.getKey());
                outNode = node.get(values[values.length - 1]);
            }
            if (field.getValue() instanceof ObjectNode) {
                source.add((ObjectNode) field.getValue());
                target.add((ObjectNode) outNode);
            } else if (field.getValue() instanceof ArrayNode && (field.getValue()).size() > 0
                    && (field.getValue()).get(0) instanceof ObjectNode) {
                Iterator<JsonNode> sourceIt = field.getValue().elements();
                Iterator<JsonNode> targetIt = outNode.elements();
                while (sourceIt.hasNext()) {
                    source.add((ObjectNode) sourceIt.next());
                    target.add((ObjectNode) targetIt.next());
                }
            }
        }
    }
    jgen.writeTree(res);
}

From source file:io.liveoak.keycloak.AuthInterceptorTest.java

private static void setupAuthInterceptor() throws Exception {
    ObjectNode interceptorConfig = JsonNodeFactory.instance.objectNode();
    ObjectNode httpChainConfig = JsonNodeFactory.instance.objectNode().put("interceptor-name", "auth");
    interceptorConfig.putArray("http").add(httpChainConfig);
    loadExtension("interceptor", new InterceptorExtension(), interceptorConfig);
}

From source file:models.ConnectionContext.java

/**
 * Sends a command object to the connected client.
 *
 * @param command The command./* w  ww .j  a v  a 2 s .com*/
 * @param data The data for the command.
 */
public void sendCommand(final String command, final ObjectNode data) {
    //TODO(barp): Map with a POJO mapper [MAI-184]
    final ObjectNode node = JsonNodeFactory.instance.objectNode();
    node.put("command", command);
    node.set("data", data);
    _connection.write(node);
}

From source file:services.GoogleComputeEngineService.java

public static JsonNode listInstances(List<String> tags) throws GoogleComputeEngineException {
    checkAuthentication();/*w  ww .  j a va 2 s  .co m*/
    ObjectNode instances = Json.newObject();
    for (Instance i : client.getInstances(tags)) {
        ObjectNode instance = Json.newObject().put("name", i.getName()).put("description", i.getDescription())
                .put("type", i.getMachineType()).put("status", i.getStatus());
        ObjectNode disks = Json.newObject();
        for (AttachedDisk d : i.getDisks()) {
            disks.set(String.valueOf(d.getIndex()),
                    Json.newObject().put("name", d.getDeviceName()).put("type", d.getType())
                            .put("mode", d.getMode()).put("boot", d.getBoot())
                            .put("autodelete", d.getAutoDelete()));
        }
        instance.set("disks", disks);
        ObjectNode interfaces = Json.newObject();
        for (NetworkInterface n : i.getNetworkInterfaces()) {
            interfaces.set(n.getName(),
                    Json.newObject().put("network", n.getNetwork()).put("address", n.getNetworkIP()));
        }
        instance.set("network-interfaces", interfaces);
        instance.set("disks", disks);
        ArrayNode tagList = new ArrayNode(JsonNodeFactory.instance);
        if (i.getTags() != null && i.getTags().getItems() != null) {
            i.getTags().getItems().forEach(tagList::add);
        }
        instance.set("tags", tagList);
        instances.set(i.getId().toString(), instance);
    }
    return Json.newObject().set("instances", instances);
}

From source file:io.confluent.connect.elasticsearch.Mapping.java

private static JsonNode inferPrimitive(String type, Object defaultValue) {
    if (type == null) {
        throw new ConnectException("Invalid primitive type.");
    }//from  w w w  .  j a  v  a 2s.c  om

    ObjectNode obj = JsonNodeFactory.instance.objectNode();
    obj.set("type", JsonNodeFactory.instance.textNode(type));
    JsonNode defaultValueNode = null;
    if (defaultValue != null) {
        switch (type) {
        case ElasticsearchSinkConnectorConstants.BYTE_TYPE:
            defaultValueNode = JsonNodeFactory.instance.numberNode((byte) defaultValue);
            break;
        case ElasticsearchSinkConnectorConstants.SHORT_TYPE:
            defaultValueNode = JsonNodeFactory.instance.numberNode((short) defaultValue);
            break;
        case ElasticsearchSinkConnectorConstants.INTEGER_TYPE:
            defaultValueNode = JsonNodeFactory.instance.numberNode((int) defaultValue);
            break;
        case ElasticsearchSinkConnectorConstants.LONG_TYPE:
            defaultValueNode = JsonNodeFactory.instance.numberNode((long) defaultValue);
            break;
        case ElasticsearchSinkConnectorConstants.FLOAT_TYPE:
            defaultValueNode = JsonNodeFactory.instance.numberNode((float) defaultValue);
            break;
        case ElasticsearchSinkConnectorConstants.DOUBLE_TYPE:
            defaultValueNode = JsonNodeFactory.instance.numberNode((double) defaultValue);
            break;
        case ElasticsearchSinkConnectorConstants.STRING_TYPE:
            defaultValueNode = JsonNodeFactory.instance.textNode((String) defaultValue);
            break;
        case ElasticsearchSinkConnectorConstants.BOOLEAN_TYPE:
            defaultValueNode = JsonNodeFactory.instance.booleanNode((boolean) defaultValue);
            break;
        case ElasticsearchSinkConnectorConstants.DATE_TYPE:
            defaultValueNode = JsonNodeFactory.instance.numberNode((long) defaultValue);
            break;
        default:
            throw new DataException("Invalid primitive type.");
        }
    }
    if (defaultValueNode != null) {
        obj.set("null_value", defaultValueNode);
    }
    return obj;
}

From source file:org.lenskit.eval.traintest.metrics.MetricLoaderHelper.java

@Nullable
public <T> T createMetric(Class<T> type, JsonNode node) {
    String typeName = getMetricTypeName(node);
    if (typeName == null) {
        return null;
    }//w ww .j  a  v  a 2 s.c o m
    if (!node.isObject()) {
        node = JsonNodeFactory.instance.objectNode().set("type", node);
    }

    Class<?> metric = findClass(typeName);
    if (metric == null) {
        logger.warn("could not find metric {} for ", typeName, type);
        return null;
    }
    for (Constructor<?> ctor : metric.getConstructors()) {
        if (ctor.getAnnotation(JsonCreator.class) != null) {
            return type.cast(SpecUtils.createMapper().convertValue(node, metric));
        }
    }

    // ok, just instantiate
    try {
        return type.cast(metric.newInstance());
    } catch (InstantiationException | IllegalAccessException e) {
        throw new RuntimeException("Cannot instantiate " + metric, e);
    }
}

From source file:org.kitesdk.apps.scheduled.Schedule.java

private TreeNode toJson() {

    JsonNodeFactory js = JsonNodeFactory.instance;

    ObjectNode root = js.objectNode();/*from  ww w  .  ja v  a2 s .  c o  m*/

    root.put(NAME, getName());
    root.put(JOBCLASS, getJobClass().getName());
    root.put(START_TIME, formatter.print(getStartTime()));
    root.put(FREQUENCY, getFrequency());

    ArrayNode views = js.arrayNode();

    for (ViewTemplate template : getViewTemplates().values()) {

        ObjectNode viewNode = views.addObject();

        viewNode.put(NAME, template.getName());
        viewNode.put(URI, template.getUriTemplate());
        viewNode.put(FREQUENCY, template.getFrequency());
        viewNode.put(TYPE, template.getInputType().getName());
        viewNode.put(IS_INPUT, template.isInput);
    }

    root.put(VIEWS, views);

    return root;
}

From source file:org.lenskit.data.dao.file.JSONEntityFormat.java

@Override
public ObjectNode toJSON() {
    JsonNodeFactory nf = JsonNodeFactory.instance;

    ObjectNode json = nf.objectNode();// w  w w .  j av  a  2  s  .co  m
    json.put("format", "json");
    json.put("entity_type", entityType.getName());

    if (!attributes.isEmpty()) {
        ObjectNode attrNode = json.putObject("attributes");
        for (Map.Entry<String, TypedName<?>> attr : attributes.entrySet()) {
            ObjectNode an = attrNode.putObject(attr.getKey());
            an.put("name", attr.getValue().getName());
            an.put("type", TypeUtils.makeTypeName(attr.getValue().getType()));
        }
    }

    return json;
}

From source file:net.hamnaberg.json.Property.java

private static ArrayNode toArray(List<Value> list) {
    ArrayNode array = JsonNodeFactory.instance.arrayNode();
    for (Value value : list) {
        array.add(value.asJson());/*from   ww w  .j a  va  2 s. c  o m*/
    }
    return array;
}