JsonCpp project page JsonCpp home page

writer.h
Go to the documentation of this file.
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
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 namespace Json {
24 
25 class Value;
26 
41 protected:
42  JSONCPP_OSTREAM* sout_; // not owned; will not delete
43 public:
44  StreamWriter();
45  virtual ~StreamWriter();
52  virtual int write(Value const& root, JSONCPP_OSTREAM* sout) = 0;
53 
56  class JSON_API Factory {
57  public:
58  virtual ~Factory();
62  virtual StreamWriter* newStreamWriter() const = 0;
63  }; // Factory
64 }; // StreamWriter
65 
70 
71 
88 public:
89  // Note: We use a Json::Value so that we can add data-members to this class
90  // without a major version bump.
113 
116 
120  StreamWriter* newStreamWriter() const JSONCPP_OVERRIDE;
121 
125  bool validate(Json::Value* invalid) const;
128  Value& operator[](JSONCPP_STRING key);
129 
135  static void setDefaults(Json::Value* settings);
136 };
137 
142 public:
143  virtual ~Writer();
144 
145  virtual JSONCPP_STRING write(const Value& root) = 0;
146 };
147 
157 class JSON_API FastWriter : public Writer {
158 
159 public:
160  FastWriter();
162 
163  void enableYAMLCompatibility();
164 
170  void dropNullPlaceholders();
171 
172  void omitEndingLineFeed();
173 
174 public: // overridden from Writer
175  JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE;
176 
177 private:
178  void writeValue(const Value& value);
179 
180  JSONCPP_STRING document_;
181  bool yamlCompatiblityEnabled_;
182  bool dropNullPlaceholders_;
183  bool omitEndingLineFeed_;
184 };
185 
210 class JSON_API StyledWriter : public Writer {
211 public:
212  StyledWriter();
214 
215 public: // overridden from Writer
220  JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE;
221 
222 private:
223  void writeValue(const Value& value);
224  void writeArrayValue(const Value& value);
225  bool isMultineArray(const Value& value);
226  void pushValue(const JSONCPP_STRING& value);
227  void writeIndent();
228  void writeWithIndent(const JSONCPP_STRING& value);
229  void indent();
230  void unindent();
231  void writeCommentBeforeValue(const Value& root);
232  void writeCommentAfterValueOnSameLine(const Value& root);
233  bool hasCommentForValue(const Value& value);
234  static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text);
235 
236  typedef std::vector<JSONCPP_STRING> ChildValues;
237 
238  ChildValues childValues_;
239  JSONCPP_STRING document_;
240  JSONCPP_STRING indentString_;
241  unsigned int rightMargin_;
242  unsigned int indentSize_;
243  bool addChildValues_;
244 };
245 
273 public:
274  StyledStreamWriter(JSONCPP_STRING indentation = "\t");
276 
277 public:
284  void write(JSONCPP_OSTREAM& out, const Value& root);
285 
286 private:
287  void writeValue(const Value& value);
288  void writeArrayValue(const Value& value);
289  bool isMultineArray(const Value& value);
290  void pushValue(const JSONCPP_STRING& value);
291  void writeIndent();
292  void writeWithIndent(const JSONCPP_STRING& value);
293  void indent();
294  void unindent();
295  void writeCommentBeforeValue(const Value& root);
296  void writeCommentAfterValueOnSameLine(const Value& root);
297  bool hasCommentForValue(const Value& value);
298  static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text);
299 
300  typedef std::vector<JSONCPP_STRING> ChildValues;
301 
302  ChildValues childValues_;
303  JSONCPP_OSTREAM* document_;
304  JSONCPP_STRING indentString_;
305  unsigned int rightMargin_;
306  JSONCPP_STRING indentation_;
307  bool addChildValues_ : 1;
308  bool indented_ : 1;
309 };
310 
311 #if defined(JSON_HAS_INT64)
314 #endif // if defined(JSON_HAS_INT64)
319 JSONCPP_STRING JSON_API valueToQuotedString(const char* value);
320 
324 
325 } // namespace Json
326 
327 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
328 #pragma warning(pop)
329 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
330 
331 #endif // JSON_WRITER_H_INCLUDED
Outputs a Value in JSON format without formatting (not human friendly).
Definition: writer.h:157
#define JSONCPP_OVERRIDE
Definition: config.h:88
A simple abstract factory.
Definition: writer.h:56
Int64 LargestInt
Definition: config.h:158
Writes a Value in JSON format in a human friendly way.
Definition: writer.h:210
#define JSON_API
If defined, indicates that the source file is amalgated to prevent private header inclusion...
Definition: config.h:53
std::string valueToQuotedString(const char *value)
#define JSONCPP_STRING
Definition: config.h:169
UInt64 LargestUInt
Definition: config.h:159
std::string valueToString(Int value)
#define JSONCPP_OSTREAM
Definition: config.h:171
JSON (JavaScript Object Notation).
Definition: allocator.h:12
Json::Value settings_
Configuration of this builder.
Definition: writer.h:112
Abstract class for writers.
Definition: writer.h:141
Represents a JSON value.
Definition: value.h:175
static std::string normalizeEOL(Reader::Location begin, Reader::Location end)
std::ostream * sout_
Definition: writer.h:42
unsigned int UInt
Definition: config.h:144
Writes a Value in JSON format in a human friendly way, to a stream rather than to a string...
Definition: writer.h:272
std::string writeString(StreamWriter::Factory const &factory, Value const &root)
Write into stringstream, then return string, for convenience.
int Int
Definition: config.h:143
std::ostream & operator<<(std::ostream &, const Value &root)
Output using the StyledStreamWriter.
Build a StreamWriter implementation.
Definition: writer.h:87