The cbor extension implements decode from and encode to the IETF standard Concise Binary Object Representation (CBOR).
It supports decoding a packed CBOR value to an unpacked (json) value and
encoding an unpacked (json) value to a packed CBOR value. It also supports a set of operations
on a view (cbor_view
) of a packed CBOR value for iterating over and accessing nested data items.
#include <jsoncons/json.hpp>
#include <jsoncons_ext/cbor/cbor.hpp>
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
using namespace jsoncons;
int main()
{
ojson j1 = ojson::parse(R"(
{
"application": "hiking",
"reputons": [
{
"rater": "HikingAsylum.example.com",
"assertion": "is-good",
"rated": "sk",
"rating": 0.90
}
]
}
)");
// Encoding an unpacked (json) value to a packed CBOR value
std::vector<uint8_t> data;
cbor::encode_cbor(j1, data);
// Decoding a packed CBOR value to an unpacked (json) value
ojson j2 = cbor::decode_cbor<ojson>(data);
std::cout << "(1)\n" << pretty_print(j2) << "\n\n";
// Iterating over and accessing the nested data items of a packed CBOR value
cbor::cbor_view datav{data};
cbor::cbor_view reputons = datav.at("reputons");
std::cout << "(2)\n";
for (auto element : reputons.array_range())
{
std::cout << element.at("rated").as_string() << ", ";
std::cout << element.at("rating").as_double() << "\n";
}
std::cout << std::endl;
// Querying a packed CBOR value for a nested data item with jsonpointer
std::error_code ec;
cbor::cbor_view rated = jsonpointer::get(datav, "/reputons/0/rated", ec);
if (!ec)
{
std::cout << "(3) " << rated.as_string() << "\n";
}
Output:
(1)
{
"application": "hiking",
"reputons": [
{
"rater": "HikingAsylum.example.com",
"assertion": "is-good",
"rated": "sk",
"rating": 0.9
}
]
}
(2)
sk, 0.9
(3) sk