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
26 namespace Json {
27
28 class Value;
29
42 class JSON_API StreamWriter {
43 protected:
44 OStream* sout_; // not owned; will not delete
45 public:
46 StreamWriter();
47 virtual ~StreamWriter();
55 virtual int write(Value const& root, OStream* sout) = 0;
56
59 class JSON_API Factory {
60 public:
61 virtual ~Factory();
65 virtual StreamWriter* newStreamWriter() const = 0;
66 }; // Factory
67}; // StreamWriter
68
72 String JSON_API writeString(StreamWriter::Factory const& factory,
73 Value const& root);
74
90 class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
91 public:
92 // Note: We use a Json::Value so that we can add data-members to this class
93 // without a major version bump.
122 Json::Value settings_;
123
124 StreamWriterBuilder();
125 ~StreamWriterBuilder() override;
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
151 class JSON_API Writer {
152 public:
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
171 class JSON_API FastWriter : public Writer {
172 public:
173 FastWriter();
174 ~FastWriter() override = default;
175
176 void enableYAMLCompatibility();
177
183 void dropNullPlaceholders();
184
185 void omitEndingLineFeed();
186
187 public: // overridden from Writer
188 String write(const Value& root) override;
189
190 private:
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 StyledWriter : public Writer {
231 public:
232 StyledWriter();
233 ~StyledWriter() override = default;
234
235 public: // overridden from Writer
240 String write(const Value& root) override;
241
242 private:
243 void writeValue(const Value& value);
244 void writeArrayValue(const Value& value);
245 bool isMultilineArray(const Value& value);
246 void pushValue(const String& value);
247 void writeIndent();
248 void writeWithIndent(const String& value);
249 void indent();
250 void unindent();
251 void writeCommentBeforeValue(const Value& root);
252 void writeCommentAfterValueOnSameLine(const Value& root);
253 static bool hasCommentForValue(const Value& value);
254 static String normalizeEOL(const String& text);
255
256 using ChildValues = std::vector<String>;
257
258 ChildValues childValues_;
259 String document_;
260 String indentString_;
261 unsigned int rightMargin_{74};
262 unsigned int indentSize_{3};
263 bool addChildValues_{false};
264};
265#if defined(_MSC_VER)
266#pragma warning(pop)
267#endif
268
294#if defined(_MSC_VER)
295#pragma warning(push)
296#pragma warning(disable : 4996) // Deriving from deprecated class
297#endif
298 class JSON_API 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 String JSON_API valueToQuotedString(const char* value, size_t length);
355
358 JSON_API OStream& operator<<(OStream&, const Value& root);
359
360} // namespace Json
361
362#pragma pack(pop)
363
364#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
365#pragma warning(pop)
366#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
367
368#endif // JSON_WRITER_H_INCLUDED
Json::FastWriter::write
String write(const Value &root) override
Definition json_writer.cpp:315
Json::FastWriter::FastWriter
FastWriter()
Json::FastWriter::dropNullPlaceholders
void dropNullPlaceholders()
Drop the "null" string from the writer's output for nullValues.
Definition json_writer.cpp:311
Json::FastWriter::enableYAMLCompatibility
void enableYAMLCompatibility()
Definition json_writer.cpp:309
Json::FastWriter::~FastWriter
~FastWriter() override=default
Json::FastWriter::omitEndingLineFeed
void omitEndingLineFeed()
Definition json_writer.cpp:313
Json::StreamWriter::Factory
A simple abstract factory.
Definition writer.h:59
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:90
Json::StreamWriterBuilder::newStreamWriter
StreamWriter * newStreamWriter() const override
Configuration of this builder.
Definition json_writer.cpp:1104
Json::StreamWriterBuilder::validate
bool validate(Json::Value *invalid) const
Definition json_writer.cpp:1147
Json::StreamWriterBuilder::setDefaults
static void setDefaults(Json::Value *settings)
Called by ctor, but you can use this to reset settings_.
Definition json_writer.cpp:1174
Json::StreamWriterBuilder::operator[]
Value & operator[](const String &key)
A simple way to update a specific setting.
Definition json_writer.cpp:1170
Json::StreamWriter
Usage:
Definition writer.h:42
Json::StreamWriter::~StreamWriter
virtual ~StreamWriter()
Json::StreamWriter::StreamWriter
StreamWriter()
Definition json_writer.cpp:1099
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:44
Json::StyledStreamWriter::write
void write(OStream &out, const Value &root)
Serialize a Value in JSON format.
Definition json_writer.cpp:595
Json::StyledStreamWriter::StyledStreamWriter
StyledStreamWriter(String indentation="\t")
Definition json_writer.cpp:591
Json::StyledStreamWriter::~StyledStreamWriter
~StyledStreamWriter()=default
Json::StyledWriter::StyledWriter
StyledWriter()
Json::StyledWriter::~StyledWriter
~StyledWriter() override=default
Json::StyledWriter::write
String write(const Value &root) override
Serialize a Value in JSON format.
Definition json_writer.cpp:381
Json::Value
Represents a JSON value.
Definition value.h:207
Json::Value::defaultRealPrecision
static constexpr UInt defaultRealPrecision
Default precision for real value for string representation.
Definition value.h:261
Json::Writer
Abstract class for writers.
Definition writer.h:151
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:16
Json::writeString
String writeString(StreamWriter::Factory const &factory, Value const &root)
Write into stringstream, then return string, for convenience.
Definition json_writer.cpp:1187
Json::valueToQuotedString
String valueToQuotedString(const char *value)
Definition json_writer.cpp:290
Json::valueToString
String valueToString(Int value)
Definition json_writer.cpp:57
Json::UInt
unsigned int UInt
Definition config.h:110
Json::operator<<
OStream & operator<<(OStream &, const Value &root)
Output using the StyledStreamWriter.
Definition json_writer.cpp:1194
Json::Int
int Int
Definition config.h:109
Json::String
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
Definition config.h:135
Json::OStream
std::ostream OStream
Definition config.h:143
Json::significantDigits
@ significantDigits
we set max number of significant digits in string
Definition value.h:143
value.h