1#ifndef IPFS_JSON_CBOR_ADAPTER_H_
2#define IPFS_JSON_CBOR_ADAPTER_H_
4#include <ipfs_client/dag_cbor_value.h>
5#include <ipfs_client/dag_json_value.h>
10#if __has_include(<nlohmann/json.hpp>)
12#include <nlohmann/json.hpp>
13#define HAS_JSON_CBOR_ADAPTER 1
17class JsonCborAdapter final :
public DagCborValue,
public DagJsonValue {
22 JsonCborAdapter(nlohmann::json
const& data) : data_(data) {}
23 std::unique_ptr<DagCborValue> at(std::string_view k)
const override {
24 if (data_.is_object() && data_.contains(k)) {
25 return std::make_unique<JsonCborAdapter>(data_.at(k));
29 std::unique_ptr<DagJsonValue> operator[](std::string_view k)
const override {
30 if (data_.is_object() && data_.contains(k)) {
31 return std::make_unique<JsonCborAdapter>(data_[k]);
35 std::optional<std::uint64_t> as_unsigned()
const override {
36 if (data_.is_number_unsigned()) {
37 return data_.get<std::uint64_t>();
41 std::optional<std::int64_t> as_signed()
const override {
42 if (data_.is_number_integer()) {
43 return data_.get<std::int64_t>();
44 }
else if (
auto ui = as_unsigned()) {
45 if (*ui <= std::numeric_limits<std::int64_t>::max()) {
46 return static_cast<std::int64_t
>(*ui);
51 std::optional<double> as_float()
const override {
52 if (data_.is_number_float()) {
53 return data_.get<
double>();
57 std::optional<std::string> as_string()
const override {
58 if (data_.is_string()) {
59 return data_.get<std::string>();
63 std::optional<std::string> get_if_string()
const override {
66 std::optional<bool> as_bool()
const override {
67 if (data_.is_boolean()) {
68 return data_.get<
bool>();
72 std::optional<std::vector<std::uint8_t>> as_bytes()
const override {
73 if (data_.is_binary()) {
74 return data_.get_binary();
78 std::optional<Cid> as_link()
const override {
79 if (!data_.is_binary()) {
82 auto& bin = data_.get_binary();
83 if (!bin.has_subtype() || bin.subtype() != 42) {
92 auto p =
reinterpret_cast<std::byte const*
>(bin.data()) + 1UL;
93 Cid from_binary(ByteView{p, bin.size() - 1UL});
94 if (from_binary.valid()) {
100 bool is_map()
const override {
return data_.is_object();}
101 bool is_array()
const override {
return data_.is_array();}
102 void iterate_map(MapElementCallback cb)
const override {
106 for (
auto& [k,v] : data_.items()) {
107 JsonCborAdapter el(v);
111 void iterate_array(ArrayElementCallback cb)
const override {
115 for (
auto& v : data_) {
116 JsonCborAdapter el(v);
120 std::string pretty_print()
const override {
121 std::ostringstream result;
122 result << std::setw(2) << data_;
125 std::optional<std::vector<std::string>> object_keys()
const override {
126 if (!data_.is_object()) {
129 std::vector<std::string> rv;
130 for (
auto& [k, v] : data_.items()) {
136 std::function<
void(DagJsonValue
const&)> cb)
const override {
137 if (!data_.is_array()) {
140 for (
auto& v : data_) {
141 JsonCborAdapter wrap(v);