@@ -196,57 +196,57 @@ namespace json
196
196
*
197
197
* @return true if the JSON value is null, false otherwise.
198
198
*/
199
- [[nodiscard]] bool is_null () const noexcept { return value.index () == 0 ; }
199
+ [[nodiscard]] constexpr bool is_null () const noexcept { return value.index () == 0 ; }
200
200
/* *
201
201
* @brief Checks if the JSON value is a boolean.
202
202
*
203
203
* @return true if the JSON value is a boolean, false otherwise.
204
204
*/
205
- [[nodiscard]] bool is_boolean () const noexcept { return value.index () == 1 ; }
205
+ [[nodiscard]] constexpr bool is_boolean () const noexcept { return value.index () == 1 ; }
206
206
/* *
207
207
* @brief Checks if the JSON value is an integer.
208
208
*
209
209
* @return true if the JSON value is an integer, false otherwise.
210
210
*/
211
- [[nodiscard]] bool is_integer () const noexcept { return value.index () == 2 ; }
211
+ [[nodiscard]] constexpr bool is_integer () const noexcept { return value.index () == 2 ; }
212
212
/* *
213
213
* @brief Checks if the JSON value is an unsigned integer.
214
214
*
215
215
* @return true if the JSON value is an unsigned integer, false otherwise.
216
216
*/
217
- [[nodiscard]] bool is_unsigned () const noexcept { return value.index () == 3 ; }
217
+ [[nodiscard]] constexpr bool is_unsigned () const noexcept { return value.index () == 3 ; }
218
218
/* *
219
219
* @brief Checks if the JSON value is a floating-point number.
220
220
*
221
221
* @return true if the JSON value is a floating-point number, false otherwise.
222
222
*/
223
- [[nodiscard]] bool is_float () const noexcept { return value.index () == 4 ; }
223
+ [[nodiscard]] constexpr bool is_float () const noexcept { return value.index () == 4 ; }
224
224
/* *
225
225
* @brief Checks if the JSON value is a number.
226
226
*
227
227
* This function checks if the JSON value is of type integer, unsigned integer, or floating-point number.
228
228
*
229
229
* @return true if the JSON value is a number, false otherwise.
230
230
*/
231
- [[nodiscard]] bool is_number () const noexcept { return value.index () == 2 || value.index () == 3 || value.index () == 4 ; }
231
+ [[nodiscard]] constexpr bool is_number () const noexcept { return value.index () == 2 || value.index () == 3 || value.index () == 4 ; }
232
232
/* *
233
233
* @brief Checks if the JSON value is a string.
234
234
*
235
235
* @return true if the JSON value is a string, false otherwise.
236
236
*/
237
- [[nodiscard]] bool is_string () const noexcept { return value.index () == 5 ; }
237
+ [[nodiscard]] constexpr bool is_string () const noexcept { return value.index () == 5 ; }
238
238
/* *
239
239
* @brief Checks if the JSON value is an object.
240
240
*
241
241
* @return true if the JSON value is an object, false otherwise.
242
242
*/
243
- [[nodiscard]] bool is_object () const noexcept { return value.index () == 6 ; }
243
+ [[nodiscard]] constexpr bool is_object () const noexcept { return value.index () == 6 ; }
244
244
/* *
245
245
* @brief Checks if the JSON value is an array.
246
246
*
247
247
* @return true if the JSON value is an array, false otherwise.
248
248
*/
249
- [[nodiscard]] bool is_array () const noexcept { return value.index () == 7 ; }
249
+ [[nodiscard]] constexpr bool is_array () const noexcept { return value.index () == 7 ; }
250
250
251
251
/* *
252
252
* @brief Checks if the JSON value is a primitive type.
@@ -256,15 +256,15 @@ namespace json
256
256
*
257
257
* @return true if the JSON value is a primitive type, false otherwise.
258
258
*/
259
- [[nodiscard]] bool is_primitive () const noexcept { return is_null () || is_boolean () || is_integer () || is_unsigned () || is_float () || is_string (); }
259
+ [[nodiscard]] constexpr bool is_primitive () const noexcept { return is_null () || is_boolean () || is_integer () || is_unsigned () || is_float () || is_string (); }
260
260
/* *
261
261
* @brief Checks if the JSON value is a structured type.
262
262
*
263
263
* This function checks if the JSON value is either an object or an array.
264
264
*
265
265
* @return true if the JSON value is a structured type (object or array), false otherwise.
266
266
*/
267
- [[nodiscard]] bool is_structured () const noexcept { return is_object () || is_array (); }
267
+ [[nodiscard]] constexpr bool is_structured () const noexcept { return is_object () || is_array (); }
268
268
269
269
/* *
270
270
* @brief Returns the type of the JSON value.
@@ -274,7 +274,7 @@ namespace json
274
274
*
275
275
* @return The type of the JSON value.
276
276
*/
277
- [[nodiscard]] json_type get_type () const noexcept
277
+ [[nodiscard]] constexpr json_type get_type () const noexcept
278
278
{
279
279
switch (value.index ())
280
280
{
@@ -326,6 +326,8 @@ namespace json
326
326
*/
327
327
[[nodiscard]] bool contains (std::string_view key) const { return is_object () && std::get<std::map<std::string, json, std::less<>>>(value).count (key) > 0 ; }
328
328
329
+ [[nodiscard]] bool operator ==(std::nullptr_t ) const noexcept { return value.index () == 0 ; }
330
+
329
331
template <typename T, std::enable_if_t <std::is_arithmetic_v<T>, int > = 0 >
330
332
[[nodiscard]] bool operator ==(T num) const noexcept
331
333
{
@@ -347,7 +349,9 @@ namespace json
347
349
template <typename T, std::enable_if_t <std::is_same_v<std::decay_t <T>, std::string> || std::is_same_v<std::decay_t <T>, std::string_view> || std::is_same_v<std::decay_t <T>, const char *>, int > = 0 >
348
350
[[nodiscard]] bool operator ==(T &&str) const noexcept { return value.index () == 5 && std::get<std::string>(value) == std::string (str); }
349
351
350
- template <typename T, std::enable_if_t <std::is_arithmetic_v<T> && !std::is_same_v<std::decay_t <T>, bool >, int > = 0 >
352
+ [[nodiscard]] bool operator !=(std::nullptr_t ) const noexcept { return value.index () != 0 ; }
353
+
354
+ template <typename T, std::enable_if_t <std::is_arithmetic_v<T>, int > = 0 >
351
355
[[nodiscard]] bool operator !=(T num) const noexcept
352
356
{
353
357
switch (value.index ())
@@ -368,6 +372,37 @@ namespace json
368
372
template <typename T, std::enable_if_t <std::is_same_v<std::decay_t <T>, std::string> || std::is_same_v<std::decay_t <T>, std::string_view> || std::is_same_v<std::decay_t <T>, const char *>, int > = 0 >
369
373
[[nodiscard]] bool operator !=(T &&str) const noexcept { return value.index () != 5 || std::get<std::string>(value) != std::string (str); }
370
374
375
+ /* *
376
+ * @brief Get the JSON value as the specified type T.
377
+ *
378
+ * This templated function provides explicit conversion of the JSON value to type T.
379
+ * Use this when you want to be explicit about the conversion or when implicit
380
+ * conversion operators might be ambiguous.
381
+ *
382
+ * @tparam T The type to convert the JSON value to.
383
+ * @return The JSON value converted to type T.
384
+ */
385
+ template <typename T>
386
+ [[nodiscard]] T get () const noexcept
387
+ {
388
+ if constexpr (std::is_same_v<T, bool >)
389
+ return static_cast <bool >(*this );
390
+ else if constexpr (std::is_integral_v<T> && std::is_signed_v<T>)
391
+ return static_cast <int64_t >(*this );
392
+ else if constexpr (std::is_integral_v<T> && std::is_unsigned_v<T>)
393
+ return static_cast <uint64_t >(*this );
394
+ else if constexpr (std::is_floating_point_v<T>)
395
+ return static_cast <double >(*this );
396
+ else if constexpr (std::is_same_v<T, std::string> || std::is_same_v<T, std::string_view> || std::is_same_v<T, const char *>)
397
+ return static_cast <std::string>(*this );
398
+ else if constexpr (std::is_same_v<T, std::map<std::string, json, std::less<>>>)
399
+ return static_cast <std::map<std::string, json, std::less<>>>(*this );
400
+ else if constexpr (std::is_same_v<T, std::vector<json>>)
401
+ return static_cast <std::vector<json>>(*this );
402
+ else
403
+ static_assert (std::is_arithmetic_v<T> || std::is_same_v<T, std::string> || std::is_same_v<T, std::string_view> || std::is_same_v<T, const char *> || std::is_same_v<T, std::map<std::string, json, std::less<>>> || std::is_same_v<T, std::vector<json>>, " Unsupported type for json::get<T>()" );
404
+ }
405
+
371
406
/* *
372
407
* @brief Conversion operator to bool.
373
408
*
@@ -387,7 +422,7 @@ namespace json
387
422
case 3 :
388
423
return std::get<uint64_t >(value) != 0 ;
389
424
case 4 :
390
- return ! std::get<double >(value);
425
+ return std::get<double >(value) != 0.0 ;
391
426
case 5 :
392
427
return !std::get<std::string>(value).empty ();
393
428
case 6 :
0 commit comments