Comments (3)
https://github.com/beached/daw_json_link/blob/release/docs/cookbook/key_values.md should describe it.
from daw_json_link.
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.
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)
- Misspelled or unknown field names... how to detect them? HOT 2
- TemporarilyMutateBuffer::yes produces a compilation error HOT 4
- Parsing of empty string when using `options::LiteralAsStringOpt`
- Taking a param by value and then creating a copy of it HOT 1
- Replace tag dispatch HOT 1
- Error linking daw_json_link with MSVC HOT 7
- Problem serializing some strings HOT 6
- Json Value parsing can produce segmentation faults HOT 2
- Json Value parsing doesn't resolve escaped paths HOT 3
- json_value terminates on type() with exceptions enabled
- Missing contract for std::variant types HOT 4
- Error parsing empty string field when a data contract has custom nullable member: "An unexpected null value was encountered while serializing" HOT 3
- Seeking Advice: implementing MsgPack deserialization HOT 1
- JsonRangeCheck::CheckForNarrowing crashes without raising exception HOT 4
- serialization failure with variant type alias mapping
- lack of daw/daw_constant.h HOT 3
- How to declare recursive self referencing data structure? HOT 10
- daw_is_detected HOT 3
- Support for streams of JSON data and incremental parsing? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from daw_json_link.