1 // Copyright 2007-2010 Baptiste Lepilleur
2 // Distributed under MIT license, or public domain if desired and
3 // recognized in your jurisdiction.
4 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
6 #ifndef JSON_WRITER_H_INCLUDED
7 # define JSON_WRITER_H_INCLUDED
9 # include "json_value.h"
18 /** \brief Abstract class for writers.
25 virtual std::string write( const Value &root ) = 0;
28 /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format without formatting (not human friendly).
30 * The JSON document is written in a single line. It is not intended for 'human' consumption,
31 * but may be usefull to support feature such as RPC where bandwith is limited.
34 class JSON_API FastWriter : public Writer
38 virtual ~FastWriter(){}
40 void enableYAMLCompatibility();
42 public: // overridden from Writer
43 virtual std::string write( const Value &root );
46 void writeValue( const Value &value );
48 std::string document_;
49 bool yamlCompatiblityEnabled_;
52 /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way.
54 * The rules for line break and indent are as follow:
56 * - if empty then print {} without indent and line break
57 * - if not empty the print '{', line break & indent, print one value per line
58 * and then unindent and line break and print '}'.
60 * - if empty then print [] without indent and line break
61 * - if the array contains no object value, empty array or some other value types,
62 * and all the values fit on one lines, then print the array on a single line.
63 * - otherwise, it the values do not fit on one line, or the array contains
64 * object or non empty array, then print one value per line.
66 * If the Value have comments then they are outputed according to their #CommentPlacement.
68 * \sa Reader, Value, Value::setComment()
70 class JSON_API StyledWriter: public Writer
74 virtual ~StyledWriter(){}
76 public: // overridden from Writer
77 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
78 * \param root Value to serialize.
79 * \return String containing the JSON document that represents the root value.
81 virtual std::string write( const Value &root );
84 void writeValue( const Value &value );
85 void writeArrayValue( const Value &value );
86 bool isMultineArray( const Value &value );
87 void pushValue( const std::string &value );
89 void writeWithIndent( const std::string &value );
92 void writeCommentBeforeValue( const Value &root );
93 void writeCommentAfterValueOnSameLine( const Value &root );
94 bool hasCommentForValue( const Value &value );
95 static std::string normalizeEOL( const std::string &text );
97 typedef std::vector<std::string> ChildValues;
99 ChildValues childValues_;
100 std::string document_;
101 std::string indentString_;
104 bool addChildValues_;
107 /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way,
108 to a stream rather than to a string.
110 * The rules for line break and indent are as follow:
112 * - if empty then print {} without indent and line break
113 * - if not empty the print '{', line break & indent, print one value per line
114 * and then unindent and line break and print '}'.
116 * - if empty then print [] without indent and line break
117 * - if the array contains no object value, empty array or some other value types,
118 * and all the values fit on one lines, then print the array on a single line.
119 * - otherwise, it the values do not fit on one line, or the array contains
120 * object or non empty array, then print one value per line.
122 * If the Value have comments then they are outputed according to their #CommentPlacement.
124 * \param indentation Each level will be indented by this amount extra.
125 * \sa Reader, Value, Value::setComment()
127 class JSON_API StyledStreamWriter
130 StyledStreamWriter( std::string indentation="\t" );
131 ~StyledStreamWriter(){}
134 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
135 * \param out Stream to write to. (Can be ostringstream, e.g.)
136 * \param root Value to serialize.
137 * \note There is no point in deriving from Writer, since write() should not return a value.
139 void write( std::ostream &out, const Value &root );
142 void writeValue( const Value &value );
143 void writeArrayValue( const Value &value );
144 bool isMultineArray( const Value &value );
145 void pushValue( const std::string &value );
147 void writeWithIndent( const std::string &value );
150 void writeCommentBeforeValue( const Value &root );
151 void writeCommentAfterValueOnSameLine( const Value &root );
152 bool hasCommentForValue( const Value &value );
153 static std::string normalizeEOL( const std::string &text );
155 typedef std::vector<std::string> ChildValues;
157 ChildValues childValues_;
158 std::ostream* document_;
159 std::string indentString_;
161 std::string indentation_;
162 bool addChildValues_;
165 std::string JSON_API valueToString( Int value );
166 std::string JSON_API valueToString( UInt value );
167 std::string JSON_API valueToString( double value );
168 std::string JSON_API valueToString( bool value );
169 std::string JSON_API valueToQuotedString( const char *value );
171 /// \brief Output using the StyledStreamWriter.
172 /// \see Json::operator>>()
173 std::ostream& operator<<( std::ostream&, const Value &root );
179 #endif // JSON_WRITER_H_INCLUDED