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
125protected:
126 struct impl;
127 std::unique_ptr<impl> mp_impl;
128
129 const_node(const document_tree* doc, json_value* jv);
130 const_node(std::unique_ptr<impl>&& p);
131public:
132 const_node() = delete;
133
134 const_node(const const_node& other);
135 const_node(const_node&& rhs);
136 ~const_node();
137
143 node_t type() const;
144
150 size_t child_count() const;
151
159 std::vector<std::string_view> keys() const;
160
175 std::string_view key(size_t index) const;
176
186 bool has_key(std::string_view key) const;
200 const_node child(size_t index) const;
201
212 const_node child(std::string_view key) const;
213
222 const_node parent() const;
223
232 const_node back() const;
233
242 std::string_view string_value() const;
243
252 double numeric_value() const;
253
254 const_node& operator=(const const_node& other);
255 const_node& operator=(const_node&& other);
256
264 uintptr_t identity() const;
265
266 const_node_iterator begin() const;
267 const_node_iterator end() const;
268};
269
274class ORCUS_DLLPUBLIC node : public const_node
275{
276 friend class document_tree;
277
278 node(const document_tree* doc, json_value* jv);
279 node(const_node&& rhs);
280
281public:
282 node() = delete;
283
284 node(const node& other);
285 node(node&& rhs);
286 ~node();
287
288 node& operator=(const node& other);
289 node& operator=(const detail::init::node& v);
290 node operator[](std::string_view key);
291
305 node child(size_t index);
306
317 node child(std::string_view key);
318
327 node parent();
328
337 node back();
338
347};
348
353class ORCUS_DLLPUBLIC array
354{
355 friend class detail::init::node;
356 friend class document_tree;
357
358 std::vector<detail::init::node> m_vs;
359public:
360 array();
361 array(const array&) = delete;
362 array(array&& other);
363 array(std::initializer_list<detail::init::node> vs);
364 ~array();
365};
366
371class ORCUS_DLLPUBLIC object
372{
373public:
374 object();
375 object(const object&) = delete;
376 object(object&& other);
377 ~object();
378};
379
380namespace detail { namespace init {
381
387class ORCUS_DLLPUBLIC node
388{
389 friend class ::orcus::json::document_tree;
390 friend class ::orcus::json::node;
391
392 struct impl;
393 std::unique_ptr<impl> mp_impl;
394
395public:
396 node(double v);
397 node(int v);
398 node(bool b);
399 node(std::nullptr_t);
400 node(const char* p);
401 node(const std::string& s);
402 node(std::initializer_list<detail::init::node> vs);
403 node(json::array array);
404 node(json::object obj);
405
406 node(const node& other) = delete;
407 node(node&& other);
408 ~node();
409
410 node& operator= (node other) = delete;
411
412private:
413 node_t type() const;
414 json_value* to_json_value(document_resource& res) const;
415 void store_to_node(document_resource& res, json_value* parent) const;
416};
417
418}}
419
423class ORCUS_DLLPUBLIC document_tree
424{
425 friend class const_node;
426 friend class node;
427
428 struct impl;
429 std::unique_ptr<impl> mp_impl;
430
431 const document_resource& get_resource() const;
432
433public:
434 document_tree();
435 document_tree(const document_tree&) = delete;
436 document_tree(document_tree&& other);
437 document_tree(document_resource& res);
438 document_tree(std::initializer_list<detail::init::node> vs);
439 document_tree(array vs);
440 document_tree(object obj);
441 ~document_tree();
442
443 document_tree& operator= (std::initializer_list<detail::init::node> vs);
444 document_tree& operator= (array vs);
445 document_tree& operator= (object obj);
446
454 void load(std::string_view stream, const json_config& config);
455
462
469
475 std::string dump() const;
476
483 std::string dump_xml() const;
484
491 std::string dump_yaml() const;
492
498 void swap(document_tree& other);
499};
500
501}}
502
503#endif
504
505/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition json_document_tree.hpp:354
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
std::string_view key(size_t index) const
const_node child(size_t index) const
std::string_view string_value() const
double numeric_value() 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:388
Definition json_document_tree.hpp:424
json::node get_document_root()
std::string dump() 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:275
node child(size_t index)
void push_back(const detail::init::node &v)
node child(std::string_view key)
Definition json_document_tree.hpp:372
Definition config.hpp:20
Definition config.hpp:60