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

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

Introduction

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

Prototype

public ArrayNode arrayNode() 

Source Link

Usage

From source file:com.flipkart.zjsonpatch.JsonDiff.java

private static ArrayNode getJsonNodes(List<Diff> diffs) {
    JsonNodeFactory FACTORY = JsonNodeFactory.instance;
    final ArrayNode patch = FACTORY.arrayNode();
    for (Diff diff : diffs) {
        ObjectNode jsonNode = getJsonNode(FACTORY, diff);
        patch.add(jsonNode);/*from   ww  w  . java  2s.c  o m*/
    }
    return patch;
}

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

/**
 * Combines all Json documents in a list into a single Json document
 *
 * @param docs List of JsonDoc objects//from   w  ww.j av a2 s  . c  om
 * @param nodeFactory Json node factory
 *
 * @return If the list has only one document, returns an ObjectNode,
 * otherwise returns an array node containing each document in array
 * elements
 */
public static JsonNode listToDoc(List<JsonDoc> docs, JsonNodeFactory nodeFactory) {
    if (docs == null) {
        return null;
    } else if (docs.isEmpty()) {
        return nodeFactory.arrayNode();
    } else if (docs.size() == 1) {
        return docs.get(0).getRoot();
    } else {
        ArrayNode node = nodeFactory.arrayNode();
        for (JsonDoc doc : docs) {
            node.add(doc.getRoot());
        }
        return node;
    }
}

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   w w  w .j av  a  2s .c o  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.mapr.synth.samplers.FileSampler.java

private void readDelimitedData(String lookup, List<String> lines) {
    Splitter splitter;//from   www .  jav a2  s . c  om
    if (lookup.matches(".*\\.csv")) {
        splitter = Splitter.on(",");
    } else if (lookup.matches(".*\\.tsv")) {
        splitter = Splitter.on("\t");
    } else {
        throw new IllegalArgumentException("Must have file with .csv, .tsv or .json suffix");
    }

    List<String> names = Lists.newArrayList(splitter.split(lines.get(0)));
    JsonNodeFactory nf = JsonNodeFactory.withExactBigDecimals(false);
    ArrayNode localData = nf.arrayNode();
    for (String line : lines.subList(1, lines.size())) {
        ObjectNode r = nf.objectNode();
        List<String> fields = Lists.newArrayList(splitter.split(line));
        Preconditions.checkState(names.size() == fields.size(), "Wrong number of fields, expected ",
                names.size(), fields.size());
        Iterator<String> ix = names.iterator();
        for (String field : fields) {
            r.put(ix.next(), field);
        }
        localData.add(r);
    }
    data = localData;
}

From source file:org.springframework.data.rest.webmvc.json.patch.JsonPatchPatchConverter.java

/**
 * Renders a {@link Patch} as a {@link JsonNode}.
 * /*  ww w.j a  v  a2s  .c  om*/
 * @param patch the patch
 * @return a {@link JsonNode} containing JSON Patch.
 */
public JsonNode convert(Patch patch) {

    List<PatchOperation> operations = patch.getOperations();
    JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
    ArrayNode patchNode = nodeFactory.arrayNode();
    for (PatchOperation operation : operations) {
        ObjectNode opNode = nodeFactory.objectNode();
        opNode.set("op", nodeFactory.textNode(operation.getOp()));
        opNode.set("path", nodeFactory.textNode(operation.getPath()));
        if (operation instanceof FromOperation) {
            FromOperation fromOp = (FromOperation) operation;
            opNode.set("from", nodeFactory.textNode(fromOp.getFrom()));
        }
        Object value = operation.getValue();
        if (value != null) {
            opNode.set("value", MAPPER.valueToTree(value));
        }
        patchNode.add(opNode);
    }

    return patchNode;
}

From source file:com.axelor.web.MapRest.java

@Path("/geomap/turnover")
@GET/*from  w w w .  j ava 2  s.c om*/
@Produces(MediaType.APPLICATION_JSON)
public JsonNode getGeoMapData() {

    Map<String, BigDecimal> data = new HashMap<String, BigDecimal>();
    List<? extends SaleOrder> orders = saleOrderRepo.all().filter("self.statusSelect=?", 3).fetch();
    JsonNodeFactory factory = JsonNodeFactory.instance;
    ObjectNode mainNode = factory.objectNode();
    ArrayNode arrayNode = factory.arrayNode();

    ArrayNode labelNode = factory.arrayNode();
    labelNode.add("Country");
    labelNode.add("Turnover");
    arrayNode.add(labelNode);

    for (SaleOrder so : orders) {

        Country country = so.getMainInvoicingAddress().getAddressL7Country();
        BigDecimal value = so.getExTaxTotal();

        if (country != null) {
            String key = country.getName();

            if (data.containsKey(key)) {
                BigDecimal oldValue = data.get(key);
                oldValue = oldValue.add(value);
                data.put(key, oldValue);
            } else {
                data.put(key, value);
            }
        }
    }

    Iterator<String> keys = data.keySet().iterator();
    while (keys.hasNext()) {
        String key = keys.next();
        ArrayNode dataNode = factory.arrayNode();
        dataNode.add(key);
        dataNode.add(data.get(key));
        arrayNode.add(dataNode);
    }

    mainNode.put("status", 0);
    mainNode.put("data", arrayNode);
    return mainNode;
}

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

@Test
public void testDocModify() throws Exception {
    JsonNodeFactory factory = JsonNodeFactory.withExactBigDecimals(false);
    ObjectNode node = factory.objectNode();
    JsonDoc doc = new JsonDoc(node);
    node.put("fld1", "value").put("arr#", 1);
    node.set("arr", factory.arrayNode().add(1).add(2));
    node.set("arr2", factory.arrayNode().add(1).add(2).add(3));
    ObjectNode obj = factory.objectNode();
    obj.put("fld3", "val");
    obj.set("arr3", factory.arrayNode().add(1).add(2).add(3).add(4));
    node.set("obj", obj);

    PredefinedFields.updateArraySizes(factory, node);

    Assert.assertEquals(2, doc.get(new Path("arr#")).intValue());
    Assert.assertEquals(3, doc.get(new Path("arr2#")).intValue());
    Assert.assertEquals(4, doc.get(new Path("obj.arr3#")).intValue());
}

From source file:org.apache.streams.rss.serializer.SyndEntrySerializer.java

private void serializeListOfStrings(List toSerialize, String key, ObjectNode node, JsonNodeFactory factory) {
    if (toSerialize == null || toSerialize.size() == 0)
        return;/*from w  w  w .j  a va2  s .  c  o  m*/
    ArrayNode keyNode = factory.arrayNode();
    for (Object obj : toSerialize) {
        if (obj instanceof String) {
            keyNode.add((String) obj);
        } else {
            LOGGER.debug("Array at Key:{} was expecting item types of String. Received class : {}", key,
                    obj.getClass().toString());
        }
    }
    node.put(key, keyNode);
}

From source file:org.apache.streams.rss.serializer.SyndEntrySerializer.java

private void serializeModules(ObjectNode root, JsonNodeFactory factory, List modules) {
    if (modules == null || modules.size() == 0)
        return;//from   w w  w.j av a  2s  .  co m
    ArrayNode modulesArray = factory.arrayNode();
    for (Object obj : modules) {
        if (obj instanceof Module) {
            Module mod = (Module) obj;
            if (mod.getUri() != null)
                modulesArray.add(mod.getUri());
        } else {
            LOGGER.debug(
                    "SyndEntry.getModules() items are not of type Module. Need to handle the case of class : {}",
                    obj.getClass().toString());
        }
    }
    root.put("modules", modulesArray);
}

From source file:io.fabric8.kubernetes.api.KubernetesHelper.java

protected static JsonNode findOrCreateConfig(Object[] objects) {
    for (Object object : objects) {
        if (object instanceof JsonNode) {
            JsonNode jsonNode = (JsonNode) object;
            JsonNode items = jsonNode.get("items");
            if (items != null && items.isArray()) {
                return jsonNode;
            }//from   w  ww  . j a v  a2s.  c  o m
        }
    }

    // lets create a new list
    JsonNodeFactory factory = createNodeFactory();
    ObjectNode config = factory.objectNode();
    config.set("apiVersion", factory.textNode("v1beta2"));
    config.set("kind", factory.textNode("List"));
    config.set("items", factory.arrayNode());
    return config;
}