17#include <nlohmann/json.hpp>
56 std::string
const &
key()
const {
return _key; }
105 :
tree(t), node(std::make_unique<
Impl::NodeRef>(other.key(), other.get()))
146 v = node->
get().
get<std::string>();
152 return Impl::mkNode(
tree, k, node->get()[k]);
157 return Impl::mkNode(
tree, k, node->get()[k]);
162 return node->get().is_array() || node->get().is_object();
167 return node->get().is_object();
172 return node->get().is_array();
182 if (node.type() == nlohmann::json::value_t::null) {
186 if (node.type() == nlohmann::json::value_t::string) {
187 if (node.get<std::string>() ==
"") {
197 if (node->get().type() == nlohmann::json::value_t::object)
200 if (isResettingPossible(node->get())) {
201 node->
get() = nlohmann::json::object();
203 throw std::runtime_error(
"cannot declare " + this->key() +
" to be of map-type, already of type " +
204 node->get().type_name());
211 if (node->get().type() == nlohmann::json::value_t::array)
214 if (isResettingPossible(node->get())) {
215 node->
get() = nlohmann::json::array();
217 throw std::runtime_error(
"cannot declare " + this->key() +
" to be of seq-type, already of type " +
218 node->get().type_name());
235 switch (node->get().type()) {
236 case nlohmann::json::value_t::string:
return node->get().get<std::string>();
237 case nlohmann::json::value_t::boolean:
return node->get().get<
bool>() ?
"true" :
"false";
238 case nlohmann::json::value_t::number_integer:
return std::to_string(node->get().get<
int>());
239 case nlohmann::json::value_t::number_unsigned:
return std::to_string(node->get().get<
unsigned int>());
240 case nlohmann::json::value_t::number_float:
return std::to_string(node->get().get<
double>());
242 throw std::runtime_error(std::string(
"node " + node->key() +
": implicit string conversion for type " +
243 node->get().type_name() +
" not supported!"));
249 return node->get().get<
int>();
253 return node->get().get<
double>();
257 auto const &nd = node->get();
260 if (nd.type() == nlohmann::json::value_t::number_unsigned) {
261 auto val = nd.get<
unsigned int>();
268 return nd.get<
bool>();
273 return !node->key().empty();
278 return node->get().is_primitive();
283 return node->get().find(
c) != node->get().end();
288 node->
get().push_back(
"");
289 return Impl::mkNode(
tree,
"", node->get().back());
294 return node->get().size();
299 return Impl::mkNode(
tree,
"", node->get().at(pos));
304 return Impl::mkNode(
tree,
"", node->get().at(pos));
310template <
class Nd,
class NdType,
class json_it>
319 std::unique_ptr<typename child_iterator::Impl>
clone()
const override
321 return std::make_unique<ChildItImpl>(
node,
iter);
338 return it && it->
iter == this->
iter;
349 return {
child_iterator(std::make_unique<childIt>(*
this, childIt::POS::BEGIN)),
350 child_iterator(std::make_unique<childIt>(*
this, childIt::POS::END))};
nlohmann::basic_json<>::const_iterator const_json_iterator
nlohmann::basic_json<>::iterator json_iterator
winID h TVirtualViewer3D TVirtualGLPainter p
JSONNode & get(std::string const &key)
std::unique_ptr< typename child_iterator::Impl > clone() const override
ChildItImpl(NdType &n, POS p)
ChildItImpl(const ChildItImpl &other)
bool equal(const typename child_iterator::Impl &other) const override
ChildItImpl(NdType &n, json_it it)
BaseNode(std::istream &is)
const nlohmann::json & get() const override
nlohmann::json & get() override
NodeRef(const std::string &k, nlohmann::json &n)
NodeRef(const NodeRef &other)
const nlohmann::json & get() const override
nlohmann::json & get() override
Impl(const std::string &k)
virtual const nlohmann::json & get() const =0
static TJSONTree::Node & mkNode(TJSONTree *t, const std::string &k, nlohmann::json &n)
virtual nlohmann::json & get()=0
std::string const & key() const
bool is_container() const override
bool has_child(std::string const &) const override
bool val_bool() const override
Node & child(size_t pos) override
const Node & operator>>(std::string &v) const override
Node & operator[](std::string const &k) override
Node & append_child() override
size_t num_children() const override
std::string val() const override
void writeJSON(std::ostream &os) const override
bool has_key() const override
std::string key() const override
Node & set_map() override
double val_double() const override
bool is_seq() const override
std::unique_ptr< Impl > node
Node & set_seq() override
bool has_val() const override
Node(TJSONTree *t, std::istream &is)
const Impl & get_node() const
bool is_map() const override
children_view children() override
Node & operator<<(std::string const &s) override
int val_int() const override
TJSONTree::Node & incache(const TJSONTree::Node &n)
std::list< Node > _nodecache
a class to store JSON values