Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <nlohmann/json.hpp>
- int main()
- {
- std::ifstream jsonFileStream("./settings.json");
- nlohmann::json jsonData = nlohmann::json::parse(jsonFileStream);
- //nlohmann::json::parse(R"({"dummy":"lel"})");
- // std::ofstream jsonOut("./out.json");
- // jsonOut<< std::setw(4)<<jsonData;
- // std::cout<< std::setw(4)<<jsonData;
- std::cout<<"Thread Count: " << jsonData["thread_count"].get<unsigned_int>()<<std::endl;
- std::cout<<"Server Host: " << jsonData["server"]["host"].get<std::string>()<<std::endl;
- std::cout<<"Server Host: " << jsonData["server"]["port"].get<unsigned_int>()<<std::endl;
- auto itTC = jsonData.find("thread_count");
- if(itTC!=jsonData.end())
- {
- if(itTC.value().is_number_unsigned())
- {
- std::cout<<"Thread count is valid!"<<std::endl;
- }
- }
- for(auto it = jsonData.begin();it!=jsonData.end();++it)
- {
- std::cout<<it.key()<<" is "<<it.value()<<std::endl;
- }
- nlohmann::json jsonNew;
- jsonNew["a"] = 7;
- jsonNew["b"] = 1.99;
- jsonNew["c"] = false;
- jsonNew["d"] = "Hello World!";
- jsonNew["arr"] = {5,6,"Hello",false, true,1.88};
- jsonNew["obj"] = { {"a", 2}, {"b", "hello"}, {"arr", {1,2,3,4,5}}, {"o",{{"1", false},{"2", true}}}};
- jsonNew["earr"] = nlohmann::json::array();
- jsonNew["earr"].push_back(5);
- jsonNew["earr"].push_back("x");
- jsonNew["earr"].push_back("d");
- jsonNew["eobj"] = nlohmann::json::object();
- jsonNew["eobj"]["a"] = 1;
- jsonNew["eobj"]["a"] = 10;
- return 0;
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #include <iostream>
- #include <string>
- #include <vector>
- #include <nlohmann/json.hpp>
- struct Person
- {
- std::string name;
- int age;
- };
- void to_json(nlohmann::json& j, const Person &p)
- {
- j = nlohmann::json{{"name",p.name}, {"age", p.age}};
- }
- void from_json(const nlohmann::json& j, Person &p)
- {
- j.at("name").get_to(p.name);
- j.at("age").get_to(p.age);
- }
- int main()
- {
- // Person p{"Alex", 18};
- // nlohmann::json j{};
- // j["name"] = p.name;
- // j["age"] = p.age;
- // std::cout<<j<<std::endl;
- // std string serializedString = j.dump();
- // Person p1{};
- // nlohmann::json j1 = nlohmann::json::parse(serializedString);
- // p1.name = j1["name"].get<std::string>();
- // p1.age = j1["age"].get<int>();
- Person p{};
- nlohmann::json j = p;
- std::cout<<j<<<<std::endl;
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // struct myAnswer
- // {
- // int everything;
- // };
- // struct myObject
- // {
- // std::string currency;
- // double value;
- // };
- // struct myStruct
- // {
- // float pi;
- // bool happy;
- // std::string name;
- // void * nothing;
- // myAnswer answer;
- // std::vector<int> list;
- // myObject object;
- // };
- // int main()
- // {
- // nlohmann::json j{};
- // j["pi"]=3.141;
- // j["happy"]= true;
- // j["name"]= "Niels";
- // j["nothing"] = nullptr;
- // j["answer"]["everything"] =42;
- // j["list"]= {1,0,2};
- // j["object"]= {{"currency","USD"},{"value",42.99}};
- // myStruct obj{};
- // obj.pi = j["pi"].get<float>();
- // obj.happy = j["happy"].get<bool>();
- // obj.name = j["name"].get<std::string>();
- // obj.nothing = j["nothing"].get_ptr<nlohmann::json::object_t*>();
- // obj.answer = myAnswer{j["answer"]["everything"].get<int>()};
- // j["list"].get_to<std::vector<int>>(obj.list);
- // obj.object = myObject{j["object"]["currency"].get<std::string>(), j["object"]["value"].get<double>() };
- return 0;
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #include <iostream>
- #include <string>
- #include <vector>
- #include "dependencies/include/nlohmann/json.hpp"
- int main()
- {
- nlohmann::json objJson;
- std::fstream fileInput;
- fileInput.open("json2.json");
- fileInput>>objJson;
- std::cout<<objJson<<std::endl;
- std::string nameMatrix;
- std::string nameVector;
- int size;
- nameMatrix = objJson["configs"][0]["name_matrix"];
- nameVector = objJson["configs"][0]["name_vector"];
- size = objJson["configs"][0]["size"];
- std::cout<<nameMatrix<<std::endl;
- std::cout<<nameVector<<std::endl;
- std::cout<<size<<std::endl;
- fileInput.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement