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)
24#pragma pack()
25
26namespace Json {
27
28class Value;
29
43protected:
44 OStream* sout_; // not owned; will not delete
45public:
47 virtual ~StreamWriter();
55 virtual int write(Value const& root, OStream* sout) = 0;
56
60 public:
61 virtual ~Factory();
65 virtual StreamWriter* newStreamWriter() const = 0;
66 }; // Factory
67}; // StreamWriter
68
73 Value const& root);
74
91public:
92 // Note: We use a Json::Value so that we can add data-members to this class
93 // without a major version bump.
123
126
130 StreamWriter* newStreamWriter() const override;
131
135 bool validate(Json::Value* invalid) const;
138 Value& operator[](const String& key);
139
145 static void setDefaults(Json::Value* settings);
146};
147
152public:
153 virtual ~Writer();
154
155 virtual String write(const Value& root) = 0;
156};
157
167#if defined(_MSC_VER)
168#pragma warning(push)
169#pragma warning(disable : 4996) // Deriving from deprecated class
170#endif
172 : public Writer {
173public:
175 ~FastWriter() override = default;
176
177 void enableYAMLCompatibility();
178
184 void dropNullPlaceholders();
185
186 void omitEndingLineFeed();
187
188public: // overridden from Writer
189 String write(const Value& root) override;
190
191private:
192 void writeValue(const Value& value);
193
194 String document_;
195 bool yamlCompatibilityEnabled_{false};
196 bool dropNullPlaceholders_{false};
197 bool omitEndingLineFeed_{false};
198};
199#if defined(_MSC_VER)
200#pragma warning(pop)
201#endif
202
227#if defined(_MSC_VER)
228#pragma warning(push)
229#pragma warning(disable : 4996) // Deriving from deprecated class
230#endif
232 StyledWriter : public Writer {
233public:
235 ~StyledWriter() override = default;
236
237public: // overridden from Writer
242 String write(const Value& root) override;
243
244private:
245 void writeValue(const Value& value);
246 void writeArrayValue(const Value& value);
247 bool isMultilineArray(const Value& value);
248 void pushValue(const String& value);
249 void writeIndent();
250 void writeWithIndent(const String& value);
251 void indent();
252 void unindent();
253 void writeCommentBeforeValue(const Value& root);
254 void writeCommentAfterValueOnSameLine(const Value& root);
255 static bool hasCommentForValue(const Value& value);
256 static String normalizeEOL(const String& text);
257
258 using ChildValues = std::vector<String>;
259
260 ChildValues childValues_;
261 String document_;
262 String indentString_;
263 unsigned int rightMargin_{74};
264 unsigned int indentSize_{3};
265 bool addChildValues_{false};
266};
267#if defined(_MSC_VER)
268#pragma warning(pop)
269#endif
270
296#if defined(_MSC_VER)
297#pragma warning(push)
298#pragma warning(disable : 4996) // Deriving from deprecated class
299#endif
302public:
306 StyledStreamWriter(String indentation = "\t");
308
309public:
316 void write(OStream& out, const Value& root);
317
318private:
319 void writeValue(const Value& value);
320 void writeArrayValue(const Value& value);
321 bool isMultilineArray(const Value& value);
322 void pushValue(const String& value);
323 void writeIndent();
324 void writeWithIndent(const String& value);
325 void indent();
326 void unindent();
327 void writeCommentBeforeValue(const Value& root);
328 void writeCommentAfterValueOnSameLine(const Value& root);
329 static bool hasCommentForValue(const Value& value);
330 static String normalizeEOL(const String& text);
331
332 using ChildValues = std::vector<String>;
333
334 ChildValues childValues_;
335 OStream* document_;
336 String indentString_;
337 unsigned int rightMargin_{74};
338 String indentation_;
339 bool addChildValues_ : 1;
340 bool indented_ : 1;
341};
342#if defined(_MSC_VER)
343#pragma warning(pop)
344#endif
345
346#if defined(JSON_HAS_INT64)
349#endif // if defined(JSON_HAS_INT64)
353 double value, unsigned int precision = Value::defaultRealPrecision,
355String JSON_API valueToString(bool value);
356String JSON_API valueToQuotedString(const char* value);
357
360JSON_API OStream& operator<<(OStream&, const Value& root);
361
362} // namespace Json
363
364#pragma pack(pop)
365
366#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
367#pragma warning(pop)
368#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
369
370#endif // JSON_WRITER_H_INCLUDED
Outputs a Value in JSON format without formatting (not human friendly).
Definition: writer.h:172
~FastWriter() override=default
A simple abstract factory.
Definition: writer.h:59
virtual StreamWriter * newStreamWriter() const =0
Allocate a CharReader via operator new().
Build a StreamWriter implementation.
Definition: writer.h:90
Json::Value settings_
Configuration of this builder.
Definition: writer.h:122
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:44
Writes a Value in JSON format in a human friendly way, to a stream rather than to a string.
Definition: writer.h:301
Writes a Value in JSON format in a human friendly way.
Definition: writer.h:232
~StyledWriter() override=default
Represents a JSON value.
Definition: value.h:194
static constexpr UInt defaultRealPrecision
Default precision for real value for string representation.
Definition: value.h:247
Abstract class for writers.
Definition: writer.h:151
virtual ~Writer()
virtual String write(const Value &root)=0
#define JSON_API
If defined, indicates that the source file is amalgamated to prevent private header inclusion.
Definition: config.h:50
JSON (JavaScript Object Notation).
Definition: allocator.h:15
std::ostream OStream
Definition: config.h:140
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
Definition: config.h:132
String writeString(StreamWriter::Factory const &factory, Value const &root)
Write into stringstream, then return string, for convenience.
Int64 LargestInt
Definition: config.h:123
String valueToQuotedString(const char *value)
String valueToString(Int value)
OStream & operator<<(OStream &, const Value &root)
Output using the StyledStreamWriter.
int Int
Definition: config.h:108
UInt64 LargestUInt
Definition: config.h:124
unsigned int UInt
Definition: config.h:109
PrecisionType
Type of precision for formatting of real values.
Definition: value.h:129
@ significantDigits
we set max number of significant digits in string
Definition: value.h:130