1 #ifndef JSON_WRITER_H_INCLUDED
2 # define JSON_WRITER_H_INCLUDED
13 /** \brief Abstract class for writers.
20 virtual std::string
write( const Value
&root
) = 0;
23 /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format without formatting (not human friendly).
25 * The JSON document is written in a single line. It is not intended for 'human' consumption,
26 * but may be useful to support feature such as RPC where bandwidth is limited.
29 class JSON_API FastWriter
: public Writer
33 virtual ~FastWriter(){}
35 void enableYAMLCompatibility();
37 public: // overridden from Writer
38 virtual std::string
write( const Value
&root
);
41 void writeValue( const Value
&value
);
43 std::string document_
;
44 bool yamlCompatiblityEnabled_
;
47 /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way.
49 * The rules for line break and indent are as follow:
51 * - if empty then print {} without indent and line break
52 * - if not empty the print '{', line break & indent, print one value per line
53 * and then unindent and line break and print '}'.
55 * - if empty then print [] without indent and line break
56 * - if the array contains no object value, empty array or some other value types,
57 * and all the values fit on one lines, then print the array on a single line.
58 * - otherwise, it the values do not fit on one line, or the array contains
59 * object or non empty array, then print one value per line.
61 * If the Value have comments then they are outputed according to their #CommentPlacement.
63 * \sa Reader, Value, Value::setComment()
65 class JSON_API StyledWriter
: public Writer
69 virtual ~StyledWriter(){}
71 public: // overridden from Writer
72 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
73 * \param root Value to serialize.
74 * \return String containing the JSON document that represents the root value.
76 virtual std::string
write( const Value
&root
);
79 void writeValue( const Value
&value
);
80 void writeArrayValue( const Value
&value
);
81 bool isMultineArray( const Value
&value
);
82 void pushValue( const std::string
&value
);
84 void writeWithIndent( const std::string
&value
);
87 void writeCommentBeforeValue( const Value
&root
);
88 void writeCommentAfterValueOnSameLine( const Value
&root
);
89 bool hasCommentForValue( const Value
&value
);
90 static std::string
normalizeEOL( const std::string
&text
);
92 typedef std::vector
<std::string
> ChildValues
;
94 ChildValues childValues_
;
95 std::string document_
;
96 std::string indentString_
;
102 /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way,
103 to a stream rather than to a string.
105 * The rules for line break and indent are as follow:
107 * - if empty then print {} without indent and line break
108 * - if not empty the print '{', line break & indent, print one value per line
109 * and then unindent and line break and print '}'.
111 * - if empty then print [] without indent and line break
112 * - if the array contains no object value, empty array or some other value types,
113 * and all the values fit on one lines, then print the array on a single line.
114 * - otherwise, it the values do not fit on one line, or the array contains
115 * object or non empty array, then print one value per line.
117 * If the Value have comments then they are outputed according to their #CommentPlacement.
119 * \sa Reader, Value, Value::setComment()
121 class JSON_API StyledStreamWriter
124 StyledStreamWriter( std::string indentation
="\t" );
125 ~StyledStreamWriter(){}
128 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
129 * \param out Stream to write to. (Can be ostringstream, e.g.)
130 * \param root Value to serialize.
131 * \note There is no point in deriving from Writer, since write() should not return a value.
133 void write( std::ostream
&out
, const Value
&root
);
136 void writeValue( const Value
&value
);
137 void writeArrayValue( const Value
&value
);
138 bool isMultineArray( const Value
&value
);
139 void pushValue( const std::string
&value
);
141 void writeWithIndent( const std::string
&value
);
144 void writeCommentBeforeValue( const Value
&root
);
145 void writeCommentAfterValueOnSameLine( const Value
&root
);
146 bool hasCommentForValue( const Value
&value
);
147 static std::string
normalizeEOL( const std::string
&text
);
149 typedef std::vector
<std::string
> ChildValues
;
151 ChildValues childValues_
;
152 std::ostream
* document_
;
153 std::string indentString_
;
155 std::string indentation_
;
156 bool addChildValues_
;
159 std::string JSON_API
valueToString( Int value
);
160 std::string JSON_API
valueToString( UInt value
);
161 std::string JSON_API
valueToString( double value
);
162 std::string JSON_API
valueToString( bool value
);
163 std::string JSON_API
valueToQuotedString( const char *value
);
165 /// \brief Output using the StyledStreamWriter.
166 /// \see Json::operator>>()
167 std::ostream
& operator<<( std::ostream
&, const Value
&root
);
173 #endif // JSON_WRITER_H_INCLUDED