GDAL

cpl_json.h
Go to the documentation of this file.
1 /******************************************************************************
2 * Project: Common Portability Library
3 * Purpose: Function wrapper for libjson-c access.
4 * Author: Dmitry Baryshnikov, dmitry.baryshnikov@nextgis.com
5 *
6 ******************************************************************************
7 * Copyright (c) 2017-2018 NextGIS, <info@nextgis.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 ****************************************************************************/
27
28 #ifndef CPL_JSON_H_INCLUDED
29 #define CPL_JSON_H_INCLUDED
30
31 #include "cpl_progress.h"
32 #include "cpl_string.h"
33
34 #include <string>
35 #include <vector>
36
44 typedef void *JSONObjectH;
45
46 CPL_C_START
47
48 class CPLJSONArray;
54 class CPL_DLL CPLJSONObject
55 {
56 friend class CPLJSONArray;
57 friend class CPLJSONDocument;
58
59 public:
63 enum class Type
64 {
65 Unknown,
66 Null,
67 Object,
68 Array,
69 Boolean,
70 String,
71 Integer,
72 Long,
73 Double
74 };
75
79 enum class PrettyFormat
80 {
81 Plain,
82 Spaced,
83 Pretty
84 };
85
86 public:
88 CPLJSONObject();
89 explicit CPLJSONObject(const std::string &osName,
90 const CPLJSONObject &oParent);
91 ~CPLJSONObject();
92 CPLJSONObject(const CPLJSONObject &other);
93 CPLJSONObject(CPLJSONObject &&other);
94 CPLJSONObject &operator=(const CPLJSONObject &other);
95 CPLJSONObject &operator=(CPLJSONObject &&other);
96
97 private:
98 explicit CPLJSONObject(const std::string &osName, JSONObjectH poJsonObject);
101 public:
102 // setters
103 void Add(const std::string &osName, const std::string &osValue);
104 void Add(const std::string &osName, const char *pszValue);
105 void Add(const std::string &osName, double dfValue);
106 void Add(const std::string &osName, int nValue);
107 void Add(const std::string &osName, GInt64 nValue);
108 void Add(const std::string &osName, const CPLJSONArray &oValue);
109 void Add(const std::string &osName, const CPLJSONObject &oValue);
110 void AddNoSplitName(const std::string &osName, const CPLJSONObject &oValue);
111 void Add(const std::string &osName, bool bValue);
112 void AddNull(const std::string &osName);
113
114 void Set(const std::string &osName, const std::string &osValue);
115 void Set(const std::string &osName, const char *pszValue);
116 void Set(const std::string &osName, double dfValue);
117 void Set(const std::string &osName, int nValue);
118 void Set(const std::string &osName, GInt64 nValue);
119 void Set(const std::string &osName, bool bValue);
120 void SetNull(const std::string &osName);
121
123 JSONObjectH GetInternalHandle() const
124 {
125 return m_poJsonObject;
126 }
129 // getters
130 std::string GetString(const std::string &osName,
131 const std::string &osDefault = "") const;
132 double GetDouble(const std::string &osName, double dfDefault = 0.0) const;
133 int GetInteger(const std::string &osName, int nDefault = 0) const;
134 GInt64 GetLong(const std::string &osName, GInt64 nDefault = 0) const;
135 bool GetBool(const std::string &osName, bool bDefault = false) const;
136 std::string ToString(const std::string &osDefault = "") const;
137 double ToDouble(double dfDefault = 0.0) const;
138 int ToInteger(int nDefault = 0) const;
139 GInt64 ToLong(GInt64 nDefault = 0) const;
140 bool ToBool(bool bDefault = false) const;
141 CPLJSONArray ToArray() const;
142 std::string Format(PrettyFormat eFormat) const;
143
144 //
145 void Delete(const std::string &osName);
146 void DeleteNoSplitName(const std::string &osName);
147 CPLJSONArray GetArray(const std::string &osName) const;
148 CPLJSONObject GetObj(const std::string &osName) const;
149 CPLJSONObject operator[](const std::string &osName) const;
150 Type GetType() const;
152 std::string GetName() const
153 {
154 return m_osKey;
155 }
158 std::vector<CPLJSONObject> GetChildren() const;
159 bool IsValid() const;
160 void Deinit();
161
162 protected:
164 CPLJSONObject GetObjectByPath(const std::string &osPath,
165 std::string &osName) const;
168 private:
169 JSONObjectH m_poJsonObject = nullptr;
170 std::string m_osKey{};
171 };
172
176 class CPL_DLL CPLJSONArray : public CPLJSONObject
177 {
178 friend class CPLJSONObject;
179 friend class CPLJSONDocument;
180
181 public:
183 CPLJSONArray();
184 explicit CPLJSONArray(const std::string &osName);
185 explicit CPLJSONArray(const CPLJSONObject &other);
186
187 private:
188 explicit CPLJSONArray(const std::string &osName, JSONObjectH poJsonObject);
189
190 class CPL_DLL ConstIterator
191 {
192 const CPLJSONArray &m_oSelf;
193 int m_nIdx;
194 mutable CPLJSONObject m_oObj{};
195
196 public:
197 ConstIterator(const CPLJSONArray &oSelf, bool bStart)
198 : m_oSelf(oSelf), m_nIdx(bStart ? 0 : oSelf.Size())
199 {
200 }
201 ~ConstIterator() = default;
202 CPLJSONObject &operator*() const
203 {
204 m_oObj = m_oSelf[m_nIdx];
205 return m_oObj;
206 }
207 ConstIterator &operator++()
208 {
209 m_nIdx++;
210 return *this;
211 }
212 bool operator==(const ConstIterator &it) const
213 {
214 return m_nIdx == it.m_nIdx;
215 }
216 bool operator!=(const ConstIterator &it) const
217 {
218 return m_nIdx != it.m_nIdx;
219 }
220 };
221
223 public:
224 int Size() const;
225 void Add(const CPLJSONObject &oValue);
226 void Add(const std::string &osValue);
227 void Add(const char *pszValue);
228 void Add(double dfValue);
229 void Add(int nValue);
230 void Add(GInt64 nValue);
231 void Add(bool bValue);
232 CPLJSONObject operator[](int nIndex);
233 const CPLJSONObject operator[](int nIndex) const;
234
236 ConstIterator begin() const
237 {
238 return ConstIterator(*this, true);
239 }
241 ConstIterator end() const
242 {
243 return ConstIterator(*this, false);
244 }
245 };
246
250 class CPL_DLL CPLJSONDocument
251 {
252 public:
254 CPLJSONDocument();
255 ~CPLJSONDocument();
256 CPLJSONDocument(const CPLJSONDocument &other);
257 CPLJSONDocument &operator=(const CPLJSONDocument &other);
258 CPLJSONDocument(CPLJSONDocument &&other);
259 CPLJSONDocument &operator=(CPLJSONDocument &&other);
262 bool Save(const std::string &osPath) const;
263 std::string SaveAsString() const;
264
265 CPLJSONObject GetRoot();
266 const CPLJSONObject GetRoot() const;
267 void SetRoot(const CPLJSONObject &oRoot);
268 bool Load(const std::string &osPath);
269 bool LoadMemory(const std::string &osStr);
270 bool LoadMemory(const GByte *pabyData, int nLength = -1);
271 bool LoadChunks(const std::string &osPath, size_t nChunkSize = 16384,
272 GDALProgressFunc pfnProgress = nullptr,
273 void *pProgressArg = nullptr);
274 bool LoadUrl(const std::string &osUrl, const char *const *papszOptions,
275 GDALProgressFunc pfnProgress = nullptr,
276 void *pProgressArg = nullptr);
277
278 private:
279 mutable JSONObjectH m_poRootJsonObject;
280 };
281
282 CPL_C_END
283
284 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
285 extern "C++"
286 {
287 CPLStringList CPLParseKeyValueJson(const char *pszJson);
288 }
289 #endif
290
291 #endif // CPL_JSON_H_INCLUDED
CPLJSONArray
The JSONArray class JSON array from JSONDocument.
Definition: cpl_json.h:176
GByte
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:205
CPLStringList
String list class designed around our use of C "char**" string lists.
Definition: cpl_string.h:437
CPLParseKeyValueJson
CPLStringList CPLParseKeyValueJson(const char *pszJson)
Return a string list of key/value pairs extracted from a JSON doc.
Definition: cpl_json.cpp:1488
GInt64
GIntBig GInt64
Signed 64 bit integer type.
Definition: cpl_port.h:256
CPL_C_START
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:315
CPLJSONObject::PrettyFormat
PrettyFormat
Json object format to string options.
Definition: cpl_json.h:79
CPLJSONObject
The CPLJSONArray class holds JSON object from CPLJSONDocument.
Definition: cpl_json.h:54
CPLJSONArray::end
ConstIterator end() const
Iterator to after last element.
Definition: cpl_json.h:241
CPL_C_END
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:319
CPLJSONObject::Type
Type
Json object types.
Definition: cpl_json.h:63
cpl_string.h
CPLJSONArray::Size
int Size() const
Get array size.
Definition: cpl_json.cpp:1337
CPLJSONArray::begin
ConstIterator begin() const
Iterator to first element.
Definition: cpl_json.h:236
CPLJSONDocument
The CPLJSONDocument class Wrapper class around json-c library.
Definition: cpl_json.h:250