diff --git a/src/jsoncons/json_structures.hpp b/src/jsoncons/json_structures.hpp index d59bf2af74..8ef36f82d3 100644 --- a/src/jsoncons/json_structures.hpp +++ b/src/jsoncons/json_structures.hpp @@ -186,7 +186,13 @@ class compare_with_string bool operator()(const ValueT& a, const CharT* b) const { size_t len = std::min JSONCONS_NO_MACRO_EXP(a.name().length(),length_); - return std::char_traits::compare(a.name().c_str(),b,len) < 0; + int result = std::char_traits::compare(a.name().data(),b,len); + if (result < 0 || result > 0) + { + return result < 0; + } + + return a.name().length() < length_; } }; diff --git a/test_suite/src/json_accessor_tests.cpp b/test_suite/src/json_accessor_tests.cpp index 4c91c8b8ea..21e2244dc0 100644 --- a/test_suite/src/json_accessor_tests.cpp +++ b/test_suite/src/json_accessor_tests.cpp @@ -41,6 +41,17 @@ BOOST_AUTO_TEST_CASE(test_object_key_proxy) BOOST_CHECK(a.is_null()); } +BOOST_AUTO_TEST_CASE(test_compare_with_string) +{ + json a; + a["key"] = "value"; + a["key1"] = "value1"; + a["key2"] = "value2"; + BOOST_CHECK(a["key"] == a["key"]); + BOOST_CHECK(!(a["key"] == a["key1"])); + BOOST_CHECK(!(a["key"] == a["key2"])); +} + BOOST_AUTO_TEST_CASE(test_count) { json a;