Coder Social home page Coder Social logo

Comments (3)

beached avatar beached commented on June 11, 2024

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

from daw_json_link.

beached avatar beached commented on June 11, 2024

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

from daw_json_link.

roman-orekhov avatar roman-orekhov commented on June 11, 2024

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.

from daw_json_link.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.