27
27
#include < jsoncons/allocator_set.hpp>
28
28
#include < jsoncons/config/compiler_support.hpp>
29
29
#include < jsoncons/config/version.hpp>
30
+ #include < jsoncons/conv_error.hpp>
31
+ #include < jsoncons/conversion_result.hpp>
30
32
#include < jsoncons/json_array.hpp>
31
33
#include < jsoncons/json_decoder.hpp>
32
34
#include < jsoncons/json_encoder.hpp>
@@ -395,7 +397,6 @@ namespace jsoncons {
395
397
using allocator_type = Allocator;
396
398
397
399
using policy_type = Policy;
398
-
399
400
using char_type = CharT;
400
401
using char_traits_type = std::char_traits<char_type>;
401
402
using string_view_type = jsoncons::basic_string_view<char_type,char_traits_type>;
@@ -3371,8 +3372,10 @@ namespace jsoncons {
3371
3372
}
3372
3373
3373
3374
template <typename IntegerType>
3374
- IntegerType as_integer () const
3375
+ conversion_result< IntegerType> try_as_integer () const
3375
3376
{
3377
+ using result_type = conversion_result<IntegerType>;
3378
+
3376
3379
switch (storage_kind ())
3377
3380
{
3378
3381
case json_storage_kind::short_str:
@@ -3382,27 +3385,38 @@ namespace jsoncons {
3382
3385
auto result = jsoncons::utility::to_integer<IntegerType>(as_string_view ().data (), as_string_view ().length (), val);
3383
3386
if (!result)
3384
3387
{
3385
- JSONCONS_THROW (json_runtime_error<std::runtime_error>(result. error_code (). message ()) );
3388
+ return result_type (unexpect, conv_errc::not_integer );
3386
3389
}
3387
3390
return val;
3388
3391
}
3389
3392
case json_storage_kind::half_float:
3390
- return static_cast <IntegerType>(cast<half_storage>().value ());
3393
+ return result_type ( static_cast <IntegerType>(cast<half_storage>().value () ));
3391
3394
case json_storage_kind::float64:
3392
- return static_cast <IntegerType>(cast<double_storage>().value ());
3395
+ return result_type ( static_cast <IntegerType>(cast<double_storage>().value () ));
3393
3396
case json_storage_kind::int64:
3394
- return static_cast <IntegerType>(cast<int64_storage>().value ());
3397
+ return result_type ( static_cast <IntegerType>(cast<int64_storage>().value () ));
3395
3398
case json_storage_kind::uint64:
3396
- return static_cast <IntegerType>(cast<uint64_storage>().value ());
3399
+ return result_type ( static_cast <IntegerType>(cast<uint64_storage>().value () ));
3397
3400
case json_storage_kind::boolean:
3398
- return static_cast <IntegerType>(cast<bool_storage>().value () ? 1 : 0 );
3401
+ return result_type ( static_cast <IntegerType>(cast<bool_storage>().value () ? 1 : 0 ) );
3399
3402
case json_storage_kind::json_const_reference:
3400
- return cast<json_const_reference_storage>().value ().template as_integer <IntegerType>();
3403
+ return cast<json_const_reference_storage>().value ().template try_as_integer <IntegerType>();
3401
3404
case json_storage_kind::json_reference:
3402
- return cast<json_reference_storage>().value ().template as_integer <IntegerType>();
3405
+ return cast<json_reference_storage>().value ().template try_as_integer <IntegerType>();
3403
3406
default :
3404
- JSONCONS_THROW (json_runtime_error<std::domain_error>(" Not an integer" ));
3407
+ return result_type (unexpect, conv_errc::not_integer);
3408
+ }
3409
+ }
3410
+
3411
+ template <typename IntegerType>
3412
+ IntegerType as_integer () const
3413
+ {
3414
+ auto result = try_as_integer<IntegerType>();
3415
+ if (!result)
3416
+ {
3417
+ JSONCONS_THROW (conv_error (result.error ().code ()));
3405
3418
}
3419
+ return result.value ();
3406
3420
}
3407
3421
3408
3422
template <typename IntegerType>
@@ -3495,8 +3509,10 @@ namespace jsoncons {
3495
3509
}
3496
3510
}
3497
3511
3498
- double as_double () const
3512
+ conversion_result< double > try_as_double () const
3499
3513
{
3514
+ using result_type = conversion_result<double >;
3515
+
3500
3516
switch (storage_kind ())
3501
3517
{
3502
3518
case json_storage_kind::short_str:
@@ -3510,43 +3526,53 @@ namespace jsoncons {
3510
3526
auto result = jsoncons::utility::hexstr_to_double (s, len, x);
3511
3527
if (result.ec == std::errc::invalid_argument)
3512
3528
{
3513
- JSONCONS_THROW (json_runtime_error<std::invalid_argument>( " Not a double " ) );
3529
+ return result_type (unexpect, conv_errc::not_double );
3514
3530
}
3515
3531
}
3516
3532
else if (JSONCONS_UNLIKELY (len > 3 && s[0 ] == ' -' && s[1 ] == ' 0' && (s[2 ] == ' x' || s[2 ] == ' X' )))
3517
3533
{
3518
3534
auto result = jsoncons::utility::hexstr_to_double (s, len, x);
3519
3535
if (result.ec == std::errc::invalid_argument)
3520
3536
{
3521
- JSONCONS_THROW (json_runtime_error<std::invalid_argument>( " Not a double " ) );
3537
+ return result_type (unexpect, conv_errc::not_double );
3522
3538
}
3523
3539
}
3524
3540
else
3525
3541
{
3526
3542
auto result = jsoncons::utility::decstr_to_double (as_cstring (), as_string_view ().length (), x);
3527
3543
if (result.ec == std::errc::invalid_argument)
3528
3544
{
3529
- JSONCONS_THROW (json_runtime_error<std::invalid_argument>( " Not a double " ) );
3545
+ return result_type (unexpect, conv_errc::not_double );
3530
3546
}
3531
3547
}
3532
3548
3533
- return x ;
3549
+ return result_type (x) ;
3534
3550
}
3535
3551
case json_storage_kind::half_float:
3536
- return binary::decode_half (cast<half_storage>().value ());
3552
+ return result_type ( binary::decode_half (cast<half_storage>().value () ));
3537
3553
case json_storage_kind::float64:
3538
- return cast<double_storage>().value ();
3554
+ return result_type ( cast<double_storage>().value () );
3539
3555
case json_storage_kind::int64:
3540
- return static_cast <double >(cast<int64_storage>().value ());
3556
+ return result_type ( static_cast <double >(cast<int64_storage>().value () ));
3541
3557
case json_storage_kind::uint64:
3542
- return static_cast <double >(cast<uint64_storage>().value ());
3558
+ return result_type ( static_cast <double >(cast<uint64_storage>().value () ));
3543
3559
case json_storage_kind::json_const_reference:
3544
- return cast<json_const_reference_storage>().value ().as_double ();
3560
+ return cast<json_const_reference_storage>().value ().try_as_double ();
3545
3561
case json_storage_kind::json_reference:
3546
- return cast<json_reference_storage>().value ().as_double ();
3562
+ return cast<json_reference_storage>().value ().try_as_double ();
3547
3563
default :
3548
- JSONCONS_THROW (json_runtime_error<std::invalid_argument>(" Not a double" ));
3564
+ return result_type (unexpect, conv_errc::not_double);
3565
+ }
3566
+ }
3567
+
3568
+ double as_double () const
3569
+ {
3570
+ auto result = try_as_double ();
3571
+ if (!result)
3572
+ {
3573
+ JSONCONS_THROW (conv_error (result.error ().code ()));
3549
3574
}
3575
+ return result.value ();
3550
3576
}
3551
3577
3552
3578
template <typename SAllocator=std::allocator<char_type>>
0 commit comments