Example usage for com.fasterxml.jackson.core JsonFactory createGenerator

List of usage examples for com.fasterxml.jackson.core JsonFactory createGenerator

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonFactory createGenerator.

Prototype

public JsonGenerator createGenerator(Writer out) throws IOException 

Source Link

Document

Method for constructing JSON generator for writing JSON content using specified Writer.

Usage

From source file:squash.booking.lambdas.core.PageManager.java

/**
 * Returns JSON-encoded booking data for a specified date.
 * /* www.j  ava2s.c om*/
 * <p>This is not private only so that it can be unit-tested.
 * 
 * @param date the date in YYYY-MM-DD format.
 * @param validDates the dates for which bookings can be made, in YYYY-MM-DD format.
 * @param bookings the bookings for the specified date.
 * @throws Exception 
 */
protected String createCachedBookingData(String date, List<String> validDates, List<Booking> bookings)
        throws Exception {

    ImmutablePair<LifecycleState, Optional<String>> lifecycleState = lifecycleManager.getLifecycleState();

    // N.B. we assume that the date is known to be a valid date
    logger.log("About to create cached booking data");
    logger.log("Lifecycle state is: " + lifecycleState.left.name());
    if (lifecycleState.left.equals(LifecycleState.RETIRED)) {
        logger.log("Lifecycle state forwarding url is: " + lifecycleState.right.get());
    }

    // 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();

    rootNode.put("date", date);
    ArrayNode validDatesNode = rootNode.putArray("validdates");
    for (int i = 0; i < validDates.size(); i++) {
        validDatesNode.add(validDates.get(i));
    }
    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());
        bookingsNode.add(bookingNode);
    }
    // This gives the Angularjs app access to the lifecycle state.
    ObjectNode lifecycleStateNode = rootNode.putObject("lifecycleState");
    lifecycleStateNode.put("state", lifecycleState.left.name());
    lifecycleStateNode.put("url", lifecycleState.right.isPresent() ? lifecycleState.right.get() : "");

    ByteArrayOutputStream bookingDataStream = new ByteArrayOutputStream();
    PrintStream printStream = new PrintStream(bookingDataStream);
    try (JsonGenerator generator = jsonFactory.createGenerator(printStream)) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeTree(generator, rootNode);
    }
    String bookingData = bookingDataStream.toString(StandardCharsets.UTF_8.name());
    logger.log("Created cached booking data: " + bookingData);

    return bookingData;
}