1#include <iostream> 2#include <iomanip> 3#include <nlohmann/json.hpp> 4 5using json = nlohmann::json; 6using namespace nlohmann::literals; 7 8// function to print BJData's diagnostic format 9void print_byte(uint8_t byte) 10{ 11 if (32 < byte and byte < 128) 12 { 13 std::cout << (char)byte; 14 } 15 else 16 { 17 std::cout << (int)byte; 18 } 19} 20 21int main() 22{ 23 // create a JSON value 24 json j = R"({"compact": true, "schema": false})"_json; 25 26 // serialize it to BJData 27 std::vector<std::uint8_t> v = json::to_bjdata(j); 28 29 // print the vector content 30 for (auto& byte : v) 31 { 32 print_byte(byte); 33 } 34 std::cout << std::endl; 35 36 // create an array of numbers 37 json array = {1, 2, 3, 4, 5, 6, 7, 8}; 38 39 // serialize it to BJData using default representation 40 std::vector<std::uint8_t> v_array = json::to_bjdata(array); 41 // serialize it to BJData using size optimization 42 std::vector<std::uint8_t> v_array_size = json::to_bjdata(array, true); 43 // serialize it to BJData using type optimization 44 std::vector<std::uint8_t> v_array_size_and_type = json::to_bjdata(array, true, true); 45 46 // print the vector contents 47 for (auto& byte : v_array) 48 { 49 print_byte(byte); 50 } 51 std::cout << std::endl; 52 53 for (auto& byte : v_array_size) 54 { 55 print_byte(byte); 56 } 57 std::cout << std::endl; 58 59 for (auto& byte : v_array_size_and_type) 60 { 61 print_byte(byte); 62 } 63 std::cout << std::endl; 64} 65