Last active
January 27, 2026 11:09
-
-
Save zaphodikus/e0cb1cd946b70369fb55b2895508132a to your computer and use it in GitHub Desktop.
use jsoncpp to load /de-serialise a class'es data so we can duck-type it's values not rely on hard-coded keys from the json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Load a Json/Yaml file into a duck-typable object not relying on typed in | |
| // dictionary/map keys | |
| // | |
| #include <iostream> | |
| // unused #include <vector> | |
| #include <map> | |
| // unused #include <sstream> | |
| // note add json_reader.cpp, json_value.cpp, jon_write.cpp to your project | |
| #include <json/json.h> | |
| #include <json/reader.h> | |
| #define FMT_HEADER_ONLY 1 | |
| #define FMT_UNICODE 0 | |
| #include <spdlog/fmt/fmt.h> | |
| ///////////////////////////////////////////////////////////////////////////////////////////////////////// | |
| class ArgsBase | |
| { | |
| public: | |
| void loadfrom(std::map<std::string, Json::Value*> variable_map, Json::Value data) { | |
| std::cout << "loadfrom(): data" << std::endl; | |
| if (data.size() > 0) { | |
| for (Json::Value::const_iterator itr = data.begin(); itr != data.end(); itr++) { | |
| // finds using each key and updates it's variable's value | |
| *variable_map[itr.key().asString()] = *itr; | |
| } | |
| }; | |
| } | |
| void print_ent(const char* title, Json::Value value) { std::cout << title << " = " << value << std::endl; } | |
| void print(std::map<std::string, Json::Value*> use_map) { | |
| for (auto x : use_map) | |
| std::cout << x.first << " " << *x.second << std::endl; | |
| } | |
| }; | |
| // deserialised object | |
| class FolderArgs : public ArgsBase | |
| { | |
| public: | |
| Json::Value artifactName; // filename .CSV | |
| Json::Value artifactFolder; // foldername -> CSV file | |
| Json::Value bitmapName; | |
| Json::Value bitmapFolder; | |
| void loadfrom(Json::Value data) { ArgsBase::loadfrom(mymap, data); } | |
| void print() { | |
| ArgsBase::print(mymap); | |
| } | |
| private: | |
| std::map<std::string, Json::Value*> mymap{ {"artifactFolder", &this->artifactFolder}, | |
| {"artifactName", &this->artifactName}, | |
| {"bitmapFolder", &this->bitmapFolder}, | |
| {"bitmapName", &this->bitmapName} }; | |
| }; | |
| ///////////////////////////////////////////////////////////////////////////////////////////////////////// | |
| int main() | |
| { | |
| const std::string rawJson = R"({"bitmapName": "Bitmap Name", | |
| "bitmapFolder": "Bitmap Folder", | |
| "artifactName": "Artifact Name", | |
| "artifactFolder": "Artifact Folder"})"; | |
| const auto rawJsonLength = static_cast<int>(rawJson.length()); | |
| JSONCPP_STRING err; | |
| Json::Value root; | |
| Json::CharReaderBuilder builder; | |
| const std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); | |
| if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root, | |
| &err)) { | |
| std::cout << "error: " << err << std::endl; | |
| return EXIT_FAILURE; | |
| } | |
| FolderArgs folders; | |
| folders.loadfrom(root); | |
| // duck type instead of using root["artifactName"] | |
| std::cout <<folders.artifactName << std::endl; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reddit thread here https://www.reddit.com/r/cpp_questions/comments/1qkwu9s/a_reference_type_cannot_be_valueinitialized/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button