17#include <nlohmann/json.hpp>
20inline nlohmann::json parseWrapper(std::istream &is)
23 return nlohmann::json::parse(is);
24 }
catch (
const nlohmann::json::exception &
ex) {
25 throw std::runtime_error(
ex.what());
67 std::string
const &
key()
const {
return _key; }
68 virtual nlohmann::json &
get() = 0;
69 virtual const nlohmann::json &
get()
const = 0;
82 nlohmann::json &
get()
override {
return node; }
83 const nlohmann::json &
get()
const override {
return node; }
92 nlohmann::json &
get()
override {
return node; }
93 const nlohmann::json &
get()
const override {
return node; }
116 :
tree(t), node(std::make_unique<
Impl::NodeRef>(other.key(), other.get()))
155 v = node->
get().
get<std::string>();
161 return Impl::mkNode(
tree, k, node->get()[k]);
166 return Impl::mkNode(
tree, k, node->get()[k]);
171 return node->get().is_array() || node->get().is_object();
176 return node->get().is_object();
181 return node->get().is_array();
188bool isResettingPossible(nlohmann::json
const &node)
191 if (node.type() == nlohmann::json::value_t::null) {
195 if (node.type() == nlohmann::json::value_t::string) {
196 if (node.get<std::string>().empty()) {
206 if (node->get().type() == nlohmann::json::value_t::object)
209 if (isResettingPossible(node->get())) {
210 node->
get() = nlohmann::json::object();
212 throw std::runtime_error(
"cannot declare \"" + this->key() +
"\" to be of map - type, already of type " +
213 node->get().type_name());
220 if (node->get().type() == nlohmann::json::value_t::array)
223 if (isResettingPossible(node->get())) {
224 node->
get() = nlohmann::json::array();
226 throw std::runtime_error(
"cannot declare \"" + this->key() +
"\" to be of seq - type, already of type " +
227 node->get().type_name());
244 switch (node->get().type()) {
245 case nlohmann::json::value_t::string:
return node->get().get<std::string>();
246 case nlohmann::json::value_t::boolean:
return node->get().get<
bool>() ?
"true" :
"false";
247 case nlohmann::json::value_t::number_integer:
return std::to_string(node->get().get<
int>());
248 case nlohmann::json::value_t::number_unsigned:
return std::to_string(node->get().get<
unsigned int>());
249 case nlohmann::json::value_t::number_float:
return std::to_string(node->get().get<
double>());
251 throw std::runtime_error(
"node \"" + node->key() +
"\": implicit string conversion for type " +
252 node->get().type_name() +
" not supported!");
258 return node->get().get<
int>();
262 return node->get().get<
double>();
266 auto const &nd = node->get();
269 if (nd.type() == nlohmann::json::value_t::number_unsigned) {
270 auto val = nd.get<
unsigned int>();
277 return nd.get<
bool>();
282 return !node->key().empty();
287 return node->get().is_primitive();
292 return node->get().find(
c) != node->get().end();
297 node->
get().push_back(
"");
298 return Impl::mkNode(
tree,
"", node->get().back());
303 return node->get().size();
308 return Impl::mkNode(
tree,
"", node->get().at(pos));
313 return Impl::mkNode(
tree,
"", node->get().at(pos));
319template <
class Nd,
class NdType,
class json_it>
328 std::unique_ptr<typename child_iterator::Impl>
clone()
const override
330 return std::make_unique<ChildItImpl>(
node,
iter);
347 return it && it->
iter == this->
iter;
358 return {
child_iterator(std::make_unique<childIt>(*
this, childIt::POS::BEGIN)),
359 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