NIM PC Cross Platform SDK
writer.h
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 NIM_CPP_WRAPPER_UTIL_JSON_WRITER_H_INCLUDED
7#define NIM_CPP_WRAPPER_UTIL_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 nim_cpp_wrapper_util {
26namespace Json {
27
28class Value;
29
42class JSON_API StreamWriter {
43protected:
44 OStream* sout_; // not owned; will not delete
45public:
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
72String JSON_API writeString(StreamWriter::Factory const& factory, Value const& root);
73
90public:
91 // Note: We use a nim_cpp_wrapper_util::Json::Value so that we can add data-members to this class
92 // without a major version bump.
120
122 ~StreamWriterBuilder() override;
123
127 StreamWriter* newStreamWriter() const override;
128
132 bool validate(nim_cpp_wrapper_util::Json::Value* invalid) const;
135 Value& operator[](const String& key);
136
142 static void setDefaults(nim_cpp_wrapper_util::Json::Value* settings);
143};
144
148class 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 JSON_API FastWriter : public Writer {
169public:
170 FastWriter();
171 ~FastWriter() override = default;
172
173 void enableYAMLCompatibility();
174
180 void dropNullPlaceholders();
181
182 void omitEndingLineFeed();
183
184public: // overridden from Writer
185 String write(const Value& root) override;
186
187private:
188 void writeValue(const Value& value);
189
190 String document_;
191 bool yamlCompatibilityEnabled_{false};
192 bool dropNullPlaceholders_{false};
193 bool omitEndingLineFeed_{false};
194};
195#if defined(_MSC_VER)
196#pragma warning(pop)
197#endif
198
223#if defined(_MSC_VER)
224#pragma warning(push)
225#pragma warning(disable : 4996) // Deriving from deprecated class
226#endif
227class JSON_API StyledWriter : public Writer {
228public:
229 StyledWriter();
230 ~StyledWriter() override = default;
231
232public: // overridden from Writer
237 String write(const Value& root) override;
238
239private:
240 void writeValue(const Value& value);
241 void writeArrayValue(const Value& value);
242 bool isMultilineArray(const Value& value);
243 void pushValue(const String& value);
244 void writeIndent();
245 void writeWithIndent(const String& value);
246 void indent();
247 void unindent();
248 void writeCommentBeforeValue(const Value& root);
249 void writeCommentAfterValueOnSameLine(const Value& root);
250 static bool hasCommentForValue(const Value& value);
251 static String normalizeEOL(const String& text);
252
253 using ChildValues = std::vector<String>;
254
255 ChildValues childValues_;
256 String document_;
257 String indentString_;
258 unsigned int rightMargin_{74};
259 unsigned int indentSize_{3};
260 bool addChildValues_{false};
261};
262#if defined(_MSC_VER)
263#pragma warning(pop)
264#endif
265
291#if defined(_MSC_VER)
292#pragma warning(push)
293#pragma warning(disable : 4996) // Deriving from deprecated class
294#endif
295class JSON_API StyledStreamWriter {
296public:
300 StyledStreamWriter(String indentation = "\t");
301 ~StyledStreamWriter() = default;
302
303public:
310 void write(OStream& out, const Value& root);
311
312private:
313 void writeValue(const Value& value);
314 void writeArrayValue(const Value& value);
315 bool isMultilineArray(const Value& value);
316 void pushValue(const String& value);
317 void writeIndent();
318 void writeWithIndent(const String& value);
319 void indent();
320 void unindent();
321 void writeCommentBeforeValue(const Value& root);
322 void writeCommentAfterValueOnSameLine(const Value& root);
323 static bool hasCommentForValue(const Value& value);
324 static String normalizeEOL(const String& text);
325
326 using ChildValues = std::vector<String>;
327
328 ChildValues childValues_;
329 OStream* document_;
330 String indentString_;
331 unsigned int rightMargin_{74};
332 String indentation_;
333 bool addChildValues_ : 1;
334 bool indented_ : 1;
335};
336#if defined(_MSC_VER)
337#pragma warning(pop)
338#endif
339
340#if defined(JSON_HAS_INT64)
341String JSON_API valueToString(Int value);
342String JSON_API valueToString(UInt value);
343#endif // if defined(JSON_HAS_INT64)
344String JSON_API valueToString(LargestInt value);
345String JSON_API valueToString(LargestUInt value);
346String JSON_API valueToString(double value,
347 unsigned int precision = Value::defaultRealPrecision,
348 PrecisionType precisionType = PrecisionType::significantDigits);
349String JSON_API valueToString(bool value);
350String JSON_API valueToQuotedString(const char* value);
351
354JSON_API OStream& operator<<(OStream&, const Value& root);
355
356} // namespace Json
357} // namespace nim_cpp_wrapper_util
358
359#pragma pack(pop)
360
361#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
362#pragma warning(pop)
363#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
364
365#endif // JSON_WRITER_H_INCLUDED
Outputs a Value in JSON format without formatting (not human friendly).
Definition: writer.h:168
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:89
nim_cpp_wrapper_util::Json::Value settings_
Definition: writer.h:119
virtual int write(Value const &root, OStream *sout)=0
Writes a Value in JSON format in a human friendly way, to a stream rather than to a string.
Definition: writer.h:295
Writes a Value in JSON format in a human friendly way.
Definition: writer.h:227
Represents a JSON value.
Definition: value.h:196
static constexpr UInt defaultRealPrecision
Default precision for real value for string representation.
Definition: value.h:248
Abstract class for writers.
Definition: writer.h:148
JSON (JavaScript Object Notation).
Definition: allocator.h:14