@@ -886,19 +886,29 @@ namespace web { namespace json
886
886
// / <returns>If the key exists, a reference to the value kept in the field.</returns>
887
887
json::value& at (const utility::string_t & key)
888
888
{
889
- return const_cast <json::value&>(at_internal (key));
889
+ auto iter = find_by_key (key);
890
+
891
+ if (iter == m_elements.end () || key != (iter->first ))
892
+ throw web::json::json_exception (_XPLATSTR (" Key not found" ));
893
+
894
+ return iter->second ;
890
895
}
891
-
896
+
892
897
// / <summary>
893
898
// / Accesses an element of a JSON object. If the key doesn't exist, this method throws.
894
899
// / </summary>
895
900
// / <param name="key">The key of an element in the JSON object.</param>
896
901
// / <returns>If the key exists, a reference to the value kept in the field.</returns>
897
902
const json::value& at (const utility::string_t & key) const
898
903
{
899
- return at_internal (key);
904
+ auto iter = find_by_key (key);
905
+
906
+ if (iter == m_elements.end () || key != (iter->first ))
907
+ throw web::json::json_exception (_XPLATSTR (" Key not found" ));
908
+
909
+ return iter->second ;
900
910
}
901
-
911
+
902
912
// / <summary>
903
913
// / Accesses an element of a JSON object.
904
914
// / </summary>
@@ -911,7 +921,7 @@ namespace web { namespace json
911
921
if (iter == m_elements.end () || key != (iter->first ))
912
922
return m_elements.insert (iter, std::pair<utility::string_t , value>(key, value ()))->second ;
913
923
914
- return const_cast <json::value&>( iter->second ) ;
924
+ return iter->second ;
915
925
}
916
926
917
927
// / <summary>
@@ -977,6 +987,21 @@ namespace web { namespace json
977
987
}
978
988
}
979
989
990
+ storage_type::iterator find_by_key (const utility::string_t & key)
991
+ {
992
+ if (m_keep_order)
993
+ {
994
+ return std::find_if (m_elements.begin (), m_elements.end (),
995
+ [&key](const std::pair<utility::string_t , value>& p) {
996
+ return p.first == key;
997
+ });
998
+ }
999
+ else
1000
+ {
1001
+ return std::lower_bound (m_elements.begin (), m_elements.end (), key, compare_with_key);
1002
+ }
1003
+ }
1004
+
980
1005
const json::value& at_internal (const utility::string_t & key) const
981
1006
{
982
1007
auto iter = find_by_key (key);
@@ -997,10 +1022,20 @@ namespace web { namespace json
997
1022
return iter;
998
1023
}
999
1024
1025
+ iterator find_internal (const utility::string_t & key)
1026
+ {
1027
+ auto iter = find_by_key (key);
1028
+
1029
+ if (iter != m_elements.end () && key != (iter->first ))
1030
+ return m_elements.end ();
1031
+
1032
+ return iter;
1033
+ }
1034
+
1000
1035
const bool m_keep_order;
1001
1036
storage_type m_elements;
1002
1037
friend class details ::_Object;
1003
-
1038
+
1004
1039
template <typename CharType> friend class json ::details::JSON_Parser;
1005
1040
};
1006
1041
0 commit comments