JsonCpp project page Classes Namespace JsonCpp home page

writer.h
Go to the documentation of this file.
1// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
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 <ostream>
13#include <string>
14#include <vector>
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) && defined(_MSC_VER)
19#pragma warning(push)
20#pragma warning(disable : 4251)
21#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
22
23#pragma pack(push, 8)
24
25namespace Json {
26
27class Value;
28
42protected:
43 OStream* sout_; // not owned; will not delete
44public:
46 virtual ~StreamWriter();
54 virtual int write(Value const& root, OStream* sout) = 0;
55
59 public:
60 virtual ~Factory();
64 virtual StreamWriter* newStreamWriter() const = 0;
65 }; // Factory
66}; // StreamWriter
67
72 Value const& root);
73
90public:
91 // Note: We use a Json::Value so that we can add data-members to this class
92 // without a major version bump.
120
123
127 StreamWriter* newStreamWriter() const override;
128
132 bool validate(Json::Value* invalid) const;
135 Value& operator[](const String& key);
136
142 static void setDefaults(Json::Value* settings);
143};
144
148class JSONCPP_DEPRECATED("Use StreamWriter instead") JSON_API Writer {
149public:
150 virtual ~Writer();
151
152 virtual String write(const Value& root) = 0;
153};
154
164#if defined(_MSC_VER)
165#pragma warning(push)
166#pragma warning(disable : 4996) // Deriving from deprecated class
167#endif
168class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API FastWriter
169 : public Writer {
170public:
171 FastWriter();
172 ~FastWriter() override = default;
173
174 void enableYAMLCompatibility();
175
181 void dropNullPlaceholders();
182
183 void omitEndingLineFeed();
184
185public: // overridden from Writer
186 String write(const Value& root) override;
187
188private:
189 void writeValue(const Value& value);
190
191 String document_;
192 bool yamlCompatibilityEnabled_{false};
193 bool dropNullPlaceholders_{false};
194 bool omitEndingLineFeed_{false};
195};
196#if defined(_MSC_VER)
197#pragma warning(pop)
198#endif
199
224#if defined(_MSC_VER)
225#pragma warning(push)
226#pragma warning(disable : 4996) // Deriving from deprecated class
227#endif
228class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API
229 StyledWriter : public Writer {
230public:
231 StyledWriter();
232 ~StyledWriter() override = default;
233
234public: // overridden from Writer
239 String write(const Value& root) override;
240
241private:
242 void writeValue(const Value& value);
243 void writeArrayValue(const Value& value);
244 bool isMultilineArray(const Value& value);
245 void pushValue(const String& value);
246 void writeIndent();
247 void writeWithIndent(const String& value);
248 void indent();
249 void unindent();
250 void writeCommentBeforeValue(const Value& root);
251 void writeCommentAfterValueOnSameLine(const Value& root);
252 static bool hasCommentForValue(const Value& value);
253 static String normalizeEOL(const String& text);
254
255 typedef std::vector<String> ChildValues;
256
257 ChildValues childValues_;
258 String document_;
259 String indentString_;
260 unsigned int rightMargin_{74};
261 unsigned int indentSize_{3};
262 bool addChildValues_{false};
263};
264#if defined(_MSC_VER)
265#pragma warning(pop)
266#endif
267
293#if defined(_MSC_VER)
294#pragma warning(push)
295#pragma warning(disable : 4996) // Deriving from deprecated class
296#endif
297class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API
298 StyledStreamWriter {
299public:
303 StyledStreamWriter(String indentation = "\t");
304 ~StyledStreamWriter() = default;
305
306public:
313 void write(OStream& out, const Value& root);
314
315private:
316 void writeValue(const Value& value);
317 void writeArrayValue(const Value& value);
318 bool isMultilineArray(const Value& value);
319 void pushValue(const String& value);
320 void writeIndent();
321 void writeWithIndent(const String& value);
322 void indent();
323 void unindent();
324 void writeCommentBeforeValue(const Value& root);
325 void writeCommentAfterValueOnSameLine(const Value& root);
326 static bool hasCommentForValue(const Value& value);
327 static String normalizeEOL(const String& text);
328
329 typedef std::vector<String> ChildValues;
330
331 ChildValues childValues_;
332 OStream* document_;
333 String indentString_;
334 unsigned int rightMargin_{74};
335 String indentation_;
336 bool addChildValues_ : 1;
337 bool indented_ : 1;
338};
339#if defined(_MSC_VER)
340#pragma warning(pop)
341#endif
342
343#if defined(JSON_HAS_INT64)
346#endif // if defined(JSON_HAS_INT64)
350 double value, unsigned int precision = Value::defaultRealPrecision,
352String JSON_API valueToString(bool value);
353String JSON_API valueToQuotedString(const char* value);
354
357JSON_API OStream& operator<<(OStream&, const Value& root);
358
359} // namespace Json
360
361#pragma pack(pop)
362
363#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
364#pragma warning(pop)
365#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
366
367#endif // JSON_WRITER_H_INCLUDED
A simple abstract factory.
Definition writer.h:58
virtual StreamWriter * newStreamWriter() const =0
Allocate a CharReader via operator new().
Build a StreamWriter implementation.
Definition writer.h:89
Json::Value settings_
Configuration of this builder.
Definition writer.h:119
virtual ~StreamWriter()
virtual int write(Value const &root, OStream *sout)=0
Write Value into document as configured in sub-class.
OStream * sout_
Definition writer.h:43
Represents a JSON value.
Definition value.h:188
static constexpr UInt defaultRealPrecision
Default precision for real value for string representation.
Definition value.h:241
#define JSON_API
If defined, indicates that the source file is amalgamated to prevent private header inclusion.
Definition config.h:66
#define JSONCPP_DEPRECATED(message)
Definition config.h:119
JSON (JavaScript Object Notation).
Definition allocator.h:14
std::ostream OStream
Definition config.h:170
int Int
Definition config.h:138
Int64 LargestInt
Definition config.h:153
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
Definition config.h:162
String writeString(StreamWriter::Factory const &factory, Value const &root)
Write into stringstream, then return string, for convenience.
String valueToQuotedString(const char *value)
String valueToString(Int value)
unsigned int UInt
Definition config.h:139
OStream & operator<<(OStream &, const Value &root)
Output using the StyledStreamWriter.
UInt64 LargestUInt
Definition config.h:154
PrecisionType
Type of precision for formatting of real values.
Definition value.h:118
@ significantDigits
we set max number of significant digits in string
Definition value.h:119