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 <string>
24 
25 #include <opm/common/utility/FileSystem.hpp>
26 
27 struct cJSON;
28 
29 namespace Json {
30 
31  class JsonObject {
32  public:
33  JsonObject();
34 
35  explicit JsonObject(const Opm::filesystem::path& jsonFile );
36  explicit JsonObject(const std::string& inline_json);
37  explicit JsonObject(const char * inline_json);
38  explicit JsonObject(cJSON * root);
39  ~JsonObject();
40 
41  void add(double value);
42  void add(int value);
43  void add(const std::string& value);
44  JsonObject add_array();
45  JsonObject add_object();
46  void add_item(const std::string& key, double value);
47  void add_item(const std::string& key, int value);
48  void add_item(const std::string& key, const std::string& value);
49  JsonObject add_array(const std::string& key);
50  JsonObject add_object(const std::string& key);
51  std::string dump() const;
52 
53  bool has_item(const std::string& key) const;
54  JsonObject get_array_item( size_t index ) const;
55  JsonObject get_item(const std::string& key) const;
56 
57  std::string to_string() const;
58  std::string get_string(const std::string& key) const;
59  std::string as_string() const;
60  bool is_string( ) const;
61 
62  bool is_number( ) const;
63  int get_int(const std::string& key) const;
64  int as_int() const;
65  double get_double(const std::string& key) const;
66  double as_double() const;
67 
68  bool is_array( ) const;
69  bool is_object( ) const;
70 
71  size_t size() const;
72  private:
73  JsonObject get_scalar_object(const std::string& key) const;
74  void initialize(const std::string& inline_json);
75  cJSON * root;
76  bool owner;
77  };
78 }
79 
80 
81 
82 #endif
83 
84 
Definition: JsonObject.hpp:31