| 1 | // |
| 2 | // JSONConfiguration.h |
| 3 | // |
| 4 | // Library: Util |
| 5 | // Package: Util |
| 6 | // Module: JSONConfiguration |
| 7 | // |
| 8 | // Definition of the JSONConfiguration class. |
| 9 | // |
| 10 | // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. |
| 11 | // and Contributors. |
| 12 | // |
| 13 | // SPDX-License-Identifier: BSL-1.0 |
| 14 | // |
| 15 | |
| 16 | |
| 17 | #ifndef Util_JSONConfiguration_INCLUDED |
| 18 | #define Util_JSONConfiguration_INCLUDED |
| 19 | |
| 20 | |
| 21 | #include "Poco/Util/Util.h" |
| 22 | |
| 23 | |
| 24 | #ifndef POCO_UTIL_NO_JSONCONFIGURATION |
| 25 | |
| 26 | |
| 27 | #include "Poco/Util/AbstractConfiguration.h" |
| 28 | #include "Poco/JSON/Object.h" |
| 29 | #include <istream> |
| 30 | |
| 31 | |
| 32 | namespace Poco { |
| 33 | namespace Util { |
| 34 | |
| 35 | |
| 36 | class Util_API JSONConfiguration : public AbstractConfiguration |
| 37 | /// This configuration class extracts configuration properties |
| 38 | /// from a JSON object. An XPath-like syntax for property |
| 39 | /// names is supported to allow full access to the JSON object. |
| 40 | /// |
| 41 | /// Given the following JSON object as an example: |
| 42 | /// { |
| 43 | /// "config" : { |
| 44 | /// "prop1" : "value1", |
| 45 | /// "prop2" : 10, |
| 46 | /// "prop3" : [ |
| 47 | /// "element1", |
| 48 | /// "element2" |
| 49 | /// ], |
| 50 | /// "prop4" : { |
| 51 | /// "prop5" : false, |
| 52 | /// "prop6" : null |
| 53 | /// } |
| 54 | /// } |
| 55 | /// } |
| 56 | /// The following property names would be valid and would |
| 57 | /// yield the shown values: |
| 58 | /// |
| 59 | /// config.prop1 --> "value1" |
| 60 | /// config.prop3[1] --> "element2" |
| 61 | /// config.prop4.prop5 --> false |
| 62 | { |
| 63 | public: |
| 64 | |
| 65 | JSONConfiguration(); |
| 66 | /// Creates an empty configuration |
| 67 | |
| 68 | |
| 69 | JSONConfiguration(const std::string& path); |
| 70 | /// Creates a configuration and loads the JSON structure from the given file |
| 71 | |
| 72 | |
| 73 | JSONConfiguration(std::istream& istr); |
| 74 | /// Creates a configuration and loads the JSON structure from the given stream |
| 75 | |
| 76 | |
| 77 | JSONConfiguration(const JSON::Object::Ptr& object); |
| 78 | /// Creates a configuration from the given JSON object |
| 79 | |
| 80 | |
| 81 | virtual ~JSONConfiguration(); |
| 82 | /// Destructor |
| 83 | |
| 84 | |
| 85 | void load(const std::string& path); |
| 86 | /// Loads the configuration from the given file |
| 87 | |
| 88 | |
| 89 | void load(std::istream& istr); |
| 90 | /// Loads the configuration from the given stream |
| 91 | |
| 92 | |
| 93 | void loadEmpty(const std::string& root); |
| 94 | /// Loads an empty object containing only a root object with the given name. |
| 95 | |
| 96 | |
| 97 | void save(std::ostream& ostr, unsigned int indent = 2) const; |
| 98 | /// Saves the configuration to the given stream |
| 99 | |
| 100 | |
| 101 | virtual void setInt(const std::string& key, int value); |
| 102 | |
| 103 | |
| 104 | virtual void setBool(const std::string& key, bool value); |
| 105 | |
| 106 | |
| 107 | virtual void setDouble(const std::string& key, double value); |
| 108 | |
| 109 | |
| 110 | virtual void setString(const std::string& key, const std::string& value); |
| 111 | |
| 112 | |
| 113 | virtual void removeRaw(const std::string& key); |
| 114 | |
| 115 | |
| 116 | protected: |
| 117 | |
| 118 | bool (const std::string & key, std::string & value) const; |
| 119 | |
| 120 | |
| 121 | void setRaw(const std::string& key, const std::string& value); |
| 122 | |
| 123 | |
| 124 | void enumerate(const std::string& key, Keys& range) const; |
| 125 | |
| 126 | |
| 127 | private: |
| 128 | |
| 129 | |
| 130 | JSON::Object::Ptr (const std::string& key, std::string& lastPart); |
| 131 | |
| 132 | |
| 133 | void (std::string& name, std::vector<int>& indexes); |
| 134 | |
| 135 | |
| 136 | void setValue(const std::string& key, const Poco::DynamicAny& value); |
| 137 | |
| 138 | |
| 139 | JSON::Object::Ptr _object; |
| 140 | }; |
| 141 | |
| 142 | |
| 143 | } } // namespace Poco::Util |
| 144 | |
| 145 | |
| 146 | #endif // POCO_UTIL_NO_JSONCONFIGURATION |
| 147 | |
| 148 | |
| 149 | #endif // Util_JSONConfiguration_INCLUDED |
| 150 | |
| 151 | |