operator_serialize.cpp 549 B

123456789101112131415161718192021
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <nlohmann/json.hpp>
  4. using json = nlohmann::json;
  5. int main()
  6. {
  7. // create JSON values
  8. json j_object = {{"one", 1}, {"two", 2}};
  9. json j_array = {1, 2, 4, 8, 16};
  10. // serialize without indentation
  11. std::cout << j_object << "\n\n";
  12. std::cout << j_array << "\n\n";
  13. // serialize with indentation
  14. std::cout << std::setw(4) << j_object << "\n\n";
  15. std::cout << std::setw(2) << j_array << "\n\n";
  16. std::cout << std::setw(1) << std::setfill('\t') << j_object << "\n\n";
  17. }