17#include "nlohmann/json.hpp"
73 std::string
const &
key()
const {
return _key; }
122 :
tree(t), node(std::make_unique<
Impl::NodeRef>(other.key(), other.get()))
157 v = node->get().get<std::string>();
163 return Impl::mkNode(
tree, k, node->get()[k]);
168 return Impl::mkNode(
tree,
"", node->get()[pos]);
173 return Impl::mkNode(
tree, k, node->get()[k]);
178 return Impl::mkNode(
tree,
"", node->get()[pos]);
183 return node->get().is_array() || node->get().is_object();
188 return node->get().is_object();
193 return node->get().is_array();
203 if (node.type() == nlohmann::json::value_t::null) {
207 if (node.type() == nlohmann::json::value_t::string) {
208 if (node.get<std::string>() ==
"") {
218 if (node->get().type() == nlohmann::json::value_t::object)
221 if (isResettingPossible(node->get())) {
222 node->get() = nlohmann::json::object();
224 throw std::runtime_error(
"cannot declare " + this->key() +
" to be of map-type, already of type " +
225 node->get().type_name());
231 if (node->get().type() == nlohmann::json::value_t::array)
234 if (isResettingPossible(node->get())) {
235 node->get() = nlohmann::json::array();
237 throw std::runtime_error(
"cannot declare " + this->key() +
" to be of seq-type, already of type " +
238 node->get().type_name());
248std::string itoa(
int i)
250 std::stringstream ss;
254std::string ftoa(
float f)
256 std::stringstream ss;
264 switch (node->get().type()) {
265 case nlohmann::json::value_t::string:
return node->get().get<std::string>();
266 case nlohmann::json::value_t::boolean:
return node->get().get<
bool>() ?
"true" :
"false";
267 case nlohmann::json::value_t::number_integer: return ::itoa(node->get().get<
int>());
268 case nlohmann::json::value_t::number_unsigned: return ::itoa(node->get().get<
unsigned int>());
269 case nlohmann::json::value_t::number_float: return ::ftoa(node->get().get<
float>());
271 throw std::runtime_error(std::string(
"node " + node->key() +
": implicit string conversion for type " +
272 node->get().type_name() +
" not supported!"));
278 return node->get().get<
int>();
282 return node->get().get<
float>();
286 auto const &nd = node->get();
289 if (nd.type() == nlohmann::json::value_t::number_unsigned) {
290 auto val = nd.get<
unsigned int>();
297 return nd.get<
bool>();
302 return node->key().size() > 0;
307 return node->get().is_primitive();
312 return node->get().find(
c) != node->get().end();
317 node->get().push_back(
"");
318 return Impl::mkNode(
tree,
"", node->get().back());
323 return node->get().size();
328 return Impl::mkNode(
tree,
"", node->get().at(pos));
333 return Impl::mkNode(
tree,
"", node->get().at(pos));
339template <
class Nd,
class NdType,
class json_it>
348 virtual std::unique_ptr<typename child_iterator::Impl>
clone()
const override
350 return std::make_unique<ChildItImpl>(
node,
iter);
365 return it && it->
iter == this->
iter;
376 return {
child_iterator(std::make_unique<childIt>(*
this, childIt::POS::BEGIN)),
377 child_iterator(std::make_unique<childIt>(*
this, childIt::POS::END))};
nlohmann::basic_json<>::const_iterator const_json_iterator
nlohmann::basic_json<>::iterator json_iterator
virtual Nd & current() override
ChildItImpl(NdType &n, POS p)
virtual void backward() override
virtual std::unique_ptr< typename child_iterator::Impl > clone() const override
ChildItImpl(const ChildItImpl &other)
virtual void forward() override
virtual bool equal(const typename child_iterator::Impl &other) const override
ChildItImpl(NdType &n, json_it it)
BaseNode(std::istream &is)
virtual nlohmann::json & get() override
virtual const nlohmann::json & get() const override
NodeRef(const std::string &k, nlohmann::json &n)
NodeRef(const NodeRef &other)
virtual nlohmann::json & get() override
virtual const nlohmann::json & get() const 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
virtual bool is_container() const override
virtual bool has_child(std::string const &) const override
virtual bool val_bool() const override
virtual Node & child(size_t pos) override
virtual const Node & operator>>(std::string &v) const override
virtual void set_seq() override
children_view children() override
virtual Node & operator[](std::string const &k) override
virtual Node & append_child() override
const TJSONTree * get_tree() const
virtual size_t num_children() const override
virtual std::string val() const override
virtual void writeJSON(std::ostream &os) const override
virtual bool has_key() const override
virtual std::string key() const override
virtual void set_map() override
virtual bool is_seq() const override
virtual bool has_val() const override
virtual float val_float() const override
Node(TJSONTree *t, std::istream &is)
const Impl & get_node() const
virtual bool is_map() const override
virtual Node & operator<<(std::string const &s) override
virtual int val_int() const override
TJSONTree::Node & incache(const TJSONTree::Node &n)
std::list< Node > _nodecache
virtual Node & rootnode() override
basic_json< std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t, double, std::allocator, adl_serializer, std::vector< std::uint8_t > > json