44#include " test.hh"
55
66#include " nlohmann/json.hpp"
7+ #include " nlohmann/to_value.hh"
8+ #include " nlohmann/traverse_value.hh"
79
810#include < tao/json/sax/from_string.hh>
911#include < tao/json/sax/to_stream.hh>
@@ -12,175 +14,34 @@ namespace tao
1214{
1315 namespace json
1416 {
15- namespace
16- {
17- // SAX consumer to build an nlohmann/json value
18- class to_nlohmann
19- {
20- private:
21- std::vector< nlohmann::json > stack_;
22- std::vector< std::string > keys_;
23-
24- public:
25- nlohmann::json value;
26-
27- void null ()
28- {
29- value = nullptr ;
30- }
31-
32- void boolean ( const bool v )
33- {
34- value = v;
35- }
36-
37- void number ( const std::int64_t v )
38- {
39- value = v;
40- }
41-
42- void number ( const std::uint64_t v )
43- {
44- value = v;
45- }
46-
47- void number ( const double v )
48- {
49- value = v;
50- }
51-
52- void string ( const std::string & v )
53- {
54- value = v;
55- }
56-
57- void string ( std::string && v )
58- {
59- value = std::move ( v );
60- }
61-
62- // array
63- void begin_array ()
64- {
65- stack_.push_back ( nlohmann::json::array () );
66- }
67-
68- void element ()
69- {
70- stack_.back ().push_back ( std::move ( value ) );
71- }
72-
73- void end_array ()
74- {
75- value = std::move ( stack_.back () );
76- stack_.pop_back ();
77- }
78-
79- // object
80- void begin_object ()
81- {
82- stack_.push_back ( nlohmann::json::object () );
83- }
84-
85- void key ( const std::string & v )
86- {
87- keys_.push_back ( v );
88- }
89-
90- void key ( std::string && v )
91- {
92- keys_.push_back ( std::move ( v ) );
93- }
94-
95- void member ()
96- {
97- stack_.back ().push_back ( nlohmann::json::object_t::value_type ( std::move ( keys_.back () ), std::move ( value ) ) );
98- keys_.pop_back ();
99- }
100-
101- void end_object ()
102- {
103- value = std::move ( stack_.back () );
104- stack_.pop_back ();
105- }
106- };
107-
108- // SAX producer for an nlohmann/json value
109- template < typename Handler >
110- void traverse_nlohmann ( const nlohmann::json & v, Handler & handler )
111- {
112- switch ( v.type () ) {
113- case nlohmann::json::value_t ::null:
114- handler.null ();
115- break ;
116- case nlohmann::json::value_t ::boolean:
117- handler.boolean ( v.get < bool >() );
118- break ;
119- case nlohmann::json::value_t ::number_integer:
120- handler.number ( v.get < std::int64_t >() );
121- break ;
122- case nlohmann::json::value_t ::number_unsigned:
123- handler.number ( v.get < std::uint64_t >() );
124- break ;
125- case nlohmann::json::value_t ::number_float:
126- handler.number ( v.get < double >() );
127- break ;
128- case nlohmann::json::value_t ::string:
129- handler.string ( v.get_ref < const std::string & >() );
130- break ;
131- case nlohmann::json::value_t ::array:
132- handler.begin_array ();
133- for ( const auto & e : v ) {
134- traverse_nlohmann ( e, handler );
135- handler.element ();
136- }
137- handler.end_array ();
138- break ;
139- case nlohmann::json::value_t ::object:
140- handler.begin_object ();
141- for ( nlohmann::json::const_iterator it = v.begin (); it != v.end (); ++it ) {
142- handler.key ( it.key () );
143- traverse_nlohmann ( it.value (), handler );
144- handler.member ();
145- }
146- handler.end_object ();
147- break ;
148- default :
149- throw std::logic_error ( " invalid value for nohmann::json::type()" ); // LCOV_EXCL_LINE
150- }
151- }
152-
153- } //
154-
15517 void unit_test ()
15618 {
157- // combine a taocpp/json SAX parser (using the PEGTL) with the above
158- to_nlohmann handler;
159- sax::from_string ( " [ null, true, false, 42, 43.0, \" foo\" , [ 1, 2, 3 ], { \" a\" : \" b\" , \" c\" : \" d\" } ]" , handler );
19+ tao::json::nlohmann::to_value handler;
20+ tao::json::sax::from_string ( " [ null, true, false, 42, 43.0, \" foo\" , [ 1, 2, 3 ], { \" a\" : \" b\" , \" c\" : \" d\" } ]" , handler );
16021
16122 const auto & v = handler.value ;
16223
163- TEST_ASSERT ( v.type () == nlohmann::json::value_t ::array );
24+ TEST_ASSERT ( v.type () == :: nlohmann::json::value_t ::array );
16425 TEST_ASSERT ( v.size () == 8 );
16526 TEST_ASSERT ( v[ 0 ] == nullptr );
16627 TEST_ASSERT ( v[ 1 ].get < bool >() == true );
16728 TEST_ASSERT ( v[ 2 ].get < bool >() == false );
16829 TEST_ASSERT ( v[ 3 ] == 42 );
16930 TEST_ASSERT ( v[ 4 ] == 43.0 );
17031 TEST_ASSERT ( v[ 5 ] == " foo" );
171- TEST_ASSERT ( v[ 6 ].type () == nlohmann::json::value_t ::array );
32+ TEST_ASSERT ( v[ 6 ].type () == :: nlohmann::json::value_t ::array );
17233 TEST_ASSERT ( v[ 6 ].size () == 3 );
17334 TEST_ASSERT ( v[ 6 ][ 0 ] == 1 );
17435 TEST_ASSERT ( v[ 6 ][ 1 ] == 2 );
17536 TEST_ASSERT ( v[ 6 ][ 2 ] == 3 );
176- TEST_ASSERT ( v[ 7 ].type () == nlohmann::json::value_t ::object );
37+ TEST_ASSERT ( v[ 7 ].type () == :: nlohmann::json::value_t ::object );
17738 TEST_ASSERT ( v[ 7 ].size () == 2 );
17839 TEST_ASSERT ( v[ 7 ].at ( " a" ) == " b" );
17940 TEST_ASSERT ( v[ 7 ].at ( " c" ) == " d" );
18041
18142 std::ostringstream oss;
182- sax::to_stream oss_handler ( oss );
183- traverse_nlohmann ( v, oss_handler );
43+ tao::json:: sax::to_stream oss_handler ( oss );
44+ tao::json::nlohmann::traverse_value ( v, oss_handler );
18445
18546 TEST_ASSERT ( oss.str () == " [null,true,false,42,43.0,\" foo\" ,[1,2,3],{\" a\" :\" b\" ,\" c\" :\" d\" }]" );
18647 }
0 commit comments