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 public: // overridden from Writer
58  virtual std::string write(const Value &root);
59 
60 private:
61  void writeValue(const Value &value);
62 
63  std::string document_;
64  bool yamlCompatiblityEnabled_;
65  bool dropNullPlaceholders_;
66 };
67 
91 class JSON_API StyledWriter : public Writer {
92 public:
93  StyledWriter();
94  virtual ~StyledWriter() {}
95 
96 public: // overridden from Writer
101  virtual std::string write(const Value &root);
102 
103 private:
104  void writeValue(const Value &value);
105  void writeArrayValue(const Value &value);
106  bool isMultineArray(const Value &value);
107  void pushValue(const std::string &value);
108  void writeIndent();
109  void writeWithIndent(const std::string &value);
110  void indent();
111  void unindent();
112  void writeCommentBeforeValue(const Value &root);
113  void writeCommentAfterValueOnSameLine(const Value &root);
114  bool hasCommentForValue(const Value &value);
115  static std::string normalizeEOL(const std::string &text);
116 
117  typedef std::vector<std::string> ChildValues;
118 
119  ChildValues childValues_;
120  std::string document_;
121  std::string indentString_;
122  int rightMargin_;
123  int indentSize_;
124  bool addChildValues_;
125 };
126 
153 public:
154  StyledStreamWriter(std::string indentation = "\t");
156 
157 public:
164  void write(std::ostream &out, const Value &root);
165 
166 private:
167  void writeValue(const Value &value);
168  void writeArrayValue(const Value &value);
169  bool isMultineArray(const Value &value);
170  void pushValue(const std::string &value);
171  void writeIndent();
172  void writeWithIndent(const std::string &value);
173  void indent();
174  void unindent();
175  void writeCommentBeforeValue(const Value &root);
176  void writeCommentAfterValueOnSameLine(const Value &root);
177  bool hasCommentForValue(const Value &value);
178  static std::string normalizeEOL(const std::string &text);
179 
180  typedef std::vector<std::string> ChildValues;
181 
182  ChildValues childValues_;
183  std::ostream *document_;
184  std::string indentString_;
185  int rightMargin_;
186  std::string indentation_;
187  bool addChildValues_;
188 };
189 
190 #if defined(JSON_HAS_INT64)
191 std::string JSON_API valueToString(Int value);
192 std::string JSON_API valueToString(UInt value);
193 #endif // if defined(JSON_HAS_INT64)
194 std::string JSON_API valueToString(LargestInt value);
195 std::string JSON_API valueToString(LargestUInt value);
196 std::string JSON_API valueToString(double value);
197 std::string JSON_API valueToString(bool value);
198 std::string JSON_API valueToQuotedString(const char *value);
199 
202 JSON_API std::ostream &operator<<(std::ostream &, const Value &root);
203 
204 } // namespace Json
205 
206 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
207 #pragma warning(pop)
208 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
209 
210 #endif // JSON_WRITER_H_INCLUDED
Json::valueToQuotedString
std::string valueToQuotedString(const char *value)
Definition: json_writer.cpp:90
Json::LargestUInt
UInt64 LargestUInt
Definition: config.h:107
Json::Int
int Int
Definition: config.h:91
Json::valueToString
std::string valueToString(Int value)
Definition: json_writer.cpp:55
Json::StyledStreamWriter::~StyledStreamWriter
~StyledStreamWriter()
Definition: writer.h:155
Json::FastWriter
Outputs a Value in JSON format without formatting (not human friendly).
Definition: writer.h:43
Json::LargestInt
Int64 LargestInt
Definition: config.h:106
Json::UInt
unsigned int UInt
Definition: config.h:92
Json::StyledWriter
Writes a Value in JSON format in a human friendly way.
Definition: writer.h:91
Json::Value
Represents a JSON value.
Definition: value.h:116
Json::operator<<
std::ostream & operator<<(std::ostream &, const Value &root)
Output using the StyledStreamWriter.
Definition: json_writer.cpp:659
Json
JSON (JavaScript Object Notation).
Definition: config.h:90
JSON_API
#define JSON_API
If defined, indicates that the source file is amalgated to prevent private header inclusion.
Definition: config.h:62
Json::StyledStreamWriter
Writes a Value in JSON format in a human friendly way, to a stream rather than to a string.
Definition: writer.h:152
Json::FastWriter::~FastWriter
virtual ~FastWriter()
Definition: writer.h:46
Json::Writer
Abstract class for writers.
Definition: writer.h:28
value.h
Json::StyledWriter::~StyledWriter
virtual ~StyledWriter()
Definition: writer.h:94