@@ -31,85 +31,93 @@ class Value;
31
31
using namespace Json;
32
32
void writeToStdout(StreamWriter::Factory const& factory, Value const& value) {
33
33
std::unique_ptr<StreamWriter> const writer(
34
- factory.newStreamWriter(&std::cout ));
35
- writer->write(value);
34
+ factory.newStreamWriter());
35
+ writer->write(value, &std::cout );
36
36
std::cout << std::endl; // add lf and flush
37
37
}
38
38
\endcode
39
39
*/
40
40
class JSON_API StreamWriter {
41
41
protected:
42
- std::ostream& sout_; // not owned; will not delete
42
+ std::ostream* sout_; // not owned; will not delete
43
43
public:
44
- // / Scoped enums are not available until C++11.
45
- struct CommentStyle {
46
- // / Decide whether to write comments.
47
- enum Enum {
48
- None, // /< Drop all comments.
49
- Most, // /< Recover odd behavior of previous versions (not implemented yet).
50
- All // /< Keep all comments.
51
- };
52
- };
53
-
54
- // / Keep a reference, but do not take ownership of `sout`.
55
- StreamWriter (std::ostream* sout);
44
+ StreamWriter ();
56
45
virtual ~StreamWriter ();
57
- // / Write Value into document as configured in sub-class.
58
- // / \return zero on success
59
- // / \throw std::exception possibly, depending on configuration
60
- virtual int write (Value const & root) = 0;
46
+ /* * Write Value into document as configured in sub-class.
47
+ Do not take ownership of sout, but maintain a reference during function.
48
+ \pre sout != NULL
49
+ \return zero on success
50
+ \throw std::exception possibly, depending on configuration
51
+ */
52
+ virtual int write (Value const & root, std::ostream* sout) = 0;
61
53
62
54
/* * \brief A simple abstract factory.
63
55
*/
64
56
class JSON_API Factory {
65
57
public:
66
58
virtual ~Factory ();
67
- // / Do not take ownership of sout, but maintain a reference.
68
- virtual StreamWriter* newStreamWriter (std::ostream* sout) const = 0;
59
+ /* * \brief Allocate a CharReader via operator new().
60
+ * \throw std::exception if something goes wrong (e.g. invalid settings)
61
+ */
62
+ virtual StreamWriter* newStreamWriter () const = 0;
69
63
}; // Factory
70
64
}; // StreamWriter
71
65
72
- // / \brief Write into stringstream, then return string, for convenience.
73
- std::string writeString (Value const & root, StreamWriter::Factory const & factory);
66
+ /* * \brief Write into stringstream, then return string, for convenience.
67
+ * A StreamWriter will be created from the factory, used, and then deleted.
68
+ */
69
+ std::string writeString (StreamWriter::Factory const & factory, Value const & root);
74
70
75
71
76
72
/* * \brief Build a StreamWriter implementation.
77
73
78
- \deprecated This is experimental and will be altered before the next release.
79
-
80
74
Usage:
81
75
\code
82
76
using namespace Json;
83
77
Value value = ...;
84
78
StreamWriterBuilder builder;
85
- builder.cs_ = StreamWriter::CommentStyle::None;
86
- builder.indentation_ = " "; // or whatever you like
87
- writer->write(value);
79
+ builder.settings_["commentStyle"] = "None";
80
+ builder.settings_["indentation"] = " "; // or whatever you like
81
+ std::unique_ptr<Json::StreamWriter> writer(
82
+ builder.newStreamWriter());
83
+ writer->write(value, &std::cout);
88
84
std::cout << std::endl; // add lf and flush
89
85
\endcode
90
86
*/
91
87
class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
92
88
public:
93
- // Note: We cannot add data-members to this class without a major version bump.
94
- // So these might as well be completely exposed.
95
-
96
- /* * \brief How to write comments.
97
- * Default: All
98
- */
99
- StreamWriter::CommentStyle::Enum cs_;
100
- /* * \brief Write in human-friendly style.
101
-
102
- If "", then skip all indentation and newlines.
103
- In that case, you probably want CommentStyle::None also.
104
- Default: "\t"
105
- */
106
- std::string indentation_;
89
+ // Note: We use a Json::Value so that we can add data-members to this class
90
+ // without a major version bump.
91
+ /* * Configuration of this builder.
92
+ Available settings (case-sensitive):
93
+ - "commentStyle": "None", "Some", or "All"
94
+ - "indentation": "<anything>"
95
+
96
+ You can examine 'settings_` yourself
97
+ to see the defaults. You can also write and read them just like any
98
+ JSON Value.
99
+ \sa setDefaults()
100
+ */
101
+ Json::Value settings_;
107
102
108
103
StreamWriterBuilder ();
109
104
virtual ~StreamWriterBuilder ();
110
105
111
- // / Do not take ownership of sout, but maintain a reference.
112
- virtual StreamWriter* newStreamWriter (std::ostream* sout) const ;
106
+ /* *
107
+ * \throw std::exception if something goes wrong (e.g. invalid settings)
108
+ */
109
+ virtual StreamWriter* newStreamWriter () const ;
110
+
111
+ /* * \return true if 'settings' are legal and consistent;
112
+ * otherwise, indicate bad settings via 'invalid'.
113
+ */
114
+ bool validate (Json::Value* invalid) const ;
115
+ /* * Called by ctor, but you can use this to reset settings_.
116
+ * \pre 'settings' != NULL (but Json::null is fine)
117
+ * \remark Defaults:
118
+ * \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults
119
+ */
120
+ static void setDefaults (Json::Value* settings);
113
121
};
114
122
115
123
/* * \brief Build a StreamWriter implementation.
@@ -120,10 +128,12 @@ class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
120
128
* \code
121
129
* OldCompressingStreamWriterBuilder b;
122
130
* b.dropNullPlaceHolders_ = true; // etc.
123
- * StreamWriter* w = b.newStreamWriter(&std::cout );
124
- * w->write(value);
131
+ * StreamWriter* w = b.newStreamWriter();
132
+ * w->write(value, &std::cout );
125
133
* delete w;
126
134
* \endcode
135
+ *
136
+ * \deprecated Use StreamWriterBuilder
127
137
*/
128
138
class JSON_API OldCompressingStreamWriterBuilder : public StreamWriter::Factory
129
139
{
@@ -155,7 +165,7 @@ class JSON_API OldCompressingStreamWriterBuilder : public StreamWriter::Factory
155
165
, omitEndingLineFeed_(false )
156
166
, enableYAMLCompatibility_(false )
157
167
{}
158
- virtual StreamWriter* newStreamWriter (std::ostream* ) const ;
168
+ virtual StreamWriter* newStreamWriter () const ;
159
169
};
160
170
161
171
/* * \brief Abstract class for writers.
0 commit comments