diff --git a/test.json b/test.json new file mode 100644 index 0000000..ab3b3fb --- /dev/null +++ b/test.json @@ -0,0 +1,10 @@ +[ + { + "Name": "Test", + "Val": "TestVal" + }, + { + "Name": "Test2", + "Val": "TestVal2" + } +] \ No newline at end of file diff --git a/trying_nlohman_json b/trying_nlohman_json new file mode 100755 index 0000000..a0c7aff Binary files /dev/null and b/trying_nlohman_json differ diff --git a/trying_nlohman_json.cpp b/trying_nlohman_json.cpp new file mode 100644 index 0000000..054aca4 --- /dev/null +++ b/trying_nlohman_json.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include + +using json = nlohmann::json; + +struct Test { + std::string Name; + std::string Val; +}; + + void from_json(const json& j, Test& p) { + j.at("Name").get_to(p.Name); + j.at("Val").get_to(p.Val); +} + +template +void from_json(const json& j, T (&t)[N]) { + if (j.size()!=N) { + throw std::runtime_error("size!"); + } + size_t index=0; + for (auto& item : j) { + from_json(item, t[index++]); + } +} +int main() { + std::ifstream f("test.json"); + json data = json::parse(f); + Test my_array[2]; + from_json(data, my_array); + std::cout<