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
25 namespace Json {
26
27 class Value;
28
41 class JSON_API StreamWriter {
42 protected:
43 OStream* sout_; // not owned; will not delete
44 public:
45 StreamWriter();
46 virtual ~StreamWriter();
54 virtual int write(Value const& root, OStream* sout) = 0;
55
58 class JSON_API Factory {
59 public:
60 virtual ~Factory();
64 virtual StreamWriter* newStreamWriter() const = 0;
65 }; // Factory
66 }; // StreamWriter
67
71 String JSON_API writeString(StreamWriter::Factory const& factory,
72 Value const& root);
73
89 class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
90 public:
91 // Note: We use a Json::Value so that we can add data-members to this class
92 // without a major version bump.
119 Json::Value settings_;
120
121 StreamWriterBuilder();
122 ~StreamWriterBuilder() override;
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
148 class JSONCPP_DEPRECATED("Use StreamWriter instead") JSON_API Writer {
149 public:
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
168 class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API FastWriter
169 : public Writer {
170 public:
171 FastWriter();
172 ~FastWriter() override = default;
173
174 void enableYAMLCompatibility();
175
181 void dropNullPlaceholders();
182
183 void omitEndingLineFeed();
184
185 public: // overridden from Writer
186 String write(const Value& root) override;
187
188 private:
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
228 class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API
229 StyledWriter : public Writer {
230 public:
231 StyledWriter();
232 ~StyledWriter() override = default;
233
234 public: // overridden from Writer
239 String write(const Value& root) override;
240
241 private:
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 using ChildValues = std::vector<String>;
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
297 class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API
298 StyledStreamWriter {
299 public:
303 StyledStreamWriter(String indentation = "\t");
304 ~StyledStreamWriter() = default;
305
306 public:
313 void write(OStream& out, const Value& root);
314
315 private:
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 using ChildValues = std::vector<String>;
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)
344 String JSON_API valueToString(Int value);
345 String JSON_API valueToString(UInt value);
346 #endif // if defined(JSON_HAS_INT64)
347 String JSON_API valueToString(LargestInt value);
348 String JSON_API valueToString(LargestUInt value);
349 String JSON_API valueToString(
350 double value, unsigned int precision = Value::defaultRealPrecision,
351 PrecisionType precisionType = PrecisionType::significantDigits);
352 String JSON_API valueToString(bool value);
353 String JSON_API valueToQuotedString(const char* value);
354
357 JSON_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
Json::Value::defaultRealPrecision
static constexpr UInt defaultRealPrecision
Default precision for real value for string representation.
Definition: value.h:246
Json::StreamWriter::Factory::newStreamWriter
virtual StreamWriter * newStreamWriter() const =0
Allocate a CharReader via operator new().
Json::valueToString
String valueToString(Int value)
Definition: json_writer.cpp:120
Json::StreamWriter
Usage:
Definition: writer.h:41
Json::significantDigits
@ significantDigits
we set max number of significant digits in string
Definition: value.h:129
Json::Int
int Int
Definition: config.h:108
Json::StreamWriter::Factory
A simple abstract factory.
Definition: writer.h:58
Json::StreamWriterBuilder::~StreamWriterBuilder
~StreamWriterBuilder() override
Json::UInt
unsigned int UInt
Definition: config.h:109
Json::StreamWriter::~StreamWriter
virtual ~StreamWriter()
Json::operator<<
OStream & operator<<(OStream &, const Value &root)
Output using the StyledStreamWriter.
Definition: json_writer.cpp:1251
Json::PrecisionType
PrecisionType
Type of precision for formatting of real values.
Definition: value.h:128
Json::StreamWriter::write
virtual int write(Value const &root, OStream *sout)=0
Write Value into document as configured in sub-class.
Json::Value
Represents a JSON value.
Definition: value.h:193
Json::StreamWriterBuilder::settings_
Json::Value settings_
Configuration of this builder.
Definition: writer.h:119
Json::OStream
std::ostream OStream
Definition: config.h:140
JSONCPP_DEPRECATED
#define JSONCPP_DEPRECATED(message)
Definition: config.h:89
Json
JSON (JavaScript Object Notation).
Definition: allocator.h:14
Json::LargestUInt
UInt64 LargestUInt
Definition: config.h:124
JSON_API
#define JSON_API
If defined, indicates that the source file is amalgamated to prevent private header inclusion.
Definition: config.h:50
Json::String
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
Definition: config.h:132
Json::valueToQuotedString
String valueToQuotedString(const char *value)
Definition: json_writer.cpp:350
Json::writeString
String writeString(StreamWriter::Factory const &factory, Value const &root)
Write into stringstream, then return string, for convenience.
Definition: json_writer.cpp:1244
Json::StreamWriterBuilder
Build a StreamWriter implementation.
Definition: writer.h:89
Json::StreamWriter::Factory::~Factory
virtual ~Factory()
Json::LargestInt
Int64 LargestInt
Definition: config.h:123
value.h
Json::StreamWriter::sout_
OStream * sout_
Definition: writer.h:43