to_bson.cpp 466 B

123456789101112131415161718192021
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <nlohmann/json.hpp>
  4. using json = nlohmann::json;
  5. int main()
  6. {
  7. // create a JSON value
  8. json j = R"({"compact": true, "schema": 0})"_json;
  9. // serialize it to BSON
  10. std::vector<uint8_t> v = json::to_bson(j);
  11. // print the vector content
  12. for (auto& byte : v)
  13. {
  14. std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << (int)byte << " ";
  15. }
  16. std::cout << std::endl;
  17. }