Skip to content

Commit 29f12b6

Browse files
committed
Fixed issues with TFS merge on linux
1 parent ca9bf70 commit 29f12b6

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

Release/include/cpprest/json.h

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -886,19 +886,29 @@ namespace web { namespace json
886886
/// <returns>If the key exists, a reference to the value kept in the field.</returns>
887887
json::value& at(const utility::string_t& key)
888888
{
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;
890895
}
891-
896+
892897
/// <summary>
893898
/// Accesses an element of a JSON object. If the key doesn't exist, this method throws.
894899
/// </summary>
895900
/// <param name="key">The key of an element in the JSON object.</param>
896901
/// <returns>If the key exists, a reference to the value kept in the field.</returns>
897902
const json::value& at(const utility::string_t& key) const
898903
{
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;
900910
}
901-
911+
902912
/// <summary>
903913
/// Accesses an element of a JSON object.
904914
/// </summary>
@@ -911,7 +921,7 @@ namespace web { namespace json
911921
if (iter == m_elements.end() || key != (iter->first))
912922
return m_elements.insert(iter, std::pair<utility::string_t, value>(key, value()))->second;
913923

914-
return const_cast<json::value&>(iter->second);
924+
return iter->second;
915925
}
916926

917927
/// <summary>
@@ -977,6 +987,21 @@ namespace web { namespace json
977987
}
978988
}
979989

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+
9801005
const json::value& at_internal(const utility::string_t& key) const
9811006
{
9821007
auto iter = find_by_key(key);
@@ -997,10 +1022,20 @@ namespace web { namespace json
9971022
return iter;
9981023
}
9991024

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+
10001035
const bool m_keep_order;
10011036
storage_type m_elements;
10021037
friend class details::_Object;
1003-
1038+
10041039
template<typename CharType> friend class json::details::JSON_Parser;
10051040
};
10061041

0 commit comments

Comments
 (0)