Advertisement
RobertDeMilo

nlohmann

Apr 28th, 2024 (edited)
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. #include <nlohmann/json.hpp>
  5.  
  6. int main()
  7. {
  8.     std::ifstream jsonFileStream("./settings.json");
  9.     nlohmann::json jsonData = nlohmann::json::parse(jsonFileStream);
  10.  
  11.     //nlohmann::json::parse(R"({"dummy":"lel"})");
  12.    
  13.     // std::ofstream jsonOut("./out.json");
  14.     // jsonOut<< std::setw(4)<<jsonData;
  15.     // std::cout<< std::setw(4)<<jsonData;
  16.    
  17.     std::cout<<"Thread Count: " << jsonData["thread_count"].get<unsigned_int>()<<std::endl;
  18.     std::cout<<"Server Host: " << jsonData["server"]["host"].get<std::string>()<<std::endl;
  19.     std::cout<<"Server Host: " << jsonData["server"]["port"].get<unsigned_int>()<<std::endl;
  20.    
  21.     auto itTC = jsonData.find("thread_count");
  22.    
  23.     if(itTC!=jsonData.end())
  24.     {
  25.         if(itTC.value().is_number_unsigned())
  26.         {
  27.             std::cout<<"Thread count is valid!"<<std::endl;
  28.         }
  29.     }
  30.    
  31.     for(auto it = jsonData.begin();it!=jsonData.end();++it)
  32.     {
  33.         std::cout<<it.key()<<" is "<<it.value()<<std::endl;
  34.     }
  35.    
  36.      nlohmann::json jsonNew;
  37.      jsonNew["a"] = 7;
  38.      jsonNew["b"] = 1.99;
  39.      jsonNew["c"] = false;
  40.      jsonNew["d"] = "Hello World!";
  41.      jsonNew["arr"] = {5,6,"Hello",false, true,1.88};
  42.      jsonNew["obj"] = { {"a", 2}, {"b", "hello"}, {"arr", {1,2,3,4,5}}, {"o",{{"1", false},{"2", true}}}};
  43.      
  44.      jsonNew["earr"] = nlohmann::json::array();
  45.      jsonNew["earr"].push_back(5);
  46.      jsonNew["earr"].push_back("x");
  47.      jsonNew["earr"].push_back("d");
  48.      
  49.      jsonNew["eobj"] = nlohmann::json::object();
  50.      jsonNew["eobj"]["a"] = 1;
  51.      jsonNew["eobj"]["a"] = 10;
  52.    
  53.     return 0;
  54. }
  55.  
  56. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. #include <iostream>
  58. #include <string>
  59. #include <vector>
  60. #include <nlohmann/json.hpp>
  61.  
  62. struct Person
  63. {
  64.   std::string name;  
  65.   int age;
  66. };
  67.  
  68. void to_json(nlohmann::json& j, const Person &p)
  69. {
  70.     j = nlohmann::json{{"name",p.name}, {"age", p.age}};
  71. }
  72.  
  73. void from_json(const nlohmann::json& j, Person &p)
  74. {
  75.      j.at("name").get_to(p.name);
  76.      j.at("age").get_to(p.age);
  77. }
  78.  
  79. int main()
  80. {
  81. // Person p{"Alex", 18};
  82.  
  83. // nlohmann::json j{};
  84. // j["name"] = p.name;
  85. // j["age"] = p.age;
  86.  
  87. // std::cout<<j<<std::endl;
  88.  
  89. // std string serializedString = j.dump();
  90.  
  91. // Person p1{};
  92. // nlohmann::json j1 = nlohmann::json::parse(serializedString);
  93. // p1.name = j1["name"].get<std::string>();
  94. // p1.age = j1["age"].get<int>();
  95.  
  96. Person p{};
  97. nlohmann::json j = p;
  98. std::cout<<j<<<<std::endl;
  99.  
  100. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  101. // struct myAnswer
  102. // {
  103. //     int everything;
  104. // };
  105.  
  106. // struct myObject
  107. // {
  108. //     std::string currency;
  109. //     double value;
  110. // };
  111.  
  112. // struct myStruct
  113. // {
  114. //     float pi;
  115. //     bool happy;
  116. //     std::string name;
  117. //     void * nothing;
  118. //     myAnswer answer;
  119. //     std::vector<int> list;
  120. //     myObject object;
  121. // };
  122.  
  123.  
  124. // int main()
  125. // {
  126.    
  127. //     nlohmann::json j{};
  128. //     j["pi"]=3.141;
  129. //     j["happy"]= true;
  130. //     j["name"]= "Niels";
  131. //     j["nothing"] = nullptr;
  132.  
  133. //     j["answer"]["everything"] =42;
  134. //     j["list"]= {1,0,2};
  135. //     j["object"]= {{"currency","USD"},{"value",42.99}};
  136.  
  137. //     myStruct obj{};
  138. //     obj.pi =      j["pi"].get<float>();
  139. //     obj.happy =   j["happy"].get<bool>();
  140. //     obj.name =    j["name"].get<std::string>();
  141. //     obj.nothing = j["nothing"].get_ptr<nlohmann::json::object_t*>();
  142. //     obj.answer  = myAnswer{j["answer"]["everything"].get<int>()};
  143. //     j["list"].get_to<std::vector<int>>(obj.list);
  144. //     obj.object  = myObject{j["object"]["currency"].get<std::string>(), j["object"]["value"].get<double>() };
  145.  
  146.     return 0;
  147. }
  148.  
  149.  
  150. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  151. #include <iostream>
  152. #include <string>
  153. #include <vector>
  154. #include "dependencies/include/nlohmann/json.hpp"
  155.  
  156. int main()
  157. {
  158. nlohmann::json objJson;
  159.  
  160. std::fstream fileInput;
  161. fileInput.open("json2.json");
  162.  
  163. fileInput>>objJson;
  164. std::cout<<objJson<<std::endl;
  165.  
  166. std::string nameMatrix;
  167. std::string nameVector;
  168. int size;
  169.  
  170. nameMatrix = objJson["configs"][0]["name_matrix"];
  171. nameVector = objJson["configs"][0]["name_vector"];
  172. size = objJson["configs"][0]["size"];
  173.  
  174. std::cout<<nameMatrix<<std::endl;
  175. std::cout<<nameVector<<std::endl;
  176. std::cout<<size<<std::endl;
  177.  
  178. fileInput.close();
  179.  
  180.     return 0;
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement