File: writer.h

package info (click to toggle)
jazz2-native 3.5.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,912 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (171 lines) | stat: -rw-r--r-- 5,978 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE

#ifndef JSON_WRITER_H_INCLUDED
#define JSON_WRITER_H_INCLUDED

#include "value.h"

#include <ostream>
#include <string>
#include <vector>

// Disable warning C4251: <data member>: <type> needs to have dll-interface to
// be used by...
#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) && defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4251)
#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)

#pragma pack(push)
#pragma pack()

namespace Json
{
	class Value;

	/**
	 *
	 * Usage:
	 *  @cpp
	 *  using namespace Json;
	 *  void writeToStdout(StreamWriter::Factory const& factory, Value const& value)
	 * { std::unique_ptr<StreamWriter> const writer( factory.newStreamWriter());
	 *    writer->write(value, &std::cout);
	 *    std::cout << std::endl;  // add lf and flush
	 *  }
	 *  @ce
	 */
	class JSON_API StreamWriter {
	protected:
		OStream* sout_; // not owned; will not delete
	public:
		StreamWriter();
		virtual ~StreamWriter();
		/** Write Value into document as configured in sub-class.
		 *   Do not take ownership of sout, but maintain a reference during function.
		 *   \pre sout != NULL
		 *   @return zero on success (For now, we always return zero, so check the
		 *   stream instead.) @throw std::exception possibly, depending on
		 * configuration
		 */
		virtual int write(Value const& root, OStream* sout) = 0;

		/** @brief A simple abstract factory.
		 */
		class JSON_API Factory {
		public:
			virtual ~Factory();
			/** @brief Allocate a CharReader via operator new().
			 * @throw std::exception if something goes wrong (e.g. invalid settings)
			 */
			virtual StreamWriter* newStreamWriter() const = 0;
		}; // Factory
	}; // StreamWriter

	/** @brief Write into stringstream, then return string, for convenience.
	 * A StreamWriter will be created from the factory, used, and then deleted.
	 */
	StringContainer JSON_API writeString(StreamWriter::Factory const& factory, Value const& root);

	/** @brief Build a StreamWriter implementation.

	* Usage:
	*   \code
	*   using namespace Json;
	*   Value value = ...;
	*   StreamWriterBuilder builder;
	*   builder["commentStyle"] = "None";
	*   builder["indentation"] = "   ";  // or whatever you like
	*   std::unique_ptr<Json::StreamWriter> writer(
	*      builder.newStreamWriter());
	*   writer->write(value, &std::cout);
	*   std::cout << std::endl;  // add lf and flush
	*   \endcode
	*/
	class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
	public:
		// Note: We use a Json::Value so that we can add data-members to this class
		// without a major version bump.
		/** Configuration of this builder.
		 *  Available settings (case-sensitive):
		 *  - "commentStyle": "None" or "All"
		 *  - "indentation":  "<anything>".
		 *  - Setting this to an empty string also omits newline characters.
		 *  - "enableYAMLCompatibility": false or true
		 *  - slightly change the whitespace around colons
		 *  - "dropNullPlaceholders": false or true
		 *  - Drop the "null" string from the writer's output for nullValues.
		 *    Strictly speaking, this is not valid JSON. But when the output is being
		 *    fed to a browser's JavaScript, it makes for smaller output and the
		 *    browser can handle the output just fine.
		 *  - "useSpecialFloats": false or true
		 *  - If true, outputs non-finite floating point values in the following way:
		 *    NaN values as "NaN", positive infinity as "Infinity", and negative
		 *  infinity as "-Infinity".
		 *  - "precision": int
		 *  - Number of precision digits for formatting of real values.
		 *  - "precisionType": "significant"(default) or "decimal"
		 *  - Type of precision for formatting of real values.
		 *  - "emitUTF8": false or true
		 *  - If true, outputs raw UTF8 strings instead of escaping them.

		 *  You can examine 'settings_` yourself
		 *  to see the defaults. You can also write and read them just like any
		 *  JSON Value.
		 *  \sa setDefaults()
		 */
		Json::Value settings_;

		StreamWriterBuilder();
		~StreamWriterBuilder() override;

		/**
		 * @throw std::exception if something goes wrong (e.g. invalid settings)
		 */
		StreamWriter* newStreamWriter() const override;

		/** @return true if 'settings' are legal and consistent;
		 *   otherwise, indicate bad settings via 'invalid'.
		 */
		bool validate(Json::Value* invalid) const;
		/** A simple way to update a specific setting.
		 */
		Value& operator[](const StringContainer& key);

		/** Called by ctor, but you can use this to reset settings_.
		 * \pre 'settings' != NULL (but Json::null is fine)
		 * \remark Defaults:
		 * \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults
		 */
		static void setDefaults(Json::Value* settings);
	};

#if defined(JSON_HAS_INT64)
	StringContainer JSON_API valueToString(Int value);
	StringContainer JSON_API valueToString(UInt value);
#endif // if defined(JSON_HAS_INT64)
	StringContainer JSON_API valueToString(LargestInt value);
	StringContainer JSON_API valueToString(LargestUInt value);
	StringContainer JSON_API valueToString(
		double value, unsigned int precision = Value::defaultRealPrecision,
		PrecisionType precisionType = PrecisionType::significantDigits);
	StringContainer JSON_API valueToString(bool value);
	StringContainer JSON_API valueToQuotedString(const char* value);
	StringContainer JSON_API valueToQuotedString(const char* value, size_t length);

	/// @brief Output using the StyledStreamWriter.
	/// \see Json::operator>>()
	JSON_API OStream& operator<<(OStream&, const Value& root);

} // namespace Json

#pragma pack(pop)

#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
#pragma warning(pop)
#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)

#endif // JSON_WRITER_H_INCLUDED