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