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

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

Introduction

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

Prototype

protected JsonNodeFactory(boolean paramBoolean) 

Source Link

Usage

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  ww.  ja  v  a  2  s  .co 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:org.teavm.flavour.json.test.TeaVMJSONRunner.java

public static JsonNode serialize(Object value) {
    return convert(new JsonNodeFactory(false), JSON.serialize(value));
}

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

public static JsonNode convertFromSimpleRecord(Schema schema, JsonNode json) throws IOException {
    return convertFromSimpleRecord(schema, json, new JsonNodeFactory(false));
}

From source file:com.redhat.lightblue.metadata.types.IntegerTypeTest.java

@Test
public void testToJson() {
    JsonNodeFactory jsonNodeFactory = new JsonNodeFactory(true);
    JsonNode jsonNode = integerType.toJson(jsonNodeFactory, Integer.MAX_VALUE);
    assertTrue(new Integer(jsonNode.asText()).equals(Integer.MAX_VALUE));
}

From source file:com.redhat.lightblue.metadata.types.StringTypeTest.java

@Test
public void testToJson() {
    JsonNodeFactory jsonNodeFactory = new JsonNodeFactory(true);
    JsonNode jsonNode = stringType.toJson(jsonNodeFactory, "json");
    assertTrue(new Boolean(jsonNode.asText().equals("json")));
}

From source file:com.redhat.lightblue.metadata.types.BooleanTypeTest.java

@Test
public void testToJson() {
    JsonNodeFactory jsonNodeFactory = new JsonNodeFactory(true);
    JsonNode jsonNode = booleanType.toJson(jsonNodeFactory, Boolean.TRUE);
    assertTrue(new Boolean(jsonNode.asBoolean()).equals(Boolean.TRUE));
}

From source file:com.redhat.lightblue.metadata.types.DoubleTypeTest.java

@Test
public void testToJson() {
    JsonNodeFactory jsonNodeFactory = new JsonNodeFactory(true);
    JsonNode jsonNode = doubleType.toJson(jsonNodeFactory, Double.MAX_VALUE);
    assertTrue(new Double(jsonNode.asDouble()).equals(Double.MAX_VALUE));
}

From source file:com.redhat.lightblue.metadata.types.BigIntegerTypeTest.java

@Test
public void testToJson() {
    JsonNodeFactory jsonNodeFactory = new JsonNodeFactory(true);
    JsonNode jsonNode = bigIntegerType.toJson(jsonNodeFactory, BigInteger.ZERO);
    assertTrue(new BigInteger(jsonNode.asText()).equals(BigInteger.ZERO));
}

From source file:com.redhat.lightblue.metadata.types.BigDecimalTypeTest.java

@Test
public void testToJson() {
    JsonNodeFactory jsonNodeFactory = new JsonNodeFactory(true);
    JsonNode jsonNode = bigDecimalType.toJson(jsonNodeFactory, Double.MAX_VALUE);
    assertTrue(new Double(jsonNode.asDouble()).equals(Double.MAX_VALUE));
}

From source file:edu.usd.btl.REST.CategoriesResource.java

@Path("/test")
@GET//from w  w w.j  a va 2s.  co m
@Produces("application/json")
public String getTestJson() throws Exception {
    // Create the node factory that gives us nodes.
    JsonNodeFactory factory = new JsonNodeFactory(false);

    // create a json factory to write the treenode as json. for the example
    // we just write to console
    JsonFactory jsonFactory = new JsonFactory();
    JsonGenerator generator = jsonFactory.createGenerator(System.out);
    ObjectMapper mapper = new ObjectMapper();

    // the root node - album
    JsonNode album = factory.objectNode();
    mapper.writeTree(generator, album);

    return "null";
}