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
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
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
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
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:
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
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
228public:
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
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
296public:
300 StyledStreamWriter(String indentation = "\t");
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
331 unsigned int rightMargin_{74};
334 bool indented_ : 1;
335};
336#if defined(_MSC_VER)
337#pragma warning(pop)
338#endif
339
340#if defined(JSON_HAS_INT64)
343#endif // if defined(JSON_HAS_INT64)
346String JSON_API valueToString(double value,
347 unsigned int precision = Value::defaultRealPrecision,
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
String document_
Definition: writer.h:190
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
OStream * sout_
Definition: writer.h:44
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
ChildValues childValues_
Definition: writer.h:328
String indentString_
Definition: writer.h:330
static String normalizeEOL(const String &text)
OStream * document_
Definition: writer.h:329
String indentation_
Definition: writer.h:332
bool addChildValues_
Definition: writer.h:333
std::vector< String > ChildValues
Definition: writer.h:326
bool indented_
Definition: writer.h:334
Writes a Value in JSON format in a human friendly way.
Definition: writer.h:227
ChildValues childValues_
Definition: writer.h:255
String indentString_
Definition: writer.h:257
String document_
Definition: writer.h:256
static String normalizeEOL(const String &text)
std::vector< String > ChildValues
Definition: writer.h:253
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
virtual String write(const Value &root)=0
#define JSON_API
Definition: config.h:50
PrecisionType
Type of precision for formatting of real values.
Definition: value.h:130
@ significantDigits
we set max number of significant digits in string
Definition: value.h:131
UInt64 LargestUInt
Definition: config.h:104
unsigned int UInt
Definition: config.h:89
int Int
Definition: config.h:88
std::ostream OStream
Definition: config.h:114
Int64 LargestInt
Definition: config.h:103
String JSON_API valueToString(Int value)
String JSON_API valueToQuotedString(const char *value)
Definition: json_writer.cpp:346
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
Definition: config.h:110
JSON_API OStream & operator<<(OStream &, const Value &root)
Output using the StyledStreamWriter.
Definition: json_writer.cpp:1257
String JSON_API writeString(StreamWriter::Factory const &factory, Value const &root)
Write into stringstream, then return string, for convenience. A StreamWriter will be created from the...
Definition: json_writer.cpp:1250
JSON (JavaScript Object Notation).
Definition: allocator.h:14