It might be nice to add std::hash support for boost::json types to enable their use in associative containers as keys. Currently the library only provides std::hash support for boost::json::string. For the full support, std::hash specializations for other constructs should be added.
namespace std {
template <>
struct hash<boost::json::array> {
size_t operator()(const boost::json::array& jv) const;
};
template <>
struct hash<boost::json::object> {
size_t operator()(const boost::json::object& jv) const;
};
template <>
struct hash<boost::json::value> {
size_t operator()(const boost::json::value& jv) const;
};
} // namespace std
Then, in my implementation, I noticed that boost::json::object preserves the insertion order when iterating over key_value_pairs as my test case below failed initially.
TEST_CASE("Boost Json Object Hash") {
auto r1 = std::hash<boost::json::value>{}(
boost::json::value({{"a", 11}, {"b", 12}}));
auto r2 = std::hash<boost::json::value>{}(
boost::json::value({{"b", 12}, {"a", 11}}));
CHECK_EQ(r1, r2);
}
Currently, when calculating hashes for boost::json::objects, I additionally sort pairs with respect to keys beforehand but wonder if this feature can be implemented better and added to the library.
Thank you!
It might be nice to add std::hash support for
boost::jsontypes to enable their use in associative containers as keys. Currently the library only provides std::hash support forboost::json::string. For the full support, std::hash specializations for other constructs should be added.Then, in my implementation, I noticed that
boost::json::objectpreserves the insertion order when iterating overkey_value_pairs as my test case below failed initially.Currently, when calculating hashes for
boost::json::objects, I additionally sort pairs with respect to keys beforehand but wonder if this feature can be implemented better and added to the library.Thank you!