Skip to content

Commit 2cdb7b3

Browse files
committed
Parse nlohmann/json with our SAX handler and the PEGTL
1 parent 0ed03b5 commit 2cdb7b3

File tree

2 files changed

+10729
-0
lines changed

2 files changed

+10729
-0
lines changed

src/test/json/nlohmann.cc

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Copyright (c) 2015-2016 Dr. Colin Hirsch and Daniel Frey
2+
// Please see LICENSE for license or visit https://github.com/taocpp/json/
3+
4+
#include "test.hh"
5+
6+
#include "nlohmann/json.hpp"
7+
8+
namespace tao
9+
{
10+
namespace json
11+
{
12+
namespace
13+
{
14+
// SAX consumer to build a NLohmann/JSON value
15+
class to_nlohmann
16+
{
17+
private:
18+
std::vector< nlohmann::json > stack_;
19+
std::vector< std::string > keys_;
20+
21+
public:
22+
nlohmann::json value;
23+
24+
void null()
25+
{
26+
value = nullptr;
27+
}
28+
29+
void boolean( const bool v )
30+
{
31+
value = v;
32+
}
33+
34+
void number( const std::int64_t v )
35+
{
36+
value = v;
37+
}
38+
39+
void number( const std::uint64_t v )
40+
{
41+
value = v;
42+
}
43+
44+
void number( const double v )
45+
{
46+
value = v;
47+
}
48+
49+
void string( const std::string & v )
50+
{
51+
value = v;
52+
}
53+
54+
void string( std::string && v )
55+
{
56+
value = std::move( v );
57+
}
58+
59+
// array
60+
void begin_array()
61+
{
62+
stack_.push_back( nlohmann::json::array() );
63+
}
64+
65+
void element()
66+
{
67+
stack_.back().push_back( std::move( value ) );
68+
}
69+
70+
void end_array()
71+
{
72+
value = std::move( stack_.back() );
73+
stack_.pop_back();
74+
}
75+
76+
// object
77+
void begin_object()
78+
{
79+
stack_.push_back( nlohmann::json::object() );
80+
}
81+
82+
void key( const std::string & v )
83+
{
84+
keys_.push_back( v );
85+
}
86+
87+
void key( std::string && v )
88+
{
89+
keys_.push_back( std::move( v ) );
90+
}
91+
92+
void member()
93+
{
94+
stack_.back().push_back( nlohmann::json::object_t::value_type( std::move( keys_.back() ), std::move( value ) ) );
95+
keys_.pop_back();
96+
}
97+
98+
void end_object()
99+
{
100+
value = std::move( stack_.back() );
101+
stack_.pop_back();
102+
}
103+
};
104+
105+
} //
106+
107+
void unit_test()
108+
{
109+
// combine a taocpp/json SAX parser (using the PEGTL) with the above
110+
to_nlohmann handler;
111+
sax::from_string( "[ null, true, false, 42, 43.0, \"foo\", [ 1, 2, 3 ], { \"a\" : \"b\", \"c\" : \"d\" } ]", handler );
112+
113+
const auto & v = handler.value;
114+
115+
TEST_ASSERT( v.type() == nlohmann::json::value_t::array );
116+
TEST_ASSERT( v.size() == 8 );
117+
TEST_ASSERT( v[ 0 ] == nullptr );
118+
TEST_ASSERT( v[ 1 ].get< bool >() == true );
119+
TEST_ASSERT( v[ 2 ].get< bool >() == false );
120+
TEST_ASSERT( v[ 3 ] == 42 );
121+
TEST_ASSERT( v[ 4 ] == 43.0 );
122+
TEST_ASSERT( v[ 5 ] == "foo" );
123+
TEST_ASSERT( v[ 6 ].type() == nlohmann::json::value_t::array );
124+
TEST_ASSERT( v[ 6 ].size() == 3 );
125+
TEST_ASSERT( v[ 6 ][ 0 ] == 1 );
126+
TEST_ASSERT( v[ 6 ][ 1 ] == 2 );
127+
TEST_ASSERT( v[ 6 ][ 2 ] == 3 );
128+
TEST_ASSERT( v[ 7 ].type() == nlohmann::json::value_t::object );
129+
TEST_ASSERT( v[ 7 ].size() == 2 );
130+
TEST_ASSERT( v[ 7 ].at( "a" ) == "b" );
131+
TEST_ASSERT( v[ 7 ].at( "c" ) == "d" );
132+
}
133+
134+
} // json
135+
136+
} // tao
137+
138+
#include "main.hh"

0 commit comments

Comments
 (0)