|
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...
|
@@ -60,7 +61,7 @@ class JSON_API StreamWriter {
|
60 | 61 | /// Because this Builder is non-virtual, we can safely add
|
61 | 62 | /// methods without a major version bump.
|
62 | 63 | /// \see http://stackoverflow.com/questions/14875052/pure-virtual-functions-and-binary-compatibility
|
63 |
| - class Builder { |
| 64 | + class JSON_API Builder { |
64 | 65 | StreamWriterBuilder* own_;
|
65 | 66 | Builder(Builder const&); // noncopyable
|
66 | 67 | void operator=(Builder const&); // noncopyable
|
@@ -98,11 +99,81 @@ class JSON_API StreamWriter {
|
98 | 99 | /// Do not take ownership of sout, but maintain a reference.
|
99 | 100 | StreamWriter* newStreamWriter(std::ostream* sout) const;
|
100 | 101 | };
|
| 102 | + |
| 103 | + /** \brief A simple abstract factory. |
| 104 | + */ |
| 105 | + class JSON_API Factory { |
| 106 | + public: |
| 107 | + virtual ~Factory(); |
| 108 | + /* Because this is only a trivial API (the Factory pattern), we will |
| 109 | + * never need to add virtual methods, so we do not need a concrete wrapper. |
| 110 | + * This is better than the Builder above, but not everyone will agree. |
| 111 | + */ |
| 112 | + |
| 113 | + /// Do not take ownership of sout, but maintain a reference. |
| 114 | + virtual StreamWriter* newStreamWriter(std::ostream* sout) const = 0; |
| 115 | + }; |
| 116 | + |
| 117 | + /** \brief Extensions of this are used to create a StreamWriter::Factory. |
| 118 | + */ |
| 119 | + class JSON_API FactoryFactory { |
| 120 | + virtual ~FactoryFactory(); |
| 121 | + virtual Factory* newFactory() const = 0; |
| 122 | + /* This class will seem strange to some developers, but it actually |
| 123 | + * simplifies our library maintenance. |
| 124 | + */ |
| 125 | + }; |
| 126 | + |
101 | 127 | };
|
102 | 128 |
|
103 | 129 | /// \brief Write into stringstream, then return string, for convenience.
|
104 | 130 | std::string writeString(Value const& root, StreamWriter::Builder const& builder);
|
105 | 131 |
|
| 132 | +/** \brief Build a StreamWriter implementation. |
| 133 | + * Comments are not written, and most whitespace is omitted. |
| 134 | + * In addition, there are some special settings to allow compatibility |
| 135 | + * with the old FastWriter. |
| 136 | + * Usage: |
| 137 | + * \code |
| 138 | + * OldCompressingStreamWriterBuilder b; |
| 139 | + * b.dropNullPlaceHolders_ = true; // etc. |
| 140 | + * StreamWriter* w = b.newStreamWriter(&std::cout); |
| 141 | + * w.write(value); |
| 142 | + * delete w; |
| 143 | + * \endcode |
| 144 | + */ |
| 145 | +class JSON_API OldCompressingStreamWriterBuilder |
| 146 | +{ |
| 147 | +public: |
| 148 | + // Note: We cannot add data-members to this class without a major version bump. |
| 149 | + // So these might as well be completely exposed. |
| 150 | + |
| 151 | + /** \brief Drop the "null" string from the writer's output for nullValues. |
| 152 | + * Strictly speaking, this is not valid JSON. But when the output is being |
| 153 | + * fed to a browser's Javascript, it makes for smaller output and the |
| 154 | + * browser can handle the output just fine. |
| 155 | + */ |
| 156 | + bool dropNullPlaceholders_; |
| 157 | + /** \brief Do not add \n at end of document. |
| 158 | + * Normally, we add an extra newline, just because. |
| 159 | + */ |
| 160 | + bool omitEndingLineFeed_; |
| 161 | + /** \brief Add a space after ':'. |
| 162 | + * If indentation is non-empty, we surround colon with whitespace, |
| 163 | + * e.g. " : " |
| 164 | + * This will add back the trailing space when there is no indentation. |
| 165 | + * This seems dubious when the entire document is on a single line, |
| 166 | + * but we leave this here to repduce the behavior of the old `FastWriter`. |
| 167 | + */ |
| 168 | + bool enableYAMLCompatibility_; |
| 169 | + |
| 170 | + OldCompressingStreamWriterBuilder() |
| 171 | + : dropNullPlaceholders_(false) |
| 172 | + , omitEndingLineFeed_(false) |
| 173 | + , enableYAMLCompatibility_(false) |
| 174 | + {} |
| 175 | + virtual StreamWriter* newStreamWriter(std::ostream*) const; |
| 176 | +}; |
106 | 177 |
|
107 | 178 | /** \brief Abstract class for writers.
|
108 | 179 | * \deprecated Use StreamWriter::Builder.
|
|
0 commit comments