11
11
#endif // if !defined(JSON_IS_AMALGAMATION)
12
12
#include < vector>
13
13
#include < string>
14
+ #include < ostream>
14
15
15
16
// Disable warning C4251: <data member>: <type> needs to have dll-interface to
16
17
// be used by...
22
23
namespace Json {
23
24
24
25
class Value ;
25
- class StreamWriterBuilder ;
26
26
27
27
/* *
28
28
29
29
Usage:
30
30
\code
31
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();
32
+ void writeToStdout(StreamWriter::Builder const& builder, Value const& value) {
33
+ std::unique_ptr<StreamWriter> const writer(
34
+ builder.newStreamWriter(&std::cout));
35
+ writer->write(value);
36
+ std::cout << std::endl; // add lf and flush
37
+ }
39
38
\endcode
40
39
*/
41
40
class JSON_API StreamWriter {
@@ -57,52 +56,102 @@ class JSON_API StreamWriter {
57
56
// / \throw std::exception possibly, depending on configuration
58
57
virtual int write (Value const & root) = 0;
59
58
60
- // / Because this Builder is non-virtual, we can safely add
61
- // / methods without a major version bump.
62
- // / \see http://stackoverflow.com/questions/14875052/pure-virtual-functions-and-binary-compatibility
63
- class Builder {
64
- StreamWriterBuilder* own_;
65
- Builder (Builder const &); // noncopyable
66
- void operator =(Builder const &); // noncopyable
59
+ /* * \brief A simple abstract factory.
60
+ */
61
+ class JSON_API Factory {
67
62
public:
68
- Builder ();
69
- ~Builder (); // delete underlying StreamWriterBuilder
63
+ virtual ~Factory ();
64
+ // / Do not take ownership of sout, but maintain a reference.
65
+ virtual StreamWriter* newStreamWriter (std::ostream* sout) const = 0;
66
+ }; // Factory
67
+ }; // StreamWriter
70
68
71
- Builder& withCommentStyle (CommentStyle cs); // / default: All
72
- /* * \brief Write in human-friendly style.
69
+ // / \brief Write into stringstream, then return string, for convenience.
70
+ std::string writeString (Value const & root, StreamWriter::Factory const & factory);
73
71
74
- If "", then skip all indentation, newlines, and comments,
75
- which implies CommentStyle::None.
76
- Default: "\t"
77
- */
78
- Builder& withIndentation (std::string indentation);
79
- /* * \brief Drop the "null" string from the writer's output for nullValues.
80
- * Strictly speaking, this is not valid JSON. But when the output is being
81
- * fed to a browser's Javascript, it makes for smaller output and the
82
- * browser can handle the output just fine.
83
- */
84
- Builder& withDropNullPlaceholders (bool v);
85
- /* * \brief Do not add \n at end of document.
86
- * Normally, we add an extra newline, just because.
87
- */
88
- Builder& withOmitEndingLineFeed (bool v);
89
- /* * \brief Add a space after ':'.
90
- * If indentation is non-empty, we surround colon with whitespace,
91
- * e.g. " : "
92
- * This will add back the trailing space when there is no indentation.
93
- * This seems dubious when the entire document is on a single line,
94
- * but we leave this here to repduce the behavior of the old `FastWriter`.
95
- */
96
- Builder& withEnableYAMLCompatibility (bool v);
97
72
98
- // / Do not take ownership of sout, but maintain a reference.
99
- StreamWriter* newStreamWriter (std::ostream* sout) const ;
100
- };
73
+ /* * \brief Build a StreamWriter implementation.
74
+
75
+ Usage:
76
+ \code
77
+ using namespace Json;
78
+ Value value = ...;
79
+ StreamWriter::Builder builder;
80
+ builder.cs_ = StreamWriter::CommentStyle::None;
81
+ std::shared_ptr<StreamWriter> writer(
82
+ builder.newStreamWriter(&std::cout));
83
+ writer->write(value);
84
+ std::cout << std::endl; // add lf and flush
85
+ \endcode
86
+ */
87
+ class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
88
+ public:
89
+ // Note: We cannot add data-members to this class without a major version bump.
90
+ // So these might as well be completely exposed.
91
+
92
+ /* * \brief How to write comments.
93
+ * Default: All
94
+ */
95
+ StreamWriter::CommentStyle cs_;
96
+ /* * \brief Write in human-friendly style.
97
+
98
+ If "", then skip all indentation and newlines.
99
+ In that case, you probably want CommentStyle::None also.
100
+ Default: "\t"
101
+ */
102
+ std::string indentation_;
103
+
104
+ StreamWriterBuilder ();
105
+
106
+ // / Do not take ownership of sout, but maintain a reference.
107
+ StreamWriter* newStreamWriter (std::ostream* sout) const ;
101
108
};
102
109
103
- // / \brief Write into stringstream, then return string, for convenience.
104
- std::string writeString (Value const & root, StreamWriter::Builder const & builder);
110
+ /* * \brief Build a StreamWriter implementation.
111
+ * Comments are not written, and most whitespace is omitted.
112
+ * In addition, there are some special settings to allow compatibility
113
+ * with the old FastWriter.
114
+ * Usage:
115
+ * \code
116
+ * OldCompressingStreamWriterBuilder b;
117
+ * b.dropNullPlaceHolders_ = true; // etc.
118
+ * StreamWriter* w = b.newStreamWriter(&std::cout);
119
+ * w.write(value);
120
+ * delete w;
121
+ * \endcode
122
+ */
123
+ class JSON_API OldCompressingStreamWriterBuilder : public StreamWriter::Factory
124
+ {
125
+ public:
126
+ // Note: We cannot add data-members to this class without a major version bump.
127
+ // So these might as well be completely exposed.
105
128
129
+ /* * \brief Drop the "null" string from the writer's output for nullValues.
130
+ * Strictly speaking, this is not valid JSON. But when the output is being
131
+ * fed to a browser's Javascript, it makes for smaller output and the
132
+ * browser can handle the output just fine.
133
+ */
134
+ bool dropNullPlaceholders_;
135
+ /* * \brief Do not add \n at end of document.
136
+ * Normally, we add an extra newline, just because.
137
+ */
138
+ bool omitEndingLineFeed_;
139
+ /* * \brief Add a space after ':'.
140
+ * If indentation is non-empty, we surround colon with whitespace,
141
+ * e.g. " : "
142
+ * This will add back the trailing space when there is no indentation.
143
+ * This seems dubious when the entire document is on a single line,
144
+ * but we leave this here to repduce the behavior of the old `FastWriter`.
145
+ */
146
+ bool enableYAMLCompatibility_;
147
+
148
+ OldCompressingStreamWriterBuilder ()
149
+ : dropNullPlaceholders_(false )
150
+ , omitEndingLineFeed_(false )
151
+ , enableYAMLCompatibility_(false )
152
+ {}
153
+ virtual StreamWriter* newStreamWriter (std::ostream*) const ;
154
+ };
106
155
107
156
/* * \brief Abstract class for writers.
108
157
* \deprecated Use StreamWriter::Builder.
0 commit comments