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