@@ -33,190 +33,201 @@ BSONCXX_INLINE_NAMESPACE_BEGIN
3333
3434namespace {
3535
36- class json_visitor {
37- public:
38- json_visitor (std::ostream& out, bool is_array, std::size_t padding)
39- : out(out), stack({is_array}), padding(padding) {}
40-
41- void visit_key (stdx::string_view value) {
42- pad ();
43-
44- if (!stack.back ()) {
45- out << " \" " << value.data () << " \" "
46- << " : " ;
47- }
48- }
49-
50- void visit_value (const types::b_eod&) {}
51-
52- void visit_value (const types::b_double& value) { out << value.value ; }
53-
54- void visit_value (const types::b_utf8& value) { out << " \" " << value.value .data () << " \" " ; }
36+ class json_visitor {
37+ public:
38+ json_visitor (std::ostream& out, bool is_array, std::size_t padding)
39+ : out(out), stack({is_array}), padding(padding) {
40+ }
5541
56- void visit_value (const types::b_document& value) {
57- out << " {" << std::endl;
58- stack.push_back (false );
59- visit_children (value.value );
60- pad ();
61- out << " }" ;
62- }
42+ void visit_key (stdx::string_view value) {
43+ pad ();
6344
64- void visit_value (const types::b_array& value) {
65- out << " [" << std::endl;
66- stack.push_back (true );
67- visit_children (value.value );
68- pad ();
69- out << " ]" ;
45+ if (!stack.back ()) {
46+ out << " \" " << value.data () << " \" "
47+ << " : " ;
7048 }
49+ }
7150
72- void visit_value (const types::b_binary& value) {
73-
74- std::size_t b64_len;
75-
76- b64_len = (value.size / 3 + 1 ) * 4 + 1 ;
77- auto b64 = stdx::make_unique<char []>(b64_len);
78- b64::ntop (value.bytes , value.size , b64.get (), b64_len);
51+ void visit_value (const types::b_eod&) {
52+ }
7953
80- out << " {" << std::endl;
81- pad (1 );
82- out << " \" $type\" : " << to_string (value.sub_type ) << " ," << std::endl;
83- pad (1 );
84- out << " \" $binary\" : " << b64.get () << " ," << std::endl;
85- pad ();
86- out << " }" ;
87- }
54+ void visit_value (const types::b_double& value) {
55+ out << value.value ;
56+ }
8857
89- void visit_value (const types::b_undefined&) {
58+ void visit_value (const types::b_utf8& value) {
59+ out << " \" " << value.value .data () << " \" " ;
60+ }
9061
91- out << " {" << std::endl;
92- pad (1 );
93- out << " \" $undefined\" : true" << std::endl;
94- pad ();
95- out << " }" ;
96- }
62+ void visit_value (const types::b_document& value) {
63+ out << " {" << std::endl;
64+ stack.push_back (false );
65+ visit_children (value.value );
66+ pad ();
67+ out << " }" ;
68+ }
9769
98- void visit_value (const types::b_oid& value) {
70+ void visit_value (const types::b_array& value) {
71+ out << " [" << std::endl;
72+ stack.push_back (true );
73+ visit_children (value.value );
74+ pad ();
75+ out << " ]" ;
76+ }
9977
100- out << " {" << std::endl;
101- pad (1 );
102- out << " \" $oid\" : \" " << value.value << " \" " << std::endl;
103- pad ();
104- out << " }" ;
105- }
78+ void visit_value (const types::b_binary& value) {
79+ std::size_t b64_len;
10680
107- void visit_value (const types::b_bool& value) { out << (value.value ? " true" : " false" ); }
81+ b64_len = (value.size / 3 + 1 ) * 4 + 1 ;
82+ auto b64 = stdx::make_unique<char []>(b64_len);
83+ b64::ntop (value.bytes , value.size , b64.get (), b64_len);
10884
109- void visit_value (const types::b_date& value) {
85+ out << " {" << std::endl;
86+ pad (1 );
87+ out << " \" $type\" : " << to_string (value.sub_type ) << " ," << std::endl;
88+ pad (1 );
89+ out << " \" $binary\" : " << b64.get () << " ," << std::endl;
90+ pad ();
91+ out << " }" ;
92+ }
11093
111- out << " {" << std::endl;
112- pad (1 );
113- out << " \" $date\" : " << value.value << std::endl;
114- pad ();
115- out << " }" ;
116- }
94+ void visit_value (const types::b_undefined&) {
95+ out << " {" << std::endl;
96+ pad (1 );
97+ out << " \" $undefined\" : true" << std::endl;
98+ pad ();
99+ out << " }" ;
100+ }
117101
118- void visit_value (const types::b_null&) { out << " null" ; }
102+ void visit_value (const types::b_oid& value) {
103+ out << " {" << std::endl;
104+ pad (1 );
105+ out << " \" $oid\" : \" " << value.value << " \" " << std::endl;
106+ pad ();
107+ out << " }" ;
108+ }
119109
120- void visit_value (const types::b_regex& value) {
110+ void visit_value (const types::b_bool& value) {
111+ out << (value.value ? " true" : " false" );
112+ }
121113
122- out << " {" << std::endl;
123- pad (1 );
124- out << " \" $regex\" : \" " << value.regex .data () << " \" ," << std::endl;
125- pad ();
126- out << " \" $options\" : \" " << value.options .data () << " \" " << std::endl;
127- pad ();
128- out << " }" ;
129- }
114+ void visit_value (const types::b_date& value) {
115+ out << " {" << std::endl;
116+ pad (1 );
117+ out << " \" $date\" : " << value.value << std::endl;
118+ pad ();
119+ out << " }" ;
120+ }
130121
131- void visit_value (const types::b_dbpointer& value) {
122+ void visit_value (const types::b_null&) {
123+ out << " null" ;
124+ }
132125
133- out << " {" << std::endl;
134- pad (1 );
135- out << " \" $ref\" : \" " << value.collection .data () << " \" " ;
126+ void visit_value (const types::b_regex& value) {
127+ out << " {" << std::endl;
128+ pad (1 );
129+ out << " \" $regex\" : \" " << value.regex .data () << " \" ," << std::endl;
130+ pad ();
131+ out << " \" $options\" : \" " << value.options .data () << " \" " << std::endl;
132+ pad ();
133+ out << " }" ;
134+ }
136135
137- if (value.value ) {
138- out << " ," << std::endl;
139- pad ();
140- out << " \" $id\" : \" " << value.value .to_string ().data () << " \" " << std::endl;
141- }
136+ void visit_value (const types::b_dbpointer& value) {
137+ out << " {" << std::endl;
138+ pad (1 );
139+ out << " \" $ref\" : \" " << value.collection .data () << " \" " ;
142140
141+ if (value.value ) {
142+ out << " ," << std::endl;
143143 pad ();
144- out << " } " ;
144+ out << " \" $id \" : \" " << value. value . to_string (). data () << " \" " << std::endl ;
145145 }
146146
147- void visit_value (const types::b_code& value) { out << value.code .data (); }
148-
149- void visit_value (const types::b_symbol& value) { out << value.symbol .data (); }
150-
151- void visit_value (const types::b_codewscope& value) { out << value.code .data (); }
147+ pad ();
148+ out << " }" ;
149+ }
152150
153- void visit_value (const types::b_int32& value) { out << value.value ; }
151+ void visit_value (const types::b_code& value) {
152+ out << value.code .data ();
153+ }
154154
155- void visit_value (const types::b_timestamp& value) {
155+ void visit_value (const types::b_symbol& value) {
156+ out << value.symbol .data ();
157+ }
156158
157- out << " {" << std::endl;
158- pad (1 );
159- out << " \" $timestamp\" : {" << std::endl;
160- pad (2 );
161- out << " \" $t\" : " << value.timestamp << " ," << std::endl;
162- pad (2 );
163- out << " \" $i\" : " << value.increment << std::endl;
164- pad (1 );
165- out << " }" ;
166- pad ();
167- out << " }" ;
168- }
159+ void visit_value (const types::b_codewscope& value) {
160+ out << value.code .data ();
161+ }
169162
170- void visit_value (const types::b_int64& value) { out << value.value ; }
163+ void visit_value (const types::b_int32& value) {
164+ out << value.value ;
165+ }
171166
172- void visit_value (const types::b_minkey&) {
167+ void visit_value (const types::b_timestamp& value) {
168+ out << " {" << std::endl;
169+ pad (1 );
170+ out << " \" $timestamp\" : {" << std::endl;
171+ pad (2 );
172+ out << " \" $t\" : " << value.timestamp << " ," << std::endl;
173+ pad (2 );
174+ out << " \" $i\" : " << value.increment << std::endl;
175+ pad (1 );
176+ out << " }" ;
177+ pad ();
178+ out << " }" ;
179+ }
173180
174- out << " {" << std::endl;
175- pad (1 );
176- out << " \" $minKey\" : 1" << std::endl;
177- pad ();
178- out << " }" ;
179- }
181+ void visit_value (const types::b_int64& value) {
182+ out << value.value ;
183+ }
180184
181- void visit_value (const types::b_maxkey&) {
185+ void visit_value (const types::b_minkey&) {
186+ out << " {" << std::endl;
187+ pad (1 );
188+ out << " \" $minKey\" : 1" << std::endl;
189+ pad ();
190+ out << " }" ;
191+ }
182192
183- out << " {" << std::endl;
184- pad (1 );
185- out << " \" $maxKey\" : 1" << std::endl;
186- pad ();
187- out << " }" ;
188- }
193+ void visit_value (const types::b_maxkey&) {
194+ out << " {" << std::endl;
195+ pad (1 );
196+ out << " \" $maxKey\" : 1" << std::endl;
197+ pad ();
198+ out << " }" ;
199+ }
189200
190- private:
191- std::ostream& out;
192- std::vector<bool > stack;
193- std::size_t padding;
201+ private:
202+ std::ostream& out;
203+ std::vector<bool > stack;
204+ std::size_t padding;
194205
195- void pad (std::size_t extra = 0 ) {
196- out << std::setw ((stack.size () + -1 + extra + padding) * 4 ) << " " << std::setw (0 );
197- }
206+ void pad (std::size_t extra = 0 ) {
207+ out << std::setw ((stack.size () + -1 + extra + padding) * 4 ) << " " << std::setw (0 );
208+ }
198209
199- void visit_children (const document::view& view) {
200- bool first = true ;
201- for (auto && x : view) {
202- if (!first) {
203- out << " , " << std::endl;
204- }
205- first = false ;
206- visit_key (x.key ());
207- switch (static_cast <int >(x.type ())) {
208- #define BSONCXX_ENUM (name, val ) \
209- case val: \
210- visit_value (x.get_ ##name ()); \
211- break ;
210+ void visit_children (const document::view& view) {
211+ bool first = true ;
212+ for (auto && x : view) {
213+ if (!first) {
214+ out << " , " << std::endl;
215+ }
216+ first = false ;
217+ visit_key (x.key ());
218+ switch (static_cast <int >(x.type ())) {
219+ #define BSONCXX_ENUM (name, val ) \
220+ case val: \
221+ visit_value (x.get_ ##name ()); \
222+ break ;
212223#include < bsoncxx/enums/type.hpp>
213224#undef BSONCXX_ENUM
214- }
215225 }
216- out << std::endl;
217- stack.pop_back ();
218226 }
219- };
227+ out << std::endl;
228+ stack.pop_back ();
229+ }
230+ };
220231
221232} // namespace
222233
@@ -235,11 +246,11 @@ std::string to_json(document::element element) {
235246 json_visitor v (ss, false , 0 );
236247
237248 switch ((int )element.type ()) {
238- #define BSONCXX_ENUM (name, val ) \
239- case val: \
240- v.visit_key (element.key ()); \
241- v.visit_value (element.get_ ##name ()); \
242- break ;
249+ #define BSONCXX_ENUM (name, val ) \
250+ case val: \
251+ v.visit_key (element.key ()); \
252+ v.visit_value (element.get_ ##name ()); \
253+ break ;
243254#include < bsoncxx/enums/type.hpp>
244255#undef BSONCXX_ENUM
245256 }
@@ -253,10 +264,10 @@ std::string to_json(types::value value) {
253264 json_visitor v (ss, false , 0 );
254265
255266 switch ((int )value.type ()) {
256- #define BSONCXX_ENUM (name, val ) \
257- case val: \
258- v.visit_value (value.get_ ##name ()); \
259- break ;
267+ #define BSONCXX_ENUM (name, val ) \
268+ case val: \
269+ v.visit_value (value.get_ ##name ()); \
270+ break ;
260271#include < bsoncxx/enums/type.hpp>
261272#undef BSONCXX_ENUM
262273 }
0 commit comments