22
22
namespace Json {
23
23
24
24
class Value ;
25
+ class StreamWriterBuilder ;
26
+
27
+ /* *
28
+
29
+ Usage:
30
+
31
+ using namespace Json;
32
+ Value value;
33
+ StreamWriter::Builder builder;
34
+ builder.withCommentStyle(StreamWriter::CommentStyle::None);
35
+ std::shared_ptr<StreamWriter> writer(
36
+ builder.newStreamWriter(&std::cout));
37
+ writer->write(value);
38
+ std::cout.flush();
39
+ */
40
+ class JSON_API StreamWriter {
41
+ protected:
42
+ std::ostream& sout_; // not owned; will not delete
43
+ public:
44
+ // / `All`: Keep all comments.
45
+ // / `None`: Drop all comments.
46
+ // / Use `Most` to recover the odd behavior of previous versions.
47
+ // / Only `All` is currently implemented.
48
+ enum class CommentStyle {None, Most, All};
49
+
50
+ // / Keep a reference, but do not take ownership of `sout`.
51
+ StreamWriter (std::ostream* sout);
52
+ virtual ~StreamWriter ();
53
+ // / Write Value into document as configured in sub-class.
54
+ // / \return zero on success
55
+ // / \throw std::exception possibly, depending on configuration
56
+ virtual int write (Value const & root) = 0;
57
+
58
+ // / Because this Builder is non-virtual, we can safely add
59
+ // / methods without a major version bump.
60
+ // / \see http://stackoverflow.com/questions/14875052/pure-virtual-functions-and-binary-compatibility
61
+ class Builder {
62
+ StreamWriterBuilder* own_;
63
+ Builder (Builder const &); // noncopyable
64
+ void operator =(Builder const &); // noncopyable
65
+ public:
66
+ Builder ();
67
+ ~Builder (); // delete underlying StreamWriterBuilder
68
+
69
+ Builder& withCommentStyle (CommentStyle cs); // / default: All
70
+ /* * \brief Write in human-friendly style.
71
+
72
+ If "", then skip all indentation, newlines, and comments,
73
+ which implies CommentStyle::None.
74
+ Default: "\t"
75
+ */
76
+ Builder& withIndentation (std::string indentation);
77
+ /* * \brief Drop the "null" string from the writer's output for nullValues.
78
+ * Strictly speaking, this is not valid JSON. But when the output is being
79
+ * fed to a browser's Javascript, it makes for smaller output and the
80
+ * browser can handle the output just fine.
81
+ */
82
+ Builder& withDropNullPlaceholders (bool v);
83
+ /* * \brief Do not add \n at end of document.
84
+ * Normally, we add an extra newline, just because.
85
+ */
86
+ Builder& withOmitEndingLineFeed (bool v);
87
+ /* * \brief Add a space after ':'.
88
+ * If indentation is non-empty, we surround colon with whitespace,
89
+ * e.g. " : "
90
+ * This will add back the trailing space when there is no indentation.
91
+ * This seems dubious when the entire document is on a single line,
92
+ * but we leave this here to repduce the behavior of the old `FastWriter`.
93
+ */
94
+ Builder& withEnableYAMLCompatibility (bool v);
95
+
96
+ // / Do not take ownership of sout, but maintain a reference.
97
+ StreamWriter* newStreamWriter (std::ostream* sout) const ;
98
+ };
99
+ };
100
+
101
+ // / \brief Write into stringstream, then return string, for convenience.
102
+ std::string writeString (Value const & root, StreamWriter::Builder const & builder);
103
+
25
104
26
105
/* * \brief Abstract class for writers.
106
+ * \deprecated Use StreamWriter::Builder.
27
107
*/
28
108
class JSON_API Writer {
29
109
public:
@@ -39,6 +119,7 @@ class JSON_API Writer {
39
119
*consumption,
40
120
* but may be usefull to support feature such as RPC where bandwith is limited.
41
121
* \sa Reader, Value
122
+ * \deprecated Use StreamWriter::Builder.
42
123
*/
43
124
class JSON_API FastWriter : public Writer {
44
125
public:
@@ -90,6 +171,7 @@ class JSON_API FastWriter : public Writer {
90
171
*#CommentPlacement.
91
172
*
92
173
* \sa Reader, Value, Value::setComment()
174
+ * \deprecated Use StreamWriter::Builder.
93
175
*/
94
176
class JSON_API StyledWriter : public Writer {
95
177
public:
@@ -151,6 +233,7 @@ class JSON_API StyledWriter : public Writer {
151
233
*
152
234
* \param indentation Each level will be indented by this amount extra.
153
235
* \sa Reader, Value, Value::setComment()
236
+ * \deprecated Use StreamWriter::Builder.
154
237
*/
155
238
class JSON_API StyledStreamWriter {
156
239
public:
0 commit comments