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  std::ostream* sout_; // not owned; will not delete
43 public:
44  StreamWriter();
45  virtual ~StreamWriter();
52  virtual int write(Value const& root, std::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 
69 std::string JSON_API writeString(StreamWriter::Factory const& factory, Value const& root);
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.
109 
111  virtual ~StreamWriterBuilder();
112 
116  virtual StreamWriter* newStreamWriter() const;
117 
121  bool validate(Json::Value* invalid) const;
124  Value& operator[](std::string key);
125 
131  static void setDefaults(Json::Value* settings);
132 };
133 
138 public:
139  virtual ~Writer();
140 
141  virtual std::string write(const Value& root) = 0;
142 };
143 
153 class JSON_API FastWriter : public Writer {
154 
155 public:
156  FastWriter();
157  virtual ~FastWriter() {}
158 
159  void enableYAMLCompatibility();
160 
161 public: // overridden from Writer
162  virtual std::string write(const Value& root);
163 
164 private:
165  void writeValue(const Value& value);
166 
167  std::string document_;
168  bool yamlCompatiblityEnabled_;
169 };
170 
195 class JSON_API StyledWriter : public Writer {
196 public:
197  StyledWriter();
198  virtual ~StyledWriter() {}
199 
200 public: // overridden from Writer
205  virtual std::string write(const Value& root);
206 
207 private:
208  void writeValue(const Value& value);
209  void writeArrayValue(const Value& value);
210  bool isMultineArray(const Value& value);
211  void pushValue(const std::string& value);
212  void writeIndent();
213  void writeWithIndent(const std::string& value);
214  void indent();
215  void unindent();
216  void writeCommentBeforeValue(const Value& root);
217  void writeCommentAfterValueOnSameLine(const Value& root);
218  bool hasCommentForValue(const Value& value);
219  static std::string normalizeEOL(const std::string& text);
220 
221  typedef std::vector<std::string> ChildValues;
222 
223  ChildValues childValues_;
224  std::string document_;
225  std::string indentString_;
226  int rightMargin_;
227  int indentSize_;
228  bool addChildValues_;
229 };
230 
258 public:
259  StyledStreamWriter(std::string indentation = "\t");
261 
262 public:
269  void write(std::ostream& out, const Value& root);
270 
271 private:
272  void writeValue(const Value& value);
273  void writeArrayValue(const Value& value);
274  bool isMultineArray(const Value& value);
275  void pushValue(const std::string& value);
276  void writeIndent();
277  void writeWithIndent(const std::string& value);
278  void indent();
279  void unindent();
280  void writeCommentBeforeValue(const Value& root);
281  void writeCommentAfterValueOnSameLine(const Value& root);
282  bool hasCommentForValue(const Value& value);
283  static std::string normalizeEOL(const std::string& text);
284 
285  typedef std::vector<std::string> ChildValues;
286 
287  ChildValues childValues_;
288  std::ostream* document_;
289  std::string indentString_;
290  int rightMargin_;
291  std::string indentation_;
292  bool addChildValues_ : 1;
293  bool indented_ : 1;
294 };
295 
296 #if defined(JSON_HAS_INT64)
297 std::string JSON_API valueToString(Int value);
298 std::string JSON_API valueToString(UInt value);
299 #endif // if defined(JSON_HAS_INT64)
300 std::string JSON_API valueToString(LargestInt value);
301 std::string JSON_API valueToString(LargestUInt value);
302 std::string JSON_API valueToString(double value);
303 std::string JSON_API valueToString(bool value);
304 std::string JSON_API valueToQuotedString(const char* value);
305 
308 JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
309 
310 } // namespace Json
311 
312 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
313 #pragma warning(pop)
314 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
315 
316 #endif // JSON_WRITER_H_INCLUDED
Outputs a Value in JSON format without formatting (not human friendly).
Definition: writer.h:153
A simple abstract factory.
Definition: writer.h:56
Int64 LargestInt
Definition: config.h:103
Writes a Value in JSON format in a human friendly way.
Definition: writer.h:195
#define JSON_API
If defined, indicates that the source file is amalgated to prevent private header inclusion...
Definition: config.h:51
virtual ~FastWriter()
Definition: writer.h:157
std::string valueToQuotedString(const char *value)
UInt64 LargestUInt
Definition: config.h:104
std::string valueToString(Int value)
Definition: json_writer.cpp:94
JSON (JavaScript Object Notation).
Definition: config.h:87
Json::Value settings_
Configuration of this builder.
Definition: writer.h:108
Abstract class for writers.
Definition: writer.h:137
Represents a JSON value.
Definition: value.h:162
static std::string normalizeEOL(Reader::Location begin, Reader::Location end)
std::ostream * sout_
Definition: writer.h:42
unsigned int UInt
Definition: config.h:89
Writes a Value in JSON format in a human friendly way, to a stream rather than to a string...
Definition: writer.h:257
virtual ~StyledWriter()
Definition: writer.h:198
std::string writeString(StreamWriter::Factory const &factory, Value const &root)
Write into stringstream, then return string, for convenience.
int Int
Definition: config.h:88
std::ostream & operator<<(std::ostream &, const Value &root)
Output using the StyledStreamWriter.
Build a StreamWriter implementation.
Definition: writer.h:87