Flow123d  3.9.0-72cb42cb8
path_json.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file path_json.hh
15  * @brief
16  */
17 
18 #ifndef PATH_JSON_HH_
19 #define PATH_JSON_HH_
20 
21 #include <memory>
22 #include <stdint.h> // for int64_t
23 #include <iosfwd> // for ostream, istream
24 #include <set> // for set
25 #include <string> // for string
26 #include <vector> // for vector
27 #include "input/json_spirit/json_spirit_value.h" // for mValue
28 #include "input/path_base.hh"
29 //#include "json_spirit/json_spirit.h"
30 
31 
32 namespace Input {
33 
34 
35 
36 /**
37  * @brief Class used by ReaderToStorage class to iterate over the JSON tree provided by json_spirit library.
38  *
39  * This class keeps whole path from the root of the JSON tree to the current node. We store nodes along path in \p nodes_
40  * and address of the node in \p path_.
41  *
42  * The class also contains methods for processing of special keys 'REF' and 'TYPE'. The reference is record with only one key
43  * 'REF' with a string value that contains address of the reference. The string with the address is extracted and provided by
44  * method \p JSONtoStorage::find_ref_node.
45  */
46 class PathJSON : public PathBase {
47 public:
48 
49 
50  /**
51  * @brief Constructor.
52  *
53  * Call JSON parser for given stream and create PathJSON for the root
54  * of parsed data tree.
55  */
56  PathJSON(std::istream &in);
57 
58  /**
59  * @brief Destructor.
60  *
61  * Have to cleanup nodes_.
62  */
63  ~PathJSON() override;
64 
65  /**
66  * @brief Dive into json_spirit hierarchy.
67  *
68  * Implements @p PathBase::down(unsigned int)
69  */
70  bool down(unsigned int index) override;
71 
72  /**
73  * @brief Dive into json_spirit hierarchy.
74  *
75  * Implements @p PathBase::down(const std::string&)
76  */
77  bool down(const std::string& key, int index = -1) override;
78 
79  /// Return one level up in the hierarchy.
80  void up() override;
81 
82  /// Implements @p PathBase::level
83  inline int level() const
84  { return nodes_.size() - 1; }
85 
86  // These methods are derived from PathBase
87  bool is_null_type() const override; ///< Implements @p PathBase::is_null_type
88  bool get_bool_value() const override; ///< Implements @p PathBase::get_bool_value
89  std::int64_t get_int_value() const override; ///< Implements @p PathBase::get_int_value
90  double get_double_value() const override; ///< Implements @p PathBase::get_double_value
91  std::string get_string_value() const override; ///< Implements @p PathBase::get_string_value
92  unsigned int get_node_type_index() const override; ///< Implements @p PathBase::get_node_type_index
93  bool get_record_key_set(std::set<std::string> &) const override; ///< Implements @p PathBase::get_record_key_set
94  bool is_effectively_null() const override; ///< Implements @p PathBase::is_effectively_null
95  int get_array_size() const override; ///< Implements @p PathBase::get_array_size
96  bool is_record_type() const override; ///< Implements @p PathBase::is_record_type
97  bool is_array_type() const override; ///< Implements @p PathBase::is_array_type
98  PathJSON * clone() const override; ///< Implements @p PathBase::clone
99 
100  /// Implements @p PathBase::get_record_tag
101  std::string get_record_tag() const override;
102 
103  /// Implements reading of reference keys, and check of cyclic references.
104  PathBase * find_ref_node() override;
105 
106  /// Put address of actual reference to previous_references_ set
107  void remember_reference();
108 
109 
110 
111 protected:
112 
113  /**
114  * @brief Default constructor.
115  *
116  * Provides common initialization for public constructors.
117  */
118  PathJSON();
119 
120  /// Definition of JSON Spirit node.
122 
123  /// Pointer to JSON Value object at current path.
124  inline const Node * head() const
125  { return nodes_.back(); }
126 
127  /**
128  * @brief Remember used references in order to avoid detect cyclic references.
129  *
130  * In JSON we allow usage of references using special key 'REF'.
131  */
132  std::set<std::string> previous_references_;
133 
134  // Root node has to be automatically deleted.
135  std::shared_ptr<Node> root_node_;
136 
137  // Pointers to all nodes from the root up to the current path.
139 
140 };
141 
142 /**
143  * @brief Output operator for PathJSON.
144  *
145  * Mainly for debugging purposes and error messages.
146  */
147 std::ostream& operator<<(std::ostream& stream, const PathJSON& path);
148 
149 
150 
151 } // namespace Input
152 
153 
154 
155 #endif /* PATH_JSON_HH_ */
Input::PathJSON::get_record_tag
std::string get_record_tag() const override
Implements PathBase::get_record_tag.
Definition: path_json.cc:300
Input::PathJSON::is_null_type
bool is_null_type() const override
Implements PathBase::is_null_type.
Definition: path_json.cc:196
Input::PathJSON::Node
json_spirit::mValue Node
Definition of JSON Spirit node.
Definition: path_json.hh:121
Input::PathJSON::is_array_type
bool is_array_type() const override
Implements PathBase::is_array_type.
Definition: path_json.cc:288
Input::PathJSON::level
int level() const
Implements PathBase::level.
Definition: path_json.hh:83
Input
Abstract linear system class.
Definition: balance.hh:40
Input::PathJSON
Class used by ReaderToStorage class to iterate over the JSON tree provided by json_spirit library.
Definition: path_json.hh:46
Input::PathJSON::find_ref_node
PathBase * find_ref_node() override
Implements reading of reference keys, and check of cyclic references.
Definition: path_json.cc:120
Input::operator<<
std::ostream & operator<<(std::ostream &stream, const Address &address)
Definition: accessors.hh:252
Input::PathBase
Base abstract class used by ReaderToStorage class to iterate over the input tree.
Definition: path_base.hh:41
Input::PathJSON::nodes_
std::vector< const Node * > nodes_
Definition: path_json.hh:138
std::vector
Definition: doxy_dummy_defs.hh:7
Input::PathJSON::down
bool down(unsigned int index) override
Dive into json_spirit hierarchy.
Definition: path_json.cc:79
json_spirit::Value_impl
Definition: json_spirit_value.h:38
Input::PathJSON::get_double_value
double get_double_value() const override
Implements PathBase::get_double_value.
Definition: path_json.cc:223
Input::PathJSON::is_record_type
bool is_record_type() const override
Implements PathBase::is_record_type.
Definition: path_json.cc:282
Input::PathJSON::up
void up() override
Return one level up in the hierarchy.
Definition: path_json.cc:107
Input::PathJSON::get_int_value
std::int64_t get_int_value() const override
Implements PathBase::get_int_value.
Definition: path_json.cc:212
Input::PathJSON::~PathJSON
~PathJSON() override
Destructor.
Definition: path_json.cc:74
Input::PathJSON::remember_reference
void remember_reference()
Put address of actual reference to previous_references_ set.
Definition: path_json.cc:323
Input::PathJSON::head
const Node * head() const
Pointer to JSON Value object at current path.
Definition: path_json.hh:124
Input::PathJSON::get_string_value
std::string get_string_value() const override
Implements PathBase::get_string_value.
Definition: path_json.cc:236
Input::PathJSON::get_node_type_index
unsigned int get_node_type_index() const override
Implements PathBase::get_node_type_index.
Definition: path_json.cc:247
path_base.hh
Input::PathJSON::get_record_key_set
bool get_record_key_set(std::set< std::string > &) const override
Implements PathBase::get_record_key_set.
Definition: path_json.cc:253
Input::PathJSON::is_effectively_null
bool is_effectively_null() const override
Implements PathBase::is_effectively_null.
Definition: path_json.cc:329
Input::PathJSON::root_node_
std::shared_ptr< Node > root_node_
Definition: path_json.hh:135
json_spirit_value.h
Input::PathJSON::get_array_size
int get_array_size() const override
Implements PathBase::get_array_size.
Definition: path_json.cc:271
Input::PathJSON::PathJSON
PathJSON()
Default constructor.
Definition: path_json.cc:56
Input::PathJSON::previous_references_
std::set< std::string > previous_references_
Remember used references in order to avoid detect cyclic references.
Definition: path_json.hh:132
Input::PathJSON::clone
PathJSON * clone() const override
Implements PathBase::clone.
Definition: path_json.cc:294
Input::PathJSON::get_bool_value
bool get_bool_value() const override
Implements PathBase::get_bool_value.
Definition: path_json.cc:201