JsonCpp project page | JsonCpp home page

writer.h
Go to the documentation of this file.
1 // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
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
5
6 #ifndef JSON_WRITER_H_INCLUDED
7 #define JSON_WRITER_H_INCLUDED
8
9 #if !defined(JSON_IS_AMALGAMATION)
10 #include "value.h"
11 #endif // if !defined(JSON_IS_AMALGAMATION)
12 #include <vector>
13 #include <string>
14 #include <ostream>
15
16 // Disable warning C4251: <data member>: <type> needs to have dll-interface to
17 // be used by...
18 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
19 #pragma warning(push)
20 #pragma warning(disable : 4251)
21 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
22
23 #pragma pack(push, 8)
24
25 namespace Json {
26
27 class Value;
28
42 class JSON_API StreamWriter {
43 protected:
44 JSONCPP_OSTREAM* sout_; // not owned; will not delete
45 public:
46 StreamWriter();
47 virtual ~StreamWriter();
54 virtual int write(Value const& root, JSONCPP_OSTREAM* sout) = 0;
55
58 class JSON_API Factory {
59 public:
60 virtual ~Factory();
64 virtual StreamWriter* newStreamWriter() const = 0;
65 }; // Factory
66 }; // StreamWriter
67
71 JSONCPP_STRING JSON_API writeString(StreamWriter::Factory const& factory, Value const& root);
72
73
89 class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
90 public:
91 // Note: We use a Json::Value so that we can add data-members to this class
92 // without a major version bump.
114 Json::Value settings_;
115
116 StreamWriterBuilder();
117 ~StreamWriterBuilder() JSONCPP_OVERRIDE;
118
122 StreamWriter* newStreamWriter() const JSONCPP_OVERRIDE;
123
127 bool validate(Json::Value* invalid) const;
130 Value& operator[](JSONCPP_STRING key);
131
137 static void setDefaults(Json::Value* settings);
138 };
139
143 class JSONCPP_DEPRECATED("Use StreamWriter instead") JSON_API Writer {
144 public:
145 virtual ~Writer();
146
147 virtual JSONCPP_STRING write(const Value& root) = 0;
148 };
149
159 class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API FastWriter : public Writer {
160
161 public:
162 FastWriter();
163 ~FastWriter() JSONCPP_OVERRIDE {}
164
165 void enableYAMLCompatibility();
166
172 void dropNullPlaceholders();
173
174 void omitEndingLineFeed();
175
176 public: // overridden from Writer
177 JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE;
178
179 private:
180 void writeValue(const Value& value);
181
182 JSONCPP_STRING document_;
183 bool yamlCompatiblityEnabled_;
184 bool dropNullPlaceholders_;
185 bool omitEndingLineFeed_;
186 };
187
212 class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API StyledWriter : public Writer {
213 public:
214 StyledWriter();
215 ~StyledWriter() JSONCPP_OVERRIDE {}
216
217 public: // overridden from Writer
222 JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE;
223
224 private:
225 void writeValue(const Value& value);
226 void writeArrayValue(const Value& value);
227 bool isMultineArray(const Value& value);
228 void pushValue(const JSONCPP_STRING& value);
229 void writeIndent();
230 void writeWithIndent(const JSONCPP_STRING& value);
231 void indent();
232 void unindent();
233 void writeCommentBeforeValue(const Value& root);
234 void writeCommentAfterValueOnSameLine(const Value& root);
235 bool hasCommentForValue(const Value& value);
236 static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text);
237
238 typedef std::vector<JSONCPP_STRING> ChildValues;
239
240 ChildValues childValues_;
241 JSONCPP_STRING document_;
242 JSONCPP_STRING indentString_;
243 unsigned int rightMargin_;
244 unsigned int indentSize_;
245 bool addChildValues_;
246 };
247
273 class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API StyledStreamWriter {
274 public:
278 StyledStreamWriter(JSONCPP_STRING indentation = "\t");
279 ~StyledStreamWriter() {}
280
281 public:
288 void write(JSONCPP_OSTREAM& out, const Value& root);
289
290 private:
291 void writeValue(const Value& value);
292 void writeArrayValue(const Value& value);
293 bool isMultineArray(const Value& value);
294 void pushValue(const JSONCPP_STRING& value);
295 void writeIndent();
296 void writeWithIndent(const JSONCPP_STRING& value);
297 void indent();
298 void unindent();
299 void writeCommentBeforeValue(const Value& root);
300 void writeCommentAfterValueOnSameLine(const Value& root);
301 bool hasCommentForValue(const Value& value);
302 static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text);
303
304 typedef std::vector<JSONCPP_STRING> ChildValues;
305
306 ChildValues childValues_;
307 JSONCPP_OSTREAM* document_;
308 JSONCPP_STRING indentString_;
309 unsigned int rightMargin_;
310 JSONCPP_STRING indentation_;
311 bool addChildValues_ : 1;
312 bool indented_ : 1;
313 };
314
315 #if defined(JSON_HAS_INT64)
316 JSONCPP_STRING JSON_API valueToString(Int value);
317 JSONCPP_STRING JSON_API valueToString(UInt value);
318 #endif // if defined(JSON_HAS_INT64)
319 JSONCPP_STRING JSON_API valueToString(LargestInt value);
320 JSONCPP_STRING JSON_API valueToString(LargestUInt value);
321 JSONCPP_STRING JSON_API valueToString(double value);
322 JSONCPP_STRING JSON_API valueToString(bool value);
323 JSONCPP_STRING JSON_API valueToQuotedString(const char* value);
324
327 JSON_API JSONCPP_OSTREAM& operator<<(JSONCPP_OSTREAM&, const Value& root);
328
329 } // namespace Json
330
331 #pragma pack(pop)
332
333 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
334 #pragma warning(pop)
335 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
336
337 #endif // JSON_WRITER_H_INCLUDED
Json::FastWriter
Outputs a Value in JSON format without formatting (not human friendly).
Definition: writer.h:159
JSONCPP_OVERRIDE
#define JSONCPP_OVERRIDE
Definition: config.h:94
JSONCPP_DEPRECATED
#define JSONCPP_DEPRECATED(message)
Definition: config.h:135
Json::StreamWriter::Factory
A simple abstract factory.
Definition: writer.h:58
Json::LargestInt
Int64 LargestInt
Definition: config.h:168
Json::StyledWriter
Writes a Value in JSON format in a human friendly way.
Definition: writer.h:212
JSON_API
#define JSON_API
If defined, indicates that the source file is amalgated to prevent private header inclusion...
Definition: config.h:54
Json::valueToQuotedString
std::string valueToQuotedString(const char *value)
Definition: json_writer.cpp:179
JSONCPP_STRING
#define JSONCPP_STRING
Definition: config.h:179
Json::StreamWriter
Usage:
Definition: writer.h:42
Json::StyledStreamWriter::~StyledStreamWriter
~StyledStreamWriter()
Definition: writer.h:279
Json::StyledWriter::~StyledWriter
~StyledWriter()
Definition: writer.h:215
Json::FastWriter::~FastWriter
~FastWriter()
Definition: writer.h:163
Json::LargestUInt
UInt64 LargestUInt
Definition: config.h:169
Json::valueToString
std::string valueToString(Int value)
Definition: json_writer.cpp:128
JSONCPP_OSTREAM
#define JSONCPP_OSTREAM
Definition: config.h:181
Json
JSON (JavaScript Object Notation).
Definition: allocator.h:14
Json::StreamWriterBuilder::settings_
Json::Value settings_
Configuration of this builder.
Definition: writer.h:114
Json::Writer
Abstract class for writers.
Definition: writer.h:143
Json::Value
Represents a JSON value.
Definition: value.h:177
Json::StreamWriter::sout_
std::ostream * sout_
Definition: writer.h:44
Json::UInt
unsigned int UInt
Definition: config.h:154
Json::StyledStreamWriter
Writes a Value in JSON format in a human friendly way, to a stream rather than to a string...
Definition: writer.h:273
value.h
Json::writeString
std::string writeString(StreamWriter::Factory const &factory, Value const &root)
Write into stringstream, then return string, for convenience.
Definition: json_writer.cpp:1209
Json::Int
int Int
Definition: config.h:153
Json::operator<<
std::ostream & operator<<(std::ostream &, const Value &root)
Output using the StyledStreamWriter.
Definition: json_writer.cpp:1216
Json::StreamWriterBuilder
Build a StreamWriter implementation.
Definition: writer.h:89