beached / daw_json_link

Fast, convenient JSON serialization and parsing in C++
https://beached.github.io/daw_json_link/
Boost Software License 1.0
460 stars 30 forks source link

How to serialize associative containers? #411

Closed roman-orekhov closed 9 months ago

roman-orekhov commented 9 months ago
#include <daw/json/daw_json_link.h>
#include <fmt/format.h>
#include <map>

using Map = std::map<std::string, std::string>;

namespace daw::json {
template <>
struct json_data_contract<Map::value_type> {
  using type = json_member_list<json_string<"key">, json_string<"value">>;
};
static auto to_json_data(const Map::value_type& v) { return std::forward_as_tuple(v.first, v.second); }

template <>
struct json_data_contract<Map> {
  using type = json_member_list<json_key_value_array<"kv", Map, json_link<"code", std::string>>>;
};
static auto to_json_data(const Map& v) { return std::forward_as_tuple(v); }

}  // namespace daw::json

int main() {
    using namespace daw::json;
    Map map;
    map = from_json_array<Map::value_type, Map>(R"([{"key":"1","value":"2"}])");
    fmt::println("{}", map.at("1"));
    map = from_json<Map>(R"({"kv":[{"key":"1","code":"3"}]})");
    fmt::println("{}", map.at("1"));
    // both fail
    //fmt::println("{}", to_json(map));
    //fmt::println("{}", to_json_array(map));
    return 0;
}

Both to_json and to_json_array fail the same:

to_daw_json_string.h:973:21: error: static assertion failed due to requirement 'is_submember_tagged_variant_v<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::basic_string<char, std::char_traits<char>, std::allocator<char>>>>': Could not find appropriate mapping or to_json_data member of json_data_contract

Serializing std::vector works without hassle.

Version 3.20.1 from vcpkg.

beached commented 9 months ago

https://github.com/beached/daw_json_link/blob/release/docs/cookbook/key_values.md should describe it.

beached commented 9 months ago

I think in your case that

#include <daw/json/daw_json_link.h>
#include <fmt/format.h>

#include <map>

struct Map {
    std::map<std::string, std::string> kv;
};

namespace daw::json {
template <>
struct json_data_contract<Map> {
    using type = json_member_list<json_key_value_array<
        "kv", std::map<std::string, std::string>,
        json_link<"code", std::string>, json_link<"key", std::string>>>;

    static auto to_json_data(Map const& v) {
        return std::forward_as_tuple(v.kv);
    }
};
}  // namespace daw::json

int main() {
    using namespace daw::json;

    using kv_array_map =
        json_key_value_array_no_name<std::map<std::string, std::string>>;
    auto map = from_json<kv_array_map>(R"([{"key":"1","value":"2"}])");
    fmt::println("{}", map.at("1"));
    fmt::println("{}", to_json<kv_array_map>(map));

    auto map2 = from_json<Map>(R"({"kv":[{"key":"1","code":"3"}]})");
    fmt::println("{}", map2.kv.at("1"));
    fmt::println("{}", to_json(map2));
}

https://jsonlink.godbolt.org/z/7sKqo9GWf

Should do it

roman-orekhov commented 9 months ago

Thanks, the main culprit was that my to_json_data functions were declared outside of the templates 😊

Your example of json_key_value_array_no_name is cool but it needs <kv_array_map> wherever I want to use to_json. I tried putting it inside a contract but neither parsing nor serialization compile with the same error:

#include <daw/json/daw_json_link.h>
#include <fmt/format.h>
#include <map>

using Map = std::map<std::string, std::string>;

namespace daw::json {
template <>
struct json_data_contract<Map> {
  using type = json_key_value_array_no_name<Map, json_link<"code", std::string>>;
  static auto to_json_data(const Map& v) { return std::forward_as_tuple(v); }
};

}  // namespace daw::json

int main() {
    using namespace daw::json;
    Map map;
    //map = from_json<Map>(R"([{"key":"1","code":"3"}])");
    fmt::println("{}", map["1"]);
    //fmt::println("{}", to_json(map));
    return 0;
}
daw/json/daw_json_link_types.h:902:42: error: no member named 'result_type' in 'daw::json::json_base::json_key_value_array<std::map<std::basic_string<char>, std::basic_string<char>>, daw::json::json_string< ::daw::json::json_name<5UL>{"code"}>>'
  902 |                                     typename data_contract::template result_type<constructor_t>>>::type;

Searching for json_key_value_array_no_name in repo only found its code definition, no examples. Adding json_member_list erred on not having named elements. Finally I found an example of json_key_value_no_name in gsoc test. So a contract for such thing would require an alias and returning just v from to_json_data instead of the usual tuple. This works:

template <>
struct json_data_contract<Map> {
  using type = json_type_alias<json_key_value_array_no_name<Map, json_link<"code", std::string>>>;
  static auto to_json_data(const Map& v) { return v; }
};

HTH someone. Maybe something from here can end up in various parts of the cookbook/readme.