Advertisement
avr39ripe

cppFollyDynamicExample

Mar 6th, 2022
998
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1.     folly::dynamic json{
  2.         folly::dynamic::object ("desc", folly::dynamic::object ("id", 10)("name","John"))("data", folly::dynamic::array (42,33,26))
  3.     };
  4.  
  5.     std::string jsonStr{ folly::toJson (json) };
  6.     std::cout << "JSON: " << jsonStr << '\n';
  7.  
  8.     folly::dynamic fromJson{ folly::parseJson (jsonStr) };
  9.     auto dynData{ fromJson["data"] };
  10.  
  11.     int id{ fromJson["desc"]["id"].asInt () };
  12.     std::string name{ fromJson["desc"]["name"].asString () };
  13.     std::array<int, 3> data;
  14.     auto dataIter{ data.begin () };
  15.  
  16.     //std::copy (dynData.begin (), dynData.end (), data.begin ());
  17.     std::for_each (dynData.begin (), dynData.end (), [&dataIter] (folly::dynamic item) { *dataIter++ = item.asInt (); /*std::cout << item.asInt () << ' ';*/ });
  18.     std::cout << '\n';
  19.  
  20.     std::cout << "ID: " << id << '\n';
  21.     std::cout << "NAME: " << name << '\n';
  22.  
  23.     std::cout << "DATA: ";
  24.  
  25.     for (const auto val : data)
  26.     {
  27.         std::cout << val << ' ';
  28.     }
  29.  
  30.     std::cout << '\n';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement