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 
15 // Disable warning C4251: <data member>: <type> needs to have dll-interface to
16 // be used by...
17 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
18 #pragma warning(push)
19 #pragma warning(disable : 4251)
20 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
21 
22 namespace Json {
23 
24 class Value;
25 
29 public:
30  virtual ~Writer();
31 
32  virtual std::string write(const Value& root) = 0;
33 };
34 
43 class JSON_API FastWriter : public Writer {
44 public:
45  FastWriter();
46  virtual ~FastWriter() {}
47 
48  void enableYAMLCompatibility();
49 
55  void dropNullPlaceholders();
56 
57  void omitEndingLineFeed();
58 
59 public: // overridden from Writer
60  virtual std::string write(const Value& root);
61 
62 private:
63  void writeValue(const Value& value);
64 
65  std::string document_;
66  bool yamlCompatiblityEnabled_;
67  bool dropNullPlaceholders_;
68  bool omitEndingLineFeed_;
69 };
70 
94 class JSON_API StyledWriter : public Writer {
95 public:
96  StyledWriter();
97  virtual ~StyledWriter() {}
98 
99 public: // overridden from Writer
104  virtual std::string write(const Value& root);
105 
106 private:
107  void writeValue(const Value& value);
108  void writeArrayValue(const Value& value);
109  bool isMultineArray(const Value& value);
110  void pushValue(const std::string& value);
111  void writeIndent();
112  void writeWithIndent(const std::string& value);
113  void indent();
114  void unindent();
115  void writeCommentBeforeValue(const Value& root);
116  void writeCommentAfterValueOnSameLine(const Value& root);
117  bool hasCommentForValue(const Value& value);
118  static std::string normalizeEOL(const std::string& text);
119 
120  typedef std::vector<std::string> ChildValues;
121 
122  ChildValues childValues_;
123  std::string document_;
124  std::string indentString_;
125  int rightMargin_;
126  int indentSize_;
127  bool addChildValues_;
128 };
129 
156 public:
157  StyledStreamWriter(std::string indentation = "\t");
159 
160 public:
167  void write(std::ostream& out, const Value& root);
168 
169 private:
170  void writeValue(const Value& value);
171  void writeArrayValue(const Value& value);
172  bool isMultineArray(const Value& value);
173  void pushValue(const std::string& value);
174  void writeIndent();
175  void writeWithIndent(const std::string& value);
176  void indent();
177  void unindent();
178  void writeCommentBeforeValue(const Value& root);
179  void writeCommentAfterValueOnSameLine(const Value& root);
180  bool hasCommentForValue(const Value& value);
181  static std::string normalizeEOL(const std::string& text);
182 
183  typedef std::vector<std::string> ChildValues;
184 
185  ChildValues childValues_;
186  std::ostream* document_;
187  std::string indentString_;
188  int rightMargin_;
189  std::string indentation_;
190  bool addChildValues_;
191 };
192 
193 #if defined(JSON_HAS_INT64)
194 std::string JSON_API valueToString(Int value);
195 std::string JSON_API valueToString(UInt value);
196 #endif // if defined(JSON_HAS_INT64)
197 std::string JSON_API valueToString(LargestInt value);
198 std::string JSON_API valueToString(LargestUInt value);
199 std::string JSON_API valueToString(double value);
200 std::string JSON_API valueToString(bool value);
201 std::string JSON_API valueToQuotedString(const char* value);
202 
205 JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
206 
207 } // namespace Json
208 
209 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
210 #pragma warning(pop)
211 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
212 
213 #endif // JSON_WRITER_H_INCLUDED
Outputs a Value in JSON format without formatting (not human friendly).
Definition: writer.h:43
Int64 LargestInt
Definition: config.h:106
Writes a Value in JSON format in a human friendly way.
Definition: writer.h:94
#define JSON_API
If defined, indicates that the source file is amalgated to prevent private header inclusion...
Definition: config.h:62
virtual ~FastWriter()
Definition: writer.h:46
std::string valueToQuotedString(const char *value)
UInt64 LargestUInt
Definition: config.h:107
std::string valueToString(Int value)
Definition: json_writer.cpp:67
Abstract class for writers.
Definition: writer.h:28
Represents a JSON value.
Definition: value.h:116
unsigned int UInt
Definition: config.h:92
Writes a Value in JSON format in a human friendly way, to a stream rather than to a string...
Definition: writer.h:155
virtual ~StyledWriter()
Definition: writer.h:97
int Int
Definition: config.h:91
std::ostream & operator<<(std::ostream &, const Value &root)
Output using the StyledStreamWriter.