JsonCpp project page Classes Namespace 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) && defined(_MSC_VER)
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 
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 
72 
73 
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.
115 
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 
144 public:
145  virtual ~Writer();
146 
147  virtual JSONCPP_STRING write(const Value& root) = 0;
148 };
149 
159 #if defined(_MSC_VER)
160 #pragma warning(push)
161 #pragma warning(disable:4996) // Deriving from deprecated class
162 #endif
164 public:
165  FastWriter();
167 
168  void enableYAMLCompatibility();
169 
175  void dropNullPlaceholders();
176 
177  void omitEndingLineFeed();
178 
179 public: // overridden from Writer
180  JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE;
181 
182 private:
183  void writeValue(const Value& value);
184 
185  JSONCPP_STRING document_;
186  bool yamlCompatibilityEnabled_;
187  bool dropNullPlaceholders_;
188  bool omitEndingLineFeed_;
189 };
190 #if defined(_MSC_VER)
191 #pragma warning(pop)
192 #endif
193 
218 #if defined(_MSC_VER)
219 #pragma warning(push)
220 #pragma warning(disable:4996) // Deriving from deprecated class
221 #endif
223 public:
224  StyledWriter();
226 
227 public: // overridden from Writer
232  JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE;
233 
234 private:
235  void writeValue(const Value& value);
236  void writeArrayValue(const Value& value);
237  bool isMultilineArray(const Value& value);
238  void pushValue(const JSONCPP_STRING& value);
239  void writeIndent();
240  void writeWithIndent(const JSONCPP_STRING& value);
241  void indent();
242  void unindent();
243  void writeCommentBeforeValue(const Value& root);
244  void writeCommentAfterValueOnSameLine(const Value& root);
245  bool hasCommentForValue(const Value& value);
246  static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text);
247 
248  typedef std::vector<JSONCPP_STRING> ChildValues;
249 
250  ChildValues childValues_;
251  JSONCPP_STRING document_;
252  JSONCPP_STRING indentString_;
253  unsigned int rightMargin_;
254  unsigned int indentSize_;
255  bool addChildValues_;
256 };
257 #if defined(_MSC_VER)
258 #pragma warning(pop)
259 #endif
260 
286 #if defined(_MSC_VER)
287 #pragma warning(push)
288 #pragma warning(disable:4996) // Deriving from deprecated class
289 #endif
291 public:
295  StyledStreamWriter(JSONCPP_STRING indentation = "\t");
297 
298 public:
305  void write(JSONCPP_OSTREAM& out, const Value& root);
306 
307 private:
308  void writeValue(const Value& value);
309  void writeArrayValue(const Value& value);
310  bool isMultilineArray(const Value& value);
311  void pushValue(const JSONCPP_STRING& value);
312  void writeIndent();
313  void writeWithIndent(const JSONCPP_STRING& value);
314  void indent();
315  void unindent();
316  void writeCommentBeforeValue(const Value& root);
317  void writeCommentAfterValueOnSameLine(const Value& root);
318  bool hasCommentForValue(const Value& value);
319  static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text);
320 
321  typedef std::vector<JSONCPP_STRING> ChildValues;
322 
323  ChildValues childValues_;
324  JSONCPP_OSTREAM* document_;
325  JSONCPP_STRING indentString_;
326  unsigned int rightMargin_;
327  JSONCPP_STRING indentation_;
328  bool addChildValues_ : 1;
329  bool indented_ : 1;
330 };
331 #if defined(_MSC_VER)
332 #pragma warning(pop)
333 #endif
334 
335 #if defined(JSON_HAS_INT64)
338 #endif // if defined(JSON_HAS_INT64)
343 JSONCPP_STRING JSON_API valueToQuotedString(const char* value);
344 
347 JSON_API JSONCPP_OSTREAM& operator<<(JSONCPP_OSTREAM&, const Value& root);
348 
349 } // namespace Json
350 
351 #pragma pack(pop)
352 
353 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
354 #pragma warning(pop)
355 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
356 
357 #endif // JSON_WRITER_H_INCLUDED
Outputs a Value in JSON format without formatting (not human friendly).
Definition: writer.h:163
#define JSONCPP_OVERRIDE
Definition: config.h:94
#define JSONCPP_DEPRECATED(message)
Definition: config.h:135
A simple abstract factory.
Definition: writer.h:58
Int64 LargestInt
Definition: config.h:168
Writes a Value in JSON format in a human friendly way.
Definition: writer.h:222
#define JSON_API
If defined, indicates that the source file is amalgamated to prevent private header inclusion...
Definition: config.h:54
std::string valueToQuotedString(const char *value)
#define JSONCPP_STRING
Definition: config.h:179
UInt64 LargestUInt
Definition: config.h:169
std::string valueToString(Int value)
#define JSONCPP_OSTREAM
Definition: config.h:181
JSON (JavaScript Object Notation).
Definition: allocator.h:14
Json::Value settings_
Configuration of this builder.
Definition: writer.h:114
Abstract class for writers.
Definition: writer.h:143
Represents a JSON value.
Definition: value.h:177
std::ostream * sout_
Definition: writer.h:44
unsigned int UInt
Definition: config.h:154
Writes a Value in JSON format in a human friendly way, to a stream rather than to a string...
Definition: writer.h:290
std::string writeString(StreamWriter::Factory const &factory, Value const &root)
Write into stringstream, then return string, for convenience.
int Int
Definition: config.h:153
std::ostream & operator<<(std::ostream &, const Value &root)
Output using the StyledStreamWriter.
Build a StreamWriter implementation.
Definition: writer.h:89