1
/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
* JSON Library, originally from http://jsoncpp.sourceforge.net/
5
* Copyright (C) 2011 Stewart Smith
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions are
12
* * Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
15
* * Redistributions in binary form must reproduce the above
16
* copyright notice, this list of conditions and the following disclaimer
17
* in the documentation and/or other materials provided with the
20
* * The names of its contributors may not be used to endorse or
21
* promote products derived from this software without specific prior
24
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
#ifndef JSON_WRITER_H_INCLUDED
40
# define JSON_WRITER_H_INCLUDED
51
/** \brief Abstract class for writers.
58
virtual std::string write( const Value &root ) = 0;
61
/** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format without formatting (not human friendly).
63
* The JSON document is written in a single line. It is not intended for 'human' consumption,
64
* but may be usefull to support feature such as RPC where bandwith is limited.
67
class JSON_API FastWriter : public Writer
71
virtual ~FastWriter(){}
73
void enableYAMLCompatibility();
75
public: // overridden from Writer
76
virtual std::string write( const Value &root );
79
void writeValue( const Value &value );
81
std::string document_;
82
bool yamlCompatiblityEnabled_;
85
/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way.
87
* The rules for line break and indent are as follow:
89
* - if empty then print {} without indent and line break
90
* - if not empty the print '{', line break & indent, print one value per line
91
* and then unindent and line break and print '}'.
93
* - if empty then print [] without indent and line break
94
* - if the array contains no object value, empty array or some other value types,
95
* and all the values fit on one lines, then print the array on a single line.
96
* - otherwise, it the values do not fit on one line, or the array contains
97
* object or non empty array, then print one value per line.
99
* If the Value have comments then they are outputed according to their #CommentPlacement.
101
* \sa Reader, Value, Value::setComment()
103
class JSON_API StyledWriter: public Writer
107
virtual ~StyledWriter(){}
109
public: // overridden from Writer
110
/** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
111
* \param root Value to serialize.
112
* \return String containing the JSON document that represents the root value.
114
virtual std::string write( const Value &root );
117
void writeValue( const Value &value );
118
void writeArrayValue( const Value &value );
119
bool isMultineArray( const Value &value );
120
void pushValue( const std::string &value );
122
void writeWithIndent( const std::string &value );
125
void writeCommentBeforeValue( const Value &root );
126
void writeCommentAfterValueOnSameLine( const Value &root );
127
bool hasCommentForValue( const Value &value );
128
static std::string normalizeEOL( const std::string &text );
130
typedef std::vector<std::string> ChildValues;
132
ChildValues childValues_;
133
std::string document_;
134
std::string indentString_;
137
bool addChildValues_;
140
/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way,
141
to a stream rather than to a string.
143
* The rules for line break and indent are as follow:
145
* - if empty then print {} without indent and line break
146
* - if not empty the print '{', line break & indent, print one value per line
147
* and then unindent and line break and print '}'.
149
* - if empty then print [] without indent and line break
150
* - if the array contains no object value, empty array or some other value types,
151
* and all the values fit on one lines, then print the array on a single line.
152
* - otherwise, it the values do not fit on one line, or the array contains
153
* object or non empty array, then print one value per line.
155
* If the Value have comments then they are outputed according to their #CommentPlacement.
157
* \param indentation Each level will be indented by this amount extra.
158
* \sa Reader, Value, Value::setComment()
160
class JSON_API StyledStreamWriter
163
StyledStreamWriter( std::string indentation="\t" );
164
~StyledStreamWriter(){}
167
/** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
168
* \param out Stream to write to. (Can be ostringstream, e.g.)
169
* \param root Value to serialize.
170
* \note There is no point in deriving from Writer, since write() should not return a value.
172
void write( std::ostream &out, const Value &root );
175
void writeValue( const Value &value );
176
void writeArrayValue( const Value &value );
177
bool isMultineArray( const Value &value );
178
void pushValue( const std::string &value );
180
void writeWithIndent( const std::string &value );
183
void writeCommentBeforeValue( const Value &root );
184
void writeCommentAfterValueOnSameLine( const Value &root );
185
bool hasCommentForValue( const Value &value );
186
static std::string normalizeEOL( const std::string &text );
188
typedef std::vector<std::string> ChildValues;
190
ChildValues childValues_;
191
std::ostream* document_;
192
std::string indentString_;
194
std::string indentation_;
195
bool addChildValues_;
198
std::string JSON_API valueToString( Int value );
199
std::string JSON_API valueToString( UInt value );
200
std::string JSON_API valueToString( double value );
201
std::string JSON_API valueToString( bool value );
202
std::string JSON_API valueToQuotedString( const char *value );
204
/// \brief Output using the StyledStreamWriter.
205
/// \see Json::operator>>()
206
std::ostream& operator<<( std::ostream&, const Value &root );
212
#endif // JSON_WRITER_H_INCLUDED