Orcus
Loading...
Searching...
No Matches
json_document_tree.hpp
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 */
7
8#ifndef INCLUDED_ORCUS_JSON_DOCUMENT_TREE_HPP
9#define INCLUDED_ORCUS_JSON_DOCUMENT_TREE_HPP
10
11#include "env.hpp"
12#include "exception.hpp"
13
14#include <string>
15#include <memory>
16#include <vector>
17#include <cstdint>
18
19namespace orcus {
20
21struct json_config;
22
23namespace json {
24
25struct json_value;
26struct document_resource;
27class document_tree;
28
32class ORCUS_DLLPUBLIC document_error : public general_error
33{
34public:
35 document_error(const std::string& msg);
36 virtual ~document_error();
37};
38
44class ORCUS_DLLPUBLIC key_value_error : public document_error
45{
46public:
47 key_value_error(const std::string& msg);
48 virtual ~key_value_error();
49};
50
51enum class node_t : uint8_t
52{
54 unset = 0,
56 string = 1,
58 number = 2,
63 object = 3,
67 array = 4,
71 boolean_true = 5,
75 boolean_false = 6,
79 null = 7,
80};
81
82namespace detail { namespace init { class node; }}
83
84class const_node;
85class document_tree;
86
87class ORCUS_DLLPUBLIC const_node_iterator
88{
89 friend class const_node;
90
91 struct impl;
92 std::unique_ptr<impl> mp_impl;
93
94 const_node_iterator(const document_tree* doc, const const_node& v, bool begin);
95
96public:
97 const_node_iterator();
98 const_node_iterator(const const_node_iterator& other);
99 ~const_node_iterator();
100
101 const const_node& operator*() const;
102 const const_node* operator->() const;
103
104 const_node_iterator& operator++();
105 const_node_iterator operator++(int);
106
107 const_node_iterator& operator--();
108 const_node_iterator operator--(int);
109
110 bool operator== (const const_node_iterator& other) const;
111 bool operator!= (const const_node_iterator& other) const;
112
113 const_node_iterator& operator= (const const_node_iterator& other);
114};
115
120class ORCUS_DLLPUBLIC const_node
121{
122 friend class document_tree;
123 friend class const_node_iterator;
124 friend class subtree;
125
126 json_value* get_json_value();
127
128protected:
129 struct impl;
130 std::unique_ptr<impl> mp_impl;
131
132 const_node(const document_tree* doc, json_value* jv);
133 const_node(std::unique_ptr<impl>&& p);
134public:
135 const_node();
136 const_node(const const_node& other);
137 const_node(const_node&& rhs);
138 ~const_node();
139
145 node_t type() const;
146
152 size_t child_count() const;
153
161 std::vector<std::string_view> keys() const;
162
177 std::string_view key(size_t index) const;
178
188 bool has_key(std::string_view key) const;
202 const_node child(size_t index) const;
203
214 const_node child(std::string_view key) const;
215
224 const_node parent() const;
225
234 const_node back() const;
235
244 std::string_view string_value() const;
245
254 double numeric_value() const;
255
256 const_node& operator=(const const_node& other);
257 const_node& operator=(const_node&& other);
258
266 uintptr_t identity() const;
267
278 const_node_iterator begin() const;
279
288 const_node_iterator end() const;
289
300 std::string dump(std::size_t indent) const;
301};
302
307class ORCUS_DLLPUBLIC node : public const_node
308{
309 friend class document_tree;
310
311 node(const document_tree* doc, json_value* jv);
312 node(const_node&& rhs);
313
314public:
315 node() = delete;
316
317 node(const node& other);
318 node(node&& rhs);
319 ~node();
320
321 node& operator=(const node& other);
322 node& operator=(const detail::init::node& v);
323 node operator[](std::string_view key);
324
338 node child(size_t index);
339
350 node child(std::string_view key);
351
360 node parent();
361
370 node back();
371
380};
381
386class ORCUS_DLLPUBLIC array
387{
388 friend class detail::init::node;
389 friend class document_tree;
390
391 std::vector<detail::init::node> m_vs;
392public:
393 array();
394 array(const array&) = delete;
395 array(array&& other);
396 array(std::initializer_list<detail::init::node> vs);
397 ~array();
398};
399
404class ORCUS_DLLPUBLIC object
405{
406public:
407 object();
408 object(const object&) = delete;
409 object(object&& other);
410 ~object();
411};
412
413namespace detail { namespace init {
414
420class ORCUS_DLLPUBLIC node
421{
422 friend class ::orcus::json::document_tree;
423 friend class ::orcus::json::node;
424
425 struct impl;
426 std::unique_ptr<impl> mp_impl;
427
428public:
429 node(double v);
430 node(int v);
431 node(bool b);
432 node(std::nullptr_t);
433 node(const char* p);
434 node(const std::string& s);
435 node(std::initializer_list<detail::init::node> vs);
436 node(json::array array);
437 node(json::object obj);
438
439 node(const node& other) = delete;
440 node(node&& other);
441 ~node();
442
443 node& operator= (node other) = delete;
444
445private:
446 node_t type() const;
447 json_value* to_json_value(document_resource& res) const;
448 void store_to_node(document_resource& res, json_value* parent) const;
449};
450
451}}
452
458class ORCUS_DLLPUBLIC document_tree
459{
460 friend class const_node;
461 friend class node;
462
463 struct impl;
464 std::unique_ptr<impl> mp_impl;
465
466 const document_resource& get_resource() const;
467
468public:
469 document_tree();
470 document_tree(const document_tree&) = delete;
471 document_tree(document_tree&& other);
472 document_tree(document_resource& res);
473 document_tree(std::initializer_list<detail::init::node> vs);
474 document_tree(array vs);
475 document_tree(object obj);
476 ~document_tree();
477
478 document_tree& operator= (std::initializer_list<detail::init::node> vs);
479 document_tree& operator= (array vs);
480 document_tree& operator= (object obj);
481
489 void load(std::string_view stream, const json_config& config);
490
497
504
514 std::string dump(std::size_t indent) const;
515
522 std::string dump_xml() const;
523
530 std::string dump_yaml() const;
531
537 void swap(document_tree& other);
538};
539
548class ORCUS_DLLPUBLIC subtree
549{
550 struct impl;
551 std::unique_ptr<impl> mp_impl;
552public:
553
554 subtree();
555
563 subtree(const document_tree& src, std::string_view path);
564 subtree(const subtree&) = delete;
565 subtree(subtree&& other);
566 ~subtree();
567
568 subtree& operator=(subtree&& other);
569
579 std::string dump(std::size_t indent) const;
580};
581
582}}
583
584#endif
585
586/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition json_document_tree.hpp:387
Definition json_document_tree.hpp:121
bool has_key(std::string_view key) const
const_node back() const
std::vector< std::string_view > keys() const
const_node_iterator begin() const
std::string_view key(size_t index) const
const_node child(size_t index) const
std::string_view string_value() const
const_node_iterator end() const
double numeric_value() const
std::string dump(std::size_t indent) const
size_t child_count() const
uintptr_t identity() const
const_node parent() const
const_node child(std::string_view key) const
Definition json_document_tree.hpp:421
Definition json_document_tree.hpp:459
json::node get_document_root()
std::string dump(std::size_t indent) const
std::string dump_yaml() const
void swap(document_tree &other)
json::const_node get_document_root() const
void load(std::string_view stream, const json_config &config)
std::string dump_xml() const
Definition json_document_tree.hpp:308
node child(size_t index)
void push_back(const detail::init::node &v)
node child(std::string_view key)
Definition json_document_tree.hpp:405
std::string dump(std::size_t indent) const
subtree(const document_tree &src, std::string_view path)
Definition config.hpp:20
Definition config.hpp:60