My Project
JsonObject.hpp
1 /*
2  Copyright 2013 Statoil ASA.
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef JSON_OBJECT_HPP
21 #define JSON_OBJECT_HPP
22 
23 #include <filesystem>
24 #include <string>
25 
26 #include <opm/common/utility/FileSystem.hpp>
27 
28 struct cJSON;
29 
30 namespace Json {
31 
32  class JsonObject {
33  public:
34  JsonObject();
35 
36  explicit JsonObject(const std::filesystem::path& jsonFile );
37  explicit JsonObject(const std::string& inline_json);
38  explicit JsonObject(const char * inline_json);
39  explicit JsonObject(cJSON * root);
40  ~JsonObject();
41 
42  void add(double value);
43  void add(int value);
44  void add(const std::string& value);
45  JsonObject add_array();
46  JsonObject add_object();
47  void add_item(const std::string& key, double value);
48  void add_item(const std::string& key, int value);
49  void add_item(const std::string& key, const std::string& value);
50  JsonObject add_array(const std::string& key);
51  JsonObject add_object(const std::string& key);
52  std::string dump() const;
53 
54  bool has_item(const std::string& key) const;
55  JsonObject get_array_item( size_t index ) const;
56  JsonObject get_item(const std::string& key) const;
57 
58  std::string to_string() const;
59  std::string get_string(const std::string& key) const;
60  std::string as_string() const;
61  bool is_string( ) const;
62 
63  bool is_number( ) const;
64  int get_int(const std::string& key) const;
65  int as_int() const;
66  double get_double(const std::string& key) const;
67  double as_double() const;
68 
69  bool is_array( ) const;
70  bool is_object( ) const;
71 
72  size_t size() const;
73  private:
74  JsonObject get_scalar_object(const std::string& key) const;
75  void initialize(const std::string& inline_json);
76  cJSON * root;
77  bool owner;
78  };
79 }
80 
81 
82 
83 #endif
84 
85 
Definition: JsonObject.hpp:32