File tree Expand file tree Collapse file tree 2 files changed +80
-3
lines changed Expand file tree Collapse file tree 2 files changed +80
-3
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,35 @@ namespace Json {
23
23
24
24
class Value ;
25
25
26
+ class JSON_API StreamWriter {
27
+ protected:
28
+ std::ostream& sout_; // not owned; will not delete
29
+ public:
30
+ StreamWriter (std::ostream* sout);
31
+ virtual ~StreamWriter ();
32
+ // / Write Value into document as configured in sub-class.
33
+ // / \return zero on success
34
+ // / \throw std::exception possibly, depending on configuration
35
+ virtual int write (Value const & root) const = 0;
36
+ };
37
+
38
+ class JSON_API StreamWriterBuilder {
39
+ public:
40
+ virtual ~StreamWriterBuilder ();
41
+ // / Do not delete stream (i.e. not owned), but keep a reference.
42
+ virtual StreamWriter* newStreamWriter (std::ostream* stream) const ;
43
+ };
44
+
45
+ class JSON_API StreamWriterBuilderFactory {
46
+ public:
47
+ virtual ~StreamWriterBuilderFactory ();
48
+ virtual StreamWriterBuilder* newStreamWriterBuilder ();
49
+ };
50
+
51
+ // / \brief Write into stringstream, then return string, for convenience.
52
+ std::string writeString (Value const & root, StreamWriterBuilder const & builder);
53
+
54
+
26
55
/* * \brief Abstract class for writers.
27
56
*/
28
57
class JSON_API Writer {
Original file line number Diff line number Diff line change 7
7
#include < json/writer.h>
8
8
#include " json_tool.h"
9
9
#endif // if !defined(JSON_IS_AMALGAMATION)
10
+ #include < iomanip>
11
+ #include < memory>
12
+ #include < sstream>
10
13
#include < utility>
11
14
#include < assert.h>
15
+ #include < math.h>
12
16
#include < stdio.h>
13
17
#include < string.h>
14
- #include < sstream>
15
- #include < iomanip>
16
- #include < math.h>
17
18
18
19
#if defined(_MSC_VER) && _MSC_VER < 1500 // VC++ 8.0 and below
19
20
#include < float.h>
@@ -670,4 +671,51 @@ std::ostream& operator<<(std::ostream& sout, const Value& root) {
670
671
return sout;
671
672
}
672
673
674
+ StreamWriter::StreamWriter (std::ostream* sout)
675
+ : sout_(*sout)
676
+ {
677
+ }
678
+ StreamWriter::~StreamWriter ()
679
+ {
680
+ }
681
+ struct MyStreamWriter : public StreamWriter {
682
+ public:
683
+ MyStreamWriter (std::ostream* sout);
684
+ virtual ~MyStreamWriter ();
685
+ virtual int write (Value const & root) const = 0;
686
+ };
687
+ MyStreamWriter::MyStreamWriter (std::ostream* sout)
688
+ : StreamWriter(sout)
689
+ {
690
+ }
691
+ MyStreamWriter::~MyStreamWriter ()
692
+ {
693
+ }
694
+ int MyStreamWriter::write (Value const & root) const
695
+ {
696
+ sout_ << root;
697
+ return 0 ;
698
+ }
699
+ StreamWriterBuilder::~StreamWriterBuilder ()
700
+ {
701
+ }
702
+ StreamWriter* StreamWriterBuilder::newStreamWriter (std::ostream* stream) const
703
+ {
704
+ // return new StyledStreamWriter(stream);
705
+ return nullptr ;
706
+ }
707
+ StreamWriterBuilderFactory::~StreamWriterBuilderFactory ()
708
+ {
709
+ }
710
+ StreamWriterBuilder* StreamWriterBuilderFactory::newStreamWriterBuilder ()
711
+ {
712
+ return new StreamWriterBuilder;
713
+ }
714
+ std::string writeString (Value const & root, StreamWriterBuilder const & builder) {
715
+ std::ostringstream sout;
716
+ std::unique_ptr<StreamWriter> const sw (builder.newStreamWriter (&sout));
717
+ sw->write (root);
718
+ return sout.str ();
719
+ }
720
+
673
721
} // namespace Json
You can’t perform that action at this time.
0 commit comments