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 <cstdint>
35#include <string>
36#include <vector>
37
45typedef void *JSONObjectH;
46
48
49class CPLJSONArray;
50
56class CPL_DLL CPLJSONObject
57{
58 friend class CPLJSONArray;
59 friend class CPLJSONDocument;
60
61 public:
65 enum class Type
66 {
67 Unknown,
68 Null,
69 Object,
70 Array,
71 Boolean,
72 String,
73 Integer,
74 Long,
75 Double
76 };
77
81 enum class PrettyFormat
82 {
83 Plain,
84 Spaced,
85 Pretty
86 };
87
88 public:
91 explicit CPLJSONObject(const std::string &osName,
92 const CPLJSONObject &oParent);
93 explicit CPLJSONObject(std::nullptr_t);
94 explicit CPLJSONObject(const std::string &osVal);
95 explicit CPLJSONObject(const char *pszValue);
96 explicit CPLJSONObject(bool bVal);
97 explicit CPLJSONObject(int nVal);
98 explicit CPLJSONObject(int64_t nVal);
99 explicit CPLJSONObject(uint64_t nVal);
100 explicit CPLJSONObject(double dfVal);
102 CPLJSONObject(const CPLJSONObject &other);
104 CPLJSONObject &operator=(const CPLJSONObject &other);
105 CPLJSONObject &operator=(CPLJSONObject &&other);
106
107 // This method is not thread-safe
108 CPLJSONObject Clone() const;
109
110 private:
111 explicit CPLJSONObject(const std::string &osName, JSONObjectH poJsonObject);
114 public:
115 // setters
116 void Add(const std::string &osName, const std::string &osValue);
117 void Add(const std::string &osName, const char *pszValue);
118 void Add(const std::string &osName, double dfValue);
119 void Add(const std::string &osName, int nValue);
120 void Add(const std::string &osName, GInt64 nValue);
121 void Add(const std::string &osName, uint64_t nValue);
122 void Add(const std::string &osName, const CPLJSONArray &oValue);
123 void Add(const std::string &osName, const CPLJSONObject &oValue);
124 void AddNoSplitName(const std::string &osName, const CPLJSONObject &oValue);
125 void Add(const std::string &osName, bool bValue);
126 void AddNull(const std::string &osName);
127
128 void Set(const std::string &osName, const std::string &osValue);
129 void Set(const std::string &osName, const char *pszValue);
130 void Set(const std::string &osName, double dfValue);
131 void Set(const std::string &osName, int nValue);
132 void Set(const std::string &osName, GInt64 nValue);
133 void Set(const std::string &osName, uint64_t nValue);
134 void Set(const std::string &osName, bool bValue);
135 void SetNull(const std::string &osName);
136
138 JSONObjectH GetInternalHandle() const
139 {
140 return m_poJsonObject;
141 }
142
145 // getters
146 std::string GetString(const std::string &osName,
147 const std::string &osDefault = "") const;
148 double GetDouble(const std::string &osName, double dfDefault = 0.0) const;
149 int GetInteger(const std::string &osName, int nDefault = 0) const;
150 GInt64 GetLong(const std::string &osName, GInt64 nDefault = 0) const;
151 bool GetBool(const std::string &osName, bool bDefault = false) const;
152 std::string ToString(const std::string &osDefault = "") const;
153 double ToDouble(double dfDefault = 0.0) const;
154 int ToInteger(int nDefault = 0) const;
155 GInt64 ToLong(GInt64 nDefault = 0) const;
156 bool ToBool(bool bDefault = false) const;
157 CPLJSONArray ToArray() const;
158 std::string Format(PrettyFormat eFormat) const;
159
160 //
161 void Delete(const std::string &osName);
162 void DeleteNoSplitName(const std::string &osName);
163 CPLJSONArray GetArray(const std::string &osName) const;
164 CPLJSONObject GetObj(const std::string &osName) const;
165 CPLJSONObject operator[](const std::string &osName) const;
166 Type GetType() const;
167
169 std::string GetName() const
170 {
171 return m_osKey;
172 }
173
176 std::vector<CPLJSONObject> GetChildren() const;
177 bool IsValid() const;
178 void Deinit();
179
180 protected:
182 CPLJSONObject GetObjectByPath(const std::string &osPath,
183 std::string &osName) const;
186 private:
187 JSONObjectH m_poJsonObject = nullptr;
188 std::string m_osKey{};
189};
190
194class CPL_DLL CPLJSONArray : public CPLJSONObject
195{
196 friend class CPLJSONObject;
197 friend class CPLJSONDocument;
198
199 public:
201 CPLJSONArray();
202 explicit CPLJSONArray(const std::string &osName);
203 explicit CPLJSONArray(const CPLJSONObject &other);
204
205 private:
206 explicit CPLJSONArray(const std::string &osName, JSONObjectH poJsonObject);
207
208 class CPL_DLL ConstIterator
209 {
210 const CPLJSONArray &m_oSelf;
211 int m_nIdx;
212 mutable CPLJSONObject m_oObj{};
213
214 public:
215 ConstIterator(const CPLJSONArray &oSelf, bool bStart)
216 : m_oSelf(oSelf), m_nIdx(bStart ? 0 : oSelf.Size())
217 {
218 }
219
220 ~ConstIterator() = default;
221
222 CPLJSONObject &operator*() const
223 {
224 m_oObj = m_oSelf[m_nIdx];
225 return m_oObj;
226 }
227
228 ConstIterator &operator++()
229 {
230 m_nIdx++;
231 return *this;
232 }
233
234 bool operator==(const ConstIterator &it) const
235 {
236 return m_nIdx == it.m_nIdx;
237 }
238
239 bool operator!=(const ConstIterator &it) const
240 {
241 return m_nIdx != it.m_nIdx;
242 }
243 };
244
246 public:
247 int Size() const;
248 void AddNull();
249 void Add(const CPLJSONObject &oValue);
250 void Add(const std::string &osValue);
251 void Add(const char *pszValue);
252 void Add(double dfValue);
253 void Add(int nValue);
254 void Add(GInt64 nValue);
255 void Add(uint64_t nValue);
256 void Add(bool bValue);
257 CPLJSONObject operator[](int nIndex);
258 const CPLJSONObject operator[](int nIndex) const;
259
261 ConstIterator begin() const
262 {
263 return ConstIterator(*this, true);
264 }
265
267 ConstIterator end() const
268 {
269 return ConstIterator(*this, false);
270 }
271};
272
276class CPL_DLL CPLJSONDocument
277{
278 public:
282 CPLJSONDocument(const CPLJSONDocument &other);
283 CPLJSONDocument &operator=(const CPLJSONDocument &other);
285 CPLJSONDocument &operator=(CPLJSONDocument &&other);
288 bool Save(const std::string &osPath) const;
289 std::string SaveAsString() const;
290
291 CPLJSONObject GetRoot();
292 const CPLJSONObject GetRoot() const;
293 void SetRoot(const CPLJSONObject &oRoot);
294 bool Load(const std::string &osPath);
295 bool LoadMemory(const std::string &osStr);
296 bool LoadMemory(const GByte *pabyData, int nLength = -1);
297 bool LoadChunks(const std::string &osPath, size_t nChunkSize = 16384,
298 GDALProgressFunc pfnProgress = nullptr,
299 void *pProgressArg = nullptr);
300 bool LoadUrl(const std::string &osUrl, const char *const *papszOptions,
301 GDALProgressFunc pfnProgress = nullptr,
302 void *pProgressArg = nullptr);
303
304 private:
305 mutable JSONObjectH m_poRootJsonObject;
306};
307
309
310#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
311extern "C++"
312{
313 CPLStringList CPLParseKeyValueJson(const char *pszJson);
314}
315#endif
316
317#endif // CPL_JSON_H_INCLUDED
The JSONArray class JSON array from JSONDocument.
Definition: cpl_json.h:195
int Size() const
Get array size.
Definition: cpl_json.cpp:1455
ConstIterator begin() const
Iterator to first element.
Definition: cpl_json.h:261
ConstIterator end() const
Iterator to after last element.
Definition: cpl_json.h:267
The CPLJSONDocument class Wrapper class around json-c library.
Definition: cpl_json.h:277
The CPLJSONArray class holds JSON object from CPLJSONDocument.
Definition: cpl_json.h:57
Type
Json object types.
Definition: cpl_json.h:66
PrettyFormat
Json object format to string options.
Definition: cpl_json.h:82
String list class designed around our use of C "char**" string lists.
Definition: cpl_string.h:449
CPLStringList CPLParseKeyValueJson(const char *pszJson)
Return a string list of key/value pairs extracted from a JSON doc.
Definition: cpl_json.cpp:1632
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:299
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:295
GIntBig GInt64
Signed 64 bit integer type.
Definition: cpl_port.h:236
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:185
Various convenience functions for working with strings and string lists.