99#include < variant>
1010
1111namespace constexpr_json {
12+ template <typename First, typename Second> struct pair
13+ {
14+ First first;
15+ Second second;
16+ };
17+
1218template <typename T> struct span
1319{
1420 template <std::size_t Size>
@@ -28,7 +34,8 @@ template<typename T> struct span
2834struct json ;
2935
3036using array_t = span<json>;
31- using object_t = span<std::pair<std::string_view, json>>;
37+ using value_pair_t = pair<std::string_view, json>;
38+ using object_t = span<value_pair_t >;
3239using binary_t = span<std::uint8_t >;
3340
3441using data_t = std::
@@ -76,10 +83,7 @@ struct json
7683 {
7784 return other.parent_value_ == parent_value_ && other.index_ == index_;
7885 }
79- constexpr bool operator !=(const iterator &other) const
80- {
81- return !(*this == other);
82- }
86+ constexpr bool operator !=(const iterator &other) const { return !(*this == other); }
8387
8488
8589 constexpr bool operator <(const iterator &other) const
@@ -116,17 +120,12 @@ struct json
116120
117121 constexpr iterator cend () const { return end (); }
118122
119- constexpr std::size_t size () const noexcept {
120- if (is_null ()) {
121- return 0 ;
122- }
123- if (is_object ()) {
124- return std::get_if<object_t >(&data)->size ();
125- }
123+ constexpr std::size_t size () const noexcept
124+ {
125+ if (is_null ()) { return 0 ; }
126+ if (is_object ()) { return std::get_if<object_t >(&data)->size (); }
126127
127- if (is_array ()) {
128- return std::get_if<array_t >(&data)->size ();
129- }
128+ if (is_array ()) { return std::get_if<array_t >(&data)->size (); }
130129
131130 return 1 ;
132131 }
@@ -185,16 +184,28 @@ struct json
185184
186185 constexpr double as_number_float () const
187186 {
188- if (const double *value = std::get_if<double >(&data); value != nullptr ) { return *value; }
187+ if (const double *value = std::get_if<double >(&data); value != nullptr ) {
188+ return *value;
189+ } else {
190+ throw std::runtime_error (" Not a float type" );
191+ }
189192 }
190193 constexpr double as_boolean () const
191194 {
192- if (const bool *value = std::get_if<bool >(&data); value != nullptr ) { return *value; }
195+ if (const bool *value = std::get_if<bool >(&data); value != nullptr ) {
196+ return *value;
197+ } else {
198+ throw std::runtime_error (" Not a boolean type" );
199+ }
193200 }
194201
195202 constexpr std::string_view as_string () const
196203 {
197- if (const auto *value = std::get_if<std::string_view>(&data); value != nullptr ) { return *value; }
204+ if (const auto *value = std::get_if<std::string_view>(&data); value != nullptr ) {
205+ return *value;
206+ } else {
207+ throw std::runtime_error (" Not a string type" );
208+ }
198209 }
199210
200211 constexpr operator double () const { return as_number_float (); }
@@ -219,7 +230,6 @@ struct json
219230 data_t data;
220231};
221232
222- using value_pair_t = std::pair<std::string_view, json>;
223233}// namespace constexpr_json
224234
225235#endif
0 commit comments