@@ -434,18 +434,46 @@ namespace web { namespace json
434
434
CASABLANCA_DEPRECATED (" This API is deprecated and will be removed in a future release, use operator[] instead." )
435
435
value get (const utility::string_t &key) const ;
436
436
437
+ // / <summary>
438
+ // / Accesses an element of a JSON array. Throws when index out of bounds.
439
+ // / </summary>
440
+ // / <param name="index">The index of an element in the JSON array.</param>
441
+ // / <returns>A reference to the value.</returns>
442
+ _ASYNCRTIMP json::value& at (size_t index);
443
+
444
+ // / <summary>
445
+ // / Accesses an element of a JSON array. Throws when index out of bounds.
446
+ // / </summary>
447
+ // / <param name="index">The index of an element in the JSON array.</param>
448
+ // / <returns>A reference to the value.</returns>
449
+ _ASYNCRTIMP const json::value& at (size_t index) const ;
450
+
451
+ // / <summary>
452
+ // / Accesses an element of a JSON object. If the key doesn't exist, this method throws.
453
+ // / </summary>
454
+ // / <param name="key">The key of an element in the JSON object.</param>
455
+ // / <returns>If the key exists, a reference to the value.</returns>
456
+ _ASYNCRTIMP json::value& at (const utility::string_t & key);
457
+
458
+ // / <summary>
459
+ // / Accesses an element of a JSON object. If the key doesn't exist, this method throws.
460
+ // / </summary>
461
+ // / <param name="key">The key of an element in the JSON object.</param>
462
+ // / <returns>If the key exists, a reference to the value.</returns>
463
+ _ASYNCRTIMP const json::value& at (const utility::string_t & key) const ;
464
+
437
465
// / <summary>
438
466
// / Accesses a field of a JSON object.
439
467
// / </summary>
440
468
// / <param name="key">The name of the field</param>
441
- // / <returns>A reference to the value kept in the field</returns>
469
+ // / <returns>A reference to the value kept in the field. </returns>
442
470
_ASYNCRTIMP value & operator [] (const utility::string_t &key);
443
471
444
472
// / <summary>
445
473
// / Accesses a field of a JSON object.
446
474
// / </summary>
447
475
// / <param name="key">The name of the field</param>
448
- // / <returns>A reference to the value kept in the field</returns>
476
+ // / <returns>A reference to the value kept in the field. </returns>
449
477
_ASYNCRTIMP const value & operator [] (const utility::string_t &key) const ;
450
478
451
479
#ifdef _MS_WINDOWS
@@ -471,16 +499,9 @@ namespace web { namespace json
471
499
// / Accesses an element of a JSON array.
472
500
// / </summary>
473
501
// / <param name="index">The index of an element in the JSON array.</param>
474
- // / <returns>A reference to the value kept in the field</returns>
502
+ // / <returns>A reference to the value kept in the field. </returns>
475
503
_ASYNCRTIMP value & operator [] (size_t index);
476
504
477
- // / <summary>
478
- // / Accesses an element of a JSON array.
479
- // / </summary>
480
- // / <param name="index">The index of an element in the JSON array.</param>
481
- // / <returns>A reference to the value kept in the field</returns>
482
- _ASYNCRTIMP const value & operator [] (size_t index) const ;
483
-
484
505
private:
485
506
friend class web ::json::details::_Object;
486
507
friend class web ::json::details::_Array;
@@ -512,6 +533,26 @@ namespace web { namespace json
512
533
#endif
513
534
};
514
535
536
+ // / <summary>
537
+ // / A single exception type to represent errors in parsing, converting, and accessing
538
+ // / elements of JSON values.
539
+ // / </summary>
540
+ class json_exception : public std ::exception
541
+ {
542
+ private:
543
+ std::string _message;
544
+ public:
545
+ json_exception () {}
546
+ json_exception (const utility::char_t * const &message) : _message(utility::conversions::to_utf8string(message)) { }
547
+
548
+ // Must be narrow string because it derives from std::exception
549
+ const char * what () const _noexcept
550
+ {
551
+ return _message.c_str ();
552
+ }
553
+ ~json_exception () _noexcept {}
554
+ };
555
+
515
556
// / <summary>
516
557
// / A JSON array represented as a C++ class.
517
558
// / </summary>
@@ -641,22 +682,44 @@ namespace web { namespace json
641
682
}
642
683
643
684
// / <summary>
644
- // / Accesses an element of a JSON array.
685
+ // / Accesses an element of a JSON array. Throws when index out of bounds.
645
686
// / </summary>
646
687
// / <param name="index">The index of an element in the JSON array.</param>
647
- // / <returns>A reference to the value kept in the field</returns>
648
- json::value& operator [] (size_type index)
688
+ // / <returns>A reference to the value kept in the field. </returns>
689
+ json::value& at (size_type index)
649
690
{
691
+ if (index >= m_elements.size ())
692
+ throw json_exception (_XPLATSTR (" index out of bounds" ));
693
+
694
+ return m_elements[index];
695
+ }
696
+
697
+ // / <summary>
698
+ // / Accesses an element of a JSON array. Throws when index out of bounds.
699
+ // / </summary>
700
+ // / <param name="index">The index of an element in the JSON array.</param>
701
+ // / <returns>A reference to the value kept in the field.</returns>
702
+ const json::value& at (size_type index) const
703
+ {
704
+ if (index >= m_elements.size ())
705
+ throw json_exception (_XPLATSTR (" index out of bounds" ));
706
+
650
707
return m_elements[index];
651
708
}
652
709
653
710
// / <summary>
654
711
// / Accesses an element of a JSON array.
655
712
// / </summary>
656
713
// / <param name="index">The index of an element in the JSON array.</param>
657
- // / <returns>A const reference to the value kept in the field</returns>
658
- const json::value& operator [](size_type index) const
714
+ // / <returns>A reference to the value kept in the field. </returns>
715
+ json::value& operator [](size_type index)
659
716
{
717
+ SafeInt<size_type> nMinSize (index);
718
+ nMinSize += 1 ;
719
+ SafeInt<size_type> nlastSize (m_elements.size ());
720
+ if (nlastSize < nMinSize)
721
+ m_elements.resize (nMinSize);
722
+
660
723
return m_elements[index];
661
724
}
662
725
@@ -676,26 +739,6 @@ namespace web { namespace json
676
739
template <typename CharType> friend class json ::details::JSON_Parser;
677
740
};
678
741
679
- // / <summary>
680
- // / A single exception type to represent errors in parsing, converting, and accessing
681
- // / elements of JSON values.
682
- // / </summary>
683
- class json_exception : public std ::exception
684
- {
685
- private:
686
- std::string _message;
687
- public:
688
- json_exception () {}
689
- json_exception (const utility::char_t * const &message) : _message(utility::conversions::to_utf8string(message)) { }
690
-
691
- // Must be narrow string because it derives from std::exception
692
- const char * what () const _noexcept
693
- {
694
- return _message.c_str ();
695
- }
696
- ~json_exception () _noexcept {}
697
- };
698
-
699
742
// / <summary>
700
743
// / A JSON object represented as a C++ class.
701
744
// / </summary>
@@ -832,10 +875,40 @@ namespace web { namespace json
832
875
return m_elements.crend ();
833
876
}
834
877
878
+ // / <summary>
879
+ // / Accesses an element of a JSON object. If the key doesn't exist, this method throws.
880
+ // / </summary>
881
+ // / <param name="key">The key of an element in the JSON object.</param>
882
+ // / <returns>If the key exists, a reference to the value kept in the field.</returns>
883
+ json::value& at (const utility::string_t & key)
884
+ {
885
+ auto iter = std::lower_bound (m_elements.begin (), m_elements.end (), key, compare_with_key);
886
+
887
+ if (iter == m_elements.end () || key != (iter->first ))
888
+ throw web::json::json_exception (_XPLATSTR (" Key not found" ));
889
+
890
+ return iter->second ;
891
+ }
892
+
893
+ // / <summary>
894
+ // / Accesses an element of a JSON object. If the key doesn't exist, this method throws.
895
+ // / </summary>
896
+ // / <param name="key">The key of an element in the JSON object.</param>
897
+ // / <returns>If the key exists, a reference to the value kept in the field.</returns>
898
+ const json::value& at (const utility::string_t & key) const
899
+ {
900
+ auto iter = std::lower_bound (m_elements.begin (), m_elements.end (), key, compare_with_key);
901
+
902
+ if (iter == m_elements.end () || key != (iter->first ))
903
+ throw web::json::json_exception (_XPLATSTR (" Key not found" ));
904
+
905
+ return iter->second ;
906
+ }
907
+
835
908
// / <summary>
836
909
// / Accesses an element of a JSON object.
837
910
// / </summary>
838
- // / <param name="index ">The key of an element in the JSON object.</param>
911
+ // / <param name="key ">The key of an element in the JSON object.</param>
839
912
// / <returns>If the key exists, a reference to the value kept in the field, otherwise a newly created null value that will be stored for the given key.</returns>
840
913
json::value& operator [](const utility::string_t & key)
841
914
{
@@ -851,7 +924,7 @@ namespace web { namespace json
851
924
// / Gets an iterator to an element of a JSON object.
852
925
// / </summary>
853
926
// / <param name="key">The key of an element in the JSON object.</param>
854
- // / <returns>A const iterator to the value kept in the field</returns>
927
+ // / <returns>A const iterator to the value kept in the field. </returns>
855
928
const_iterator find (const utility::string_t & key) const
856
929
{
857
930
auto iter = std::lower_bound (m_elements.begin (), m_elements.end (), key, compare_with_key);
@@ -866,7 +939,7 @@ namespace web { namespace json
866
939
// / Gets an iterator to an element of a JSON object.
867
940
// / </summary>
868
941
// / <param name="key">The key of an element in the JSON object.</param>
869
- // / <returns>An iterator to the value kept in the field</returns>
942
+ // / <returns>An iterator to the value kept in the field. </returns>
870
943
iterator find (const utility::string_t & key)
871
944
{
872
945
auto iter = std::lower_bound (m_elements.begin (), m_elements.end (), key, compare_with_key);
@@ -1483,30 +1556,8 @@ namespace web { namespace json
1483
1556
virtual json::array& as_array () { return m_array; }
1484
1557
virtual const json::array& as_array () const { return m_array; }
1485
1558
1486
- virtual value get_element (array::size_type index) const
1487
- {
1488
- SafeInt<array::size_type> idx (index);
1489
- SafeInt<array::size_type> size (m_array.size ());
1490
- return (idx >= size) ? value () : m_array[index];
1491
- }
1492
-
1493
1559
virtual json::value &index (json::array::size_type index)
1494
1560
{
1495
- SafeInt<json::array::size_type> nMinSize (index);
1496
- nMinSize += 1 ;
1497
- SafeInt<json::array::size_type> nlastSize (m_array.size ());
1498
- if (nlastSize < nMinSize)
1499
- m_array.m_elements .resize (nMinSize);
1500
-
1501
- return m_array.m_elements [index];
1502
- }
1503
-
1504
- virtual const json::value &cnst_index (array::size_type index) const
1505
- {
1506
- SafeInt<array::size_type> idx (index);
1507
- SafeInt<array::size_type> size (m_array.size ());
1508
- if ( idx >= size )
1509
- throw json_exception (_XPLATSTR (" index out of bounds" ));
1510
1561
return m_array[index];
1511
1562
}
1512
1563
0 commit comments