    001// Copyright 2010, 2013 The Apache Software Foundation
    002//
    003// Licensed under the Apache License, Version 2.0 (the "License");
    004// you may not use this file except in compliance with the License.
    005// You may obtain a copy of the License at
    006//
    007// http://www.apache.org/licenses/LICENSE-2.0
    008//
    009// Unless required by applicable law or agreed to in writing, software
    010// distributed under the License is distributed on an "AS IS" BASIS,
    011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012// See the License for the specific language governing permissions and
    013// limitations under the License.
    014
    015package org.apache.tapestry5.json;
    016
    017import java.io.CharArrayWriter;
    018import java.io.PrintWriter;
    019import java.io.Serializable;
    020
    021/**
    022 * Base class for {@link JSONArray} and {@link JSONObject} that exists to organize the code
    023 * for printing such objects (either compact or pretty).
    024 *
    025 * @since 5.2.0
    026 */
    027public abstract class JSONCollection implements Serializable
    028{
    029    /**
    030     * Converts this JSON collection into a parsable string representation.
    031     * <p/>
    032     * Warning: This method assumes that the data structure is acyclical.
    033     * <p>
    034     * Starting in release 5.2, the result will be pretty printed for readability.
    035     *
    036     * @return a printable, displayable, portable, transmittable representation of the object, beginning with
    037     *         <code>{</code>&nbsp;<small>(left brace)</small> and ending with <code>}</code>&nbsp;<small>(right
    038     *         brace)</small>.
    039     */
    040    @Override
    041    public String toString()
    042    {
    043        CharArrayWriter caw = new CharArrayWriter();
    044        PrintWriter pw = new PrintWriter(caw);
    045
    046        JSONPrintSession session = new PrettyPrintSession(pw);
    047
    048        print(session);
    049
    050        pw.close();
    051
    052        return caw.toString();
    053    }
    054
    055    /**
    056     * Converts the JSONObject to a compact or pretty-print string representation
    057     *
    058     * @param compact
    059     *            if true, return minimal format string.
    060     * @since 5.2.0
    061     */
    062    public String toString(boolean compact)
    063    {
    064        return compact ? toCompactString() : toString();
    065    }
    066
    067    /**
    068     * Prints the JSONObject as a compact string (not extra punctuation). This is, essentially, what
    069     * Tapestry 5.1 did inside {@link #toString()}.
    070     */
    071    public String toCompactString()
    072    {
    073        CharArrayWriter caw = new CharArrayWriter();
    074        PrintWriter pw = new PrintWriter(caw);
    075
    076        print(pw);
    077
    078        pw.close();
    079
    080        return caw.toString();
    081    }
    082
    083    /**
    084     * Prints the JSONObject to the write (compactly or not).
    085     *
    086     * @param writer
    087     *            to write content to
    088     * @param compact
    089     *            if true, then write compactly, if false, write with pretty printing
    090     * @since 5.2.1
    091     */
    092    public void print(PrintWriter writer, boolean compact)
    093    {
    094        JSONPrintSession session = compact ? new CompactSession(writer) : new PrettyPrintSession(writer);
    095
    096        print(session);
    097    }
    098
    099    /**
    100     * Prints the JSONObject to the writer compactly (with no extra whitespace).
    101     */
    102    public void print(PrintWriter writer)
    103    {
    104        print(writer, true);
    105    }
    106
    107    /**
    108     * Prints the JSONObject to the writer using indentation (two spaces per indentation level).
    109     */
    110    public void prettyPrint(PrintWriter writer)
    111    {
    112        print(writer, false);
    113    }
    114
    115    /**
    116     * Print the collection in a parsable format using the session to (optionally) inject extra
    117     * whitespace (for "pretty printing").
    118     */
    119    abstract void print(JSONPrintSession session);
    120}