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

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

Introduction

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

Prototype

public ArrayNode add(JsonNode paramJsonNode) 

Source Link

Usage

From source file:org.bimserver.javamodelchecker.WriteToJson.java

public static void main(String[] args) {
    try {// w w w .  ja  va  2 s.co m
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode rootNode = objectMapper.createObjectNode();
        ArrayNode array = objectMapper.createArrayNode();
        rootNode.set("modelcheckers", array);

        ObjectNode objectNode = objectMapper.createObjectNode();
        array.add(objectNode);
        objectNode.put("name", "Check window widths");
        objectNode.put("description", "Check window widths");
        objectNode.put("modelCheckerPluginClassName", "org.bimserver.javamodelchecker.JavaModelCheckerPlugin");
        objectNode.put("code",
                changeClassName(
                        FileUtils.readFileToString(
                                new File("src/org/bimserver/javamodelchecker/WindowWidthChecker.java")),
                        "WindowWidthChecker"));

        objectNode = objectMapper.createObjectNode();
        array.add(objectNode);
        objectNode.put("name", "Pass always");
        objectNode.put("description", "Pass always");
        objectNode.put("modelCheckerPluginClassName", "org.bimserver.javamodelchecker.JavaModelCheckerPlugin");
        objectNode.put("code", changeClassName(
                FileUtils.readFileToString(new File("src/org/bimserver/javamodelchecker/PassAlways.java")),
                "PassAlways"));

        objectNode = objectMapper.createObjectNode();
        array.add(objectNode);
        objectNode.put("name", "Fail always");
        objectNode.put("description", "Fail always");
        objectNode.put("modelCheckerPluginClassName", "org.bimserver.javamodelchecker.JavaModelCheckerPlugin");
        objectNode.put("code", changeClassName(
                FileUtils.readFileToString(new File("src/org/bimserver/javamodelchecker/FailAlways.java")),
                "FailAlways"));

        objectMapper.writerWithDefaultPrettyPrinter().writeValue(new File("modelcheckers.json"), rootNode);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:squash.tools.FakeBookingCreator.java

public static void main(String[] args) throws IOException {
    int numberOfDays = 21;
    int numberOfCourts = 5;
    int maxCourtSpan = 5;
    int numberOfSlots = 16;
    int maxSlotSpan = 3;
    int minSurnameLength = 2;
    int maxSurnameLength = 20;
    int minBookingsPerDay = 0;
    int maxBookingsPerDay = 8;
    LocalDate startDate = LocalDate.of(2016, 7, 5);

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    List<Booking> bookings = new ArrayList<>();
    for (LocalDate date = startDate; date.isBefore(startDate.plusDays(numberOfDays)); date = date.plusDays(1)) {
        int numBookings = ThreadLocalRandom.current().nextInt(minBookingsPerDay, maxBookingsPerDay + 1);
        List<Booking> daysBookings = new ArrayList<>();
        for (int bookingIndex = 0; bookingIndex < numBookings; bookingIndex++) {
            String player1 = RandomStringUtils.randomAlphabetic(1) + "." + RandomStringUtils.randomAlphabetic(
                    ThreadLocalRandom.current().nextInt(minSurnameLength, maxSurnameLength + 1));
            String player2 = RandomStringUtils.randomAlphabetic(1) + "." + RandomStringUtils.randomAlphabetic(
                    ThreadLocalRandom.current().nextInt(minSurnameLength, maxSurnameLength + 1));

            Set<ImmutablePair<Integer, Integer>> bookedCourts = new HashSet<>();
            daysBookings.forEach((booking) -> {
                addBookingToSet(booking, bookedCourts);
            });/* w w w  .jav  a  2 s .  c o  m*/

            Booking booking;
            Set<ImmutablePair<Integer, Integer>> courtsToBook = new HashSet<>();
            do {
                // Loop until we create a booking of free courts
                int court = ThreadLocalRandom.current().nextInt(1, numberOfCourts + 1);
                int courtSpan = ThreadLocalRandom.current().nextInt(1,
                        Math.min(maxCourtSpan + 1, numberOfCourts - court + 2));
                int slot = ThreadLocalRandom.current().nextInt(1, numberOfSlots + 1);
                int slotSpan = ThreadLocalRandom.current().nextInt(1,
                        Math.min(maxSlotSpan + 1, numberOfSlots - slot + 2));
                booking = new Booking(court, courtSpan, slot, slotSpan, player1 + "/" + player2);
                booking.setDate(date.format(formatter));
                courtsToBook.clear();
                addBookingToSet(booking, courtsToBook);
            } while (Boolean.valueOf(Sets.intersection(courtsToBook, bookedCourts).size() > 0));

            daysBookings.add(booking);
        }
        bookings.addAll(daysBookings);
    }

    // Encode bookings as JSON
    // Create the node factory that gives us nodes.
    JsonNodeFactory factory = new JsonNodeFactory(false);
    // Create a json factory to write the treenode as json.
    JsonFactory jsonFactory = new JsonFactory();
    ObjectNode rootNode = factory.objectNode();

    ArrayNode bookingsNode = rootNode.putArray("bookings");
    for (int i = 0; i < bookings.size(); i++) {
        Booking booking = bookings.get(i);
        ObjectNode bookingNode = factory.objectNode();
        bookingNode.put("court", booking.getCourt());
        bookingNode.put("courtSpan", booking.getCourtSpan());
        bookingNode.put("slot", booking.getSlot());
        bookingNode.put("slotSpan", booking.getSlotSpan());
        bookingNode.put("name", booking.getName());
        bookingNode.put("date", booking.getDate());
        bookingsNode.add(bookingNode);
    }
    // Add empty booking rules array - just so restore works
    rootNode.putArray("bookingRules");
    rootNode.put("clearBeforeRestore", true);

    try (JsonGenerator generator = jsonFactory.createGenerator(new File("FakeBookings.json"),
            JsonEncoding.UTF8)) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(Include.NON_EMPTY);
        mapper.setSerializationInclusion(Include.NON_NULL);
        mapper.writeTree(generator, rootNode);
    }
}

From source file:com.ibm.watson.catalyst.corpus.util.JsonUtil.java

public static ArrayNode toArrayNodeString(Collection<String> aCollection) {
    ArrayNode result = MAPPER.createArrayNode();
    aCollection.forEach((aString) -> result.add(aString));
    return result;
}

From source file:com.ibm.watson.catalyst.corpus.util.JsonUtil.java

public static ArrayNode toArrayNodeJsonable(Collection<? extends Jsonable> aCollection) {
    ArrayNode result = MAPPER.createArrayNode();
    aCollection.forEach((aJsonable) -> result.add(aJsonable.toJson()));
    return result;
}

From source file:org.opendaylight.nic.bgp.service.parser.BgpDataflowParser.java

public static String fromBgpDataFlow(final BgpDataflow bgpDataflow) {
    final ObjectMapper objectMapper = createObjectMapper();
    final ObjectNode ipv4NextHopNode = objectMapper.createObjectNode();
    final ObjectNode originNode = objectMapper.createObjectNode();
    final ObjectNode localPrefNode = objectMapper.createObjectNode();
    final ObjectNode asPathNode = objectMapper.createObjectNode();

    final ObjectNode attributesNode = objectMapper.createObjectNode();

    ipv4NextHopNode.put(GLOBAL, bgpDataflow.getGlobalIp().getValue());
    attributesNode.put(IPV4_NEXT_HOP, ipv4NextHopNode);
    attributesNode.put(AS_PATH, asPathNode);
    originNode.put(VALUE, ORIGIN_IGP);/*from   w w  w. j  a  va2  s  .co m*/
    attributesNode.put(ORIGIN, originNode);
    localPrefNode.put(PREF, FIXED_PREF);
    attributesNode.put(LOCAL_PREF, localPrefNode);

    final ObjectNode ipv4RouteAttributesNode = objectMapper.createObjectNode();
    ipv4RouteAttributesNode.put(PREFIX, bgpDataflow.getPrefix().getValue());
    ipv4RouteAttributesNode.put(PATH_ID, bgpDataflow.getPathId());
    ipv4RouteAttributesNode.put(ATTRIBUTES, attributesNode);

    final ArrayNode arrayNode = objectMapper.createArrayNode();
    arrayNode.add(ipv4RouteAttributesNode);

    final ObjectNode ipv4RouteNode = objectMapper.createObjectNode();
    ipv4RouteNode.put(IPV4_ROUTE, arrayNode);

    final ObjectNode bgpInetIpv4Routes = objectMapper.createObjectNode();
    bgpInetIpv4Routes.put(BGP_INET_IPV4_ROUTES, ipv4RouteNode);

    return bgpInetIpv4Routes.toString();
}

From source file:neo4play.Neo4j.java

protected static JsonNode buildStatements(String query, JsonNode properties) {
    ObjectNode statement = Json.newObject();
    statement.put("statement", query);
    ObjectNode parameters = Json.newObject();
    parameters.put("props", properties);
    statement.put("parameters", parameters);
    ArrayNode statementList = JsonNodeFactory.instance.arrayNode();
    statementList.add(statement);
    ObjectNode statements = Json.newObject();
    statements.put("statements", statementList);
    return statements;
}

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

private static ArrayNode getArrayNode(List<String> args) {
    ArrayNode countryNode = JsonNodeFactory.instance.arrayNode();
    for (String arg : args) {
        countryNode.add(arg);
    }//  ww  w  .ja v a  2  s .com
    return countryNode;
}

From source file:com.github.fge.jsonschema.keyword.validator.AbstractKeywordValidator.java

protected static <T> JsonNode toArrayNode(final Collection<T> collection) {
    final ArrayNode node = JacksonUtils.nodeFactory().arrayNode();
    for (final T element : collection)
        node.add(element.toString());
    return node;//from  ww w . ja v a2s .com
}

From source file:com.baasbox.service.query.JsonTree.java

public static JsonNode write(JsonNode json, PartsParser pp, JsonNode data) throws MissingNodeException {
    JsonNode root = json;// www .j  a  v  a  2  s  .  c  om
    for (Part p : pp.parts()) {
        if (p.equals(pp.last())) {
            break;
        }
        if (p instanceof PartsLexer.ArrayField) {
            int index = ((PartsLexer.ArrayField) p).arrayIndex;
            root = root.path(p.getName()).path(index);
        } else if (p instanceof PartsLexer.Field) {
            root = root.path(p.getName());
        }
    }
    if (root.isMissingNode()) {
        throw new MissingNodeException(pp.treeFields());
    }
    Part last = pp.last();

    if (last instanceof PartsLexer.ArrayField) {
        PartsLexer.ArrayField arr = (PartsLexer.ArrayField) last;
        int index = arr.arrayIndex;
        root = root.path(last.getName());
        ArrayNode arrNode = (ArrayNode) root;
        if (arrNode.size() <= index) {
            arrNode.add(data);
        } else {
            arrNode.set(index, data);
        }
        return root;

    } else {
        try {
            ((ObjectNode) root).put(last.getName(), data);
        } catch (Exception e) {
            throw new MissingNodeException(pp.treeFields());
        }
        return root.get(last.getName());
    }

}

From source file:utils.ReflectionService.java

public static JsonNode createJsonNode(Class clazz, int maxDeep) {

    if (maxDeep <= 0 || JsonNode.class.isAssignableFrom(clazz)) {
        return null;
    }//from   w  w w  .  ja  v a  2  s . co  m

    ObjectMapper objectMapper = new ObjectMapper();
    ObjectNode objectNode = objectMapper.createObjectNode();

    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        String key = field.getName();
        try {
            // is array or collection
            if (field.getType().isArray() || Collection.class.isAssignableFrom(field.getType())) {
                ParameterizedType collectionType = (ParameterizedType) field.getGenericType();
                Class<?> type = (Class<?>) collectionType.getActualTypeArguments()[0];
                ArrayNode arrayNode = objectMapper.createArrayNode();
                arrayNode.add(createJsonNode(type, maxDeep - 1));
                objectNode.put(key, arrayNode);
            } else {
                Class<?> type = field.getType();
                if (Modifier.isStatic(field.getModifiers())) {
                    continue;
                } else if (type.isEnum()) {
                    objectNode.put(key, Enum.class.getName());
                } else if (!type.getName().startsWith("java") && !key.startsWith("_")) {
                    objectNode.put(key, createJsonNode(type, maxDeep - 1));
                } else {
                    objectNode.put(key, type.getName());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return objectNode;
}