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 
166  void dropNullPlaceholders();
167 
168  void omitEndingLineFeed();
169 
170 public: // overridden from Writer
171  virtual std::string write(const Value& root);
172 
173 private:
174  void writeValue(const Value& value);
175 
176  std::string document_;
177  bool yamlCompatiblityEnabled_;
178  bool dropNullPlaceholders_;
179  bool omitEndingLineFeed_;
180 };
181 
206 class JSON_API StyledWriter : public Writer {
207 public:
208  StyledWriter();
209  virtual ~StyledWriter() {}
210 
211 public: // overridden from Writer
216  virtual std::string write(const Value& root);
217 
218 private:
219  void writeValue(const Value& value);
220  void writeArrayValue(const Value& value);
221  bool isMultineArray(const Value& value);
222  void pushValue(const std::string& value);
223  void writeIndent();
224  void writeWithIndent(const std::string& value);
225  void indent();
226  void unindent();
227  void writeCommentBeforeValue(const Value& root);
228  void writeCommentAfterValueOnSameLine(const Value& root);
229  bool hasCommentForValue(const Value& value);
230  static std::string normalizeEOL(const std::string& text);
231 
232  typedef std::vector<std::string> ChildValues;
233 
234  ChildValues childValues_;
235  std::string document_;
236  std::string indentString_;
237  int rightMargin_;
238  int indentSize_;
239  bool addChildValues_;
240 };
241 
269 public:
270  StyledStreamWriter(std::string indentation = "\t");
272 
273 public:
280  void write(std::ostream& out, const Value& root);
281 
282 private:
283  void writeValue(const Value& value);
284  void writeArrayValue(const Value& value);
285  bool isMultineArray(const Value& value);
286  void pushValue(const std::string& value);
287  void writeIndent();
288  void writeWithIndent(const std::string& value);
289  void indent();
290  void unindent();
291  void writeCommentBeforeValue(const Value& root);
292  void writeCommentAfterValueOnSameLine(const Value& root);
293  bool hasCommentForValue(const Value& value);
294  static std::string normalizeEOL(const std::string& text);
295 
296  typedef std::vector<std::string> ChildValues;
297 
298  ChildValues childValues_;
299  std::ostream* document_;
300  std::string indentString_;
301  int rightMargin_;
302  std::string indentation_;
303  bool addChildValues_ : 1;
304  bool indented_ : 1;
305 };
306 
307 #if defined(JSON_HAS_INT64)
308 std::string JSON_API valueToString(Int value);
309 std::string JSON_API valueToString(UInt value);
310 #endif // if defined(JSON_HAS_INT64)
311 std::string JSON_API valueToString(LargestInt value);
312 std::string JSON_API valueToString(LargestUInt value);
313 std::string JSON_API valueToString(double value);
314 std::string JSON_API valueToString(bool value);
315 std::string JSON_API valueToQuotedString(const char* value);
316 
319 JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
320 
321 } // namespace Json
322 
323 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
324 #pragma warning(pop)
325 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
326 
327 #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:206
#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:98
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:268
virtual ~StyledWriter()
Definition: writer.h:209
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