mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2025-04-06 10:55:57 +02:00
StreamWriter::Builder
This commit is contained in:
parent
5fbfe3cdb9
commit
489707ff60
@ -22,30 +22,53 @@
|
|||||||
namespace Json {
|
namespace Json {
|
||||||
|
|
||||||
class Value;
|
class Value;
|
||||||
|
class StreamWriterBuilder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
|
||||||
|
using namespace Json;
|
||||||
|
Value value;
|
||||||
|
StreamWriterBuilderFactory f;
|
||||||
|
StreamWriter::Builder builder(&f);
|
||||||
|
builder.setCommentStyle(StreamWriter::CommentStyle::None);
|
||||||
|
std::shared_ptr<StreamWriter> writer(builder.newStreamWriter(&std::cout));
|
||||||
|
writer.write(value);
|
||||||
|
*/
|
||||||
|
class JSON_API StreamWriterBuilderFactory {
|
||||||
|
public:
|
||||||
|
virtual ~StreamWriterBuilderFactory();
|
||||||
|
virtual StreamWriterBuilder* newStreamWriterBuilder() const;
|
||||||
|
};
|
||||||
|
|
||||||
class JSON_API StreamWriter {
|
class JSON_API StreamWriter {
|
||||||
protected:
|
protected:
|
||||||
std::ostream& sout_; // not owned; will not delete
|
std::ostream& sout_; // not owned; will not delete
|
||||||
public:
|
public:
|
||||||
|
enum class CommentStyle {None, Some, All};
|
||||||
|
|
||||||
StreamWriter(std::ostream* sout);
|
StreamWriter(std::ostream* sout);
|
||||||
virtual ~StreamWriter();
|
virtual ~StreamWriter();
|
||||||
/// Write Value into document as configured in sub-class.
|
/// Write Value into document as configured in sub-class.
|
||||||
/// \return zero on success
|
/// \return zero on success
|
||||||
/// \throw std::exception possibly, depending on configuration
|
/// \throw std::exception possibly, depending on configuration
|
||||||
virtual int write(Value const& root) const = 0;
|
virtual int write(Value const& root) const = 0;
|
||||||
};
|
|
||||||
|
|
||||||
class JSON_API StreamWriterBuilder {
|
/// Because this Builder is non-virtual, we can safely add
|
||||||
public:
|
/// methods without a major version bump.
|
||||||
virtual ~StreamWriterBuilder();
|
/// \see http://stackoverflow.com/questions/14875052/pure-virtual-functions-and-binary-compatibility
|
||||||
/// Do not delete stream (i.e. not owned), but keep a reference.
|
class Builder {
|
||||||
virtual StreamWriter* newStreamWriter(std::ostream* stream) const;
|
StreamWriterBuilder* own_;
|
||||||
};
|
public:
|
||||||
|
Builder(StreamWriterBuilderFactory const*);
|
||||||
|
~Builder(); // delete underlying StreamWriterBuilder
|
||||||
|
|
||||||
class JSON_API StreamWriterBuilderFactory {
|
void setCommentStyle(CommentStyle cs); /// default: All
|
||||||
public:
|
|
||||||
virtual ~StreamWriterBuilderFactory();
|
/// Do not take ownership of sout, but maintain a reference.
|
||||||
virtual StreamWriterBuilder* newStreamWriterBuilder();
|
StreamWriter* newStreamWriter(std::ostream* sout);
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
/// \brief Write into stringstream, then return string, for convenience.
|
/// \brief Write into stringstream, then return string, for convenience.
|
||||||
|
@ -696,9 +696,21 @@ int MyStreamWriter::write(Value const& root) const
|
|||||||
sout_ << root;
|
sout_ << root;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
class StreamWriterBuilder {
|
||||||
|
typedef StreamWriter::CommentStyle CommentStyle;
|
||||||
|
CommentStyle cs_;
|
||||||
|
public:
|
||||||
|
virtual ~StreamWriterBuilder();
|
||||||
|
virtual void setCommentStyle(CommentStyle cs);
|
||||||
|
virtual StreamWriter* newStreamWriter(std::ostream* sout) const;
|
||||||
|
};
|
||||||
StreamWriterBuilder::~StreamWriterBuilder()
|
StreamWriterBuilder::~StreamWriterBuilder()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
void StreamWriterBuilder::setCommentStyle(CommentStyle cs)
|
||||||
|
{
|
||||||
|
cs_ = cs;
|
||||||
|
}
|
||||||
StreamWriter* StreamWriterBuilder::newStreamWriter(std::ostream* stream) const
|
StreamWriter* StreamWriterBuilder::newStreamWriter(std::ostream* stream) const
|
||||||
{
|
{
|
||||||
// return new StyledStreamWriter(stream);
|
// return new StyledStreamWriter(stream);
|
||||||
@ -707,10 +719,26 @@ StreamWriter* StreamWriterBuilder::newStreamWriter(std::ostream* stream) const
|
|||||||
StreamWriterBuilderFactory::~StreamWriterBuilderFactory()
|
StreamWriterBuilderFactory::~StreamWriterBuilderFactory()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
StreamWriterBuilder* StreamWriterBuilderFactory::newStreamWriterBuilder()
|
StreamWriterBuilder* StreamWriterBuilderFactory::newStreamWriterBuilder() const
|
||||||
{
|
{
|
||||||
return new StreamWriterBuilder;
|
return new StreamWriterBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StreamWriter::Builder::Builder(StreamWriterBuilderFactory const* f)
|
||||||
|
: own_(f->newStreamWriterBuilder())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
StreamWriter::Builder::~Builder()
|
||||||
|
{
|
||||||
|
delete own_;
|
||||||
|
}
|
||||||
|
void StreamWriter::Builder::setCommentStyle(CommentStyle cs)
|
||||||
|
{
|
||||||
|
own_->setCommentStyle(cs);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Do not take ownership of sout, but maintain a reference.
|
||||||
|
StreamWriter* newStreamWriter(std::ostream* sout);
|
||||||
std::string writeString(Value const& root, StreamWriterBuilder const& builder) {
|
std::string writeString(Value const& root, StreamWriterBuilder const& builder) {
|
||||||
std::ostringstream sout;
|
std::ostringstream sout;
|
||||||
std::unique_ptr<StreamWriter> const sw(builder.newStreamWriter(&sout));
|
std::unique_ptr<StreamWriter> const sw(builder.newStreamWriter(&sout));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user