Skip to content

Commit 177b7b8

Browse files
committed
OldCompressingStreamWriterBuilder
1 parent 9da9f84 commit 177b7b8

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

include/json/writer.h

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#endif // if !defined(JSON_IS_AMALGAMATION)
1212
#include <vector>
1313
#include <string>
14+
#include <ostream>
1415

1516
// Disable warning C4251: <data member>: <type> needs to have dll-interface to
1617
// be used by...
@@ -60,7 +61,7 @@ class JSON_API StreamWriter {
6061
/// Because this Builder is non-virtual, we can safely add
6162
/// methods without a major version bump.
6263
/// \see http://stackoverflow.com/questions/14875052/pure-virtual-functions-and-binary-compatibility
63-
class Builder {
64+
class JSON_API Builder {
6465
StreamWriterBuilder* own_;
6566
Builder(Builder const&); // noncopyable
6667
void operator=(Builder const&); // noncopyable
@@ -98,11 +99,81 @@ class JSON_API StreamWriter {
9899
/// Do not take ownership of sout, but maintain a reference.
99100
StreamWriter* newStreamWriter(std::ostream* sout) const;
100101
};
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+
101127
};
102128

103129
/// \brief Write into stringstream, then return string, for convenience.
104130
std::string writeString(Value const& root, StreamWriter::Builder const& builder);
105131

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+
};
106177

107178
/** \brief Abstract class for writers.
108179
* \deprecated Use StreamWriter::Builder.

src/lib_json/json_writer.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,11 +1083,34 @@ StreamWriter::Builder& StreamWriter::Builder::withEnableYAMLCompatibility(bool v
10831083
own_->setEnableYAMLCompatibility(v);
10841084
return *this;
10851085
}
1086-
StreamWriter* StreamWriter::Builder::newStreamWriter(std::ostream* sout) const
1086+
StreamWriter* StreamWriter::Builder::newStreamWriter(
1087+
std::ostream* sout) const
10871088
{
10881089
return own_->newStreamWriter(sout);
10891090
}
10901091

1092+
StreamWriter* OldCompressingStreamWriterBuilder::newStreamWriter(
1093+
std::ostream* stream) const
1094+
{
1095+
std::string colonSymbol = " : ";
1096+
if (enableYAMLCompatibility_) {
1097+
colonSymbol = ": ";
1098+
} else {
1099+
colonSymbol = ":";
1100+
}
1101+
std::string nullSymbol = "null";
1102+
if (dropNullPlaceholders_) {
1103+
nullSymbol = "";
1104+
}
1105+
std::string endingLineFeedSymbol = "\n";
1106+
if (omitEndingLineFeed_) {
1107+
endingLineFeedSymbol = "";
1108+
}
1109+
return new BuiltStyledStreamWriter(stream,
1110+
"", StreamWriter::CommentStyle::None,
1111+
colonSymbol, nullSymbol, endingLineFeedSymbol);
1112+
}
1113+
10911114
std::string writeString(Value const& root, StreamWriter::Builder const& builder) {
10921115
std::ostringstream sout;
10931116
std::unique_ptr<StreamWriter> const sw(builder.newStreamWriter(&sout));

0 commit comments

Comments
 (0)