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
33#include <string>
34#include <vector>
35
43typedef void *JSONObjectH;
44
46
47class CPLJSONArray;
53class CPL_DLL CPLJSONObject
54{
55 friend class CPLJSONArray;
56 friend class CPLJSONDocument;
57
58 public:
62 enum class Type
63 {
64 Unknown,
65 Null,
66 Object,
67 Array,
68 Boolean,
69 String,
70 Integer,
71 Long,
72 Double
73 };
74
78 enum class PrettyFormat
79 {
80 Plain,
81 Spaced,
82 Pretty
83 };
84
85 public:
88 explicit CPLJSONObject(const std::string &osName,
89 const CPLJSONObject &oParent);
91 CPLJSONObject(const CPLJSONObject &other);
93 CPLJSONObject &operator=(const CPLJSONObject &other);
94 CPLJSONObject &operator=(CPLJSONObject &&other);
95
96 private:
97 explicit CPLJSONObject(const std::string &osName, JSONObjectH poJsonObject);
100 public:
101 // setters
102 void Add(const std::string &osName, const std::string &osValue);
103 void Add(const std::string &osName, const char *pszValue);
104 void Add(const std::string &osName, double dfValue);
105 void Add(const std::string &osName, int nValue);
106 void Add(const std::string &osName, GInt64 nValue);
107 void Add(const std::string &osName, const CPLJSONArray &oValue);
108 void Add(const std::string &osName, const CPLJSONObject &oValue);
109 void AddNoSplitName(const std::string &osName, const CPLJSONObject &oValue);
110 void Add(const std::string &osName, bool bValue);
111 void AddNull(const std::string &osName);
112
113 void Set(const std::string &osName, const std::string &osValue);
114 void Set(const std::string &osName, const char *pszValue);
115 void Set(const std::string &osName, double dfValue);
116 void Set(const std::string &osName, int nValue);
117 void Set(const std::string &osName, GInt64 nValue);
118 void Set(const std::string &osName, bool bValue);
119 void SetNull(const std::string &osName);
120
122 JSONObjectH GetInternalHandle() const
123 {
124 return m_poJsonObject;
125 }
128 // getters
129 std::string GetString(const std::string &osName,
130 const std::string &osDefault = "") const;
131 double GetDouble(const std::string &osName, double dfDefault = 0.0) const;
132 int GetInteger(const std::string &osName, int nDefault = 0) const;
133 GInt64 GetLong(const std::string &osName, GInt64 nDefault = 0) const;
134 bool GetBool(const std::string &osName, bool bDefault = false) const;
135 std::string ToString(const std::string &osDefault = "") const;
136 double ToDouble(double dfDefault = 0.0) const;
137 int ToInteger(int nDefault = 0) const;
138 GInt64 ToLong(GInt64 nDefault = 0) const;
139 bool ToBool(bool bDefault = false) const;
140 CPLJSONArray ToArray() const;
141 std::string Format(PrettyFormat eFormat) const;
142
143 //
144 void Delete(const std::string &osName);
145 void DeleteNoSplitName(const std::string &osName);
146 CPLJSONArray GetArray(const std::string &osName) const;
147 CPLJSONObject GetObj(const std::string &osName) const;
148 CPLJSONObject operator[](const std::string &osName) const;
149 Type GetType() const;
151 std::string GetName() const
152 {
153 return m_osKey;
154 }
157 std::vector<CPLJSONObject> GetChildren() const;
158 bool IsValid() const;
159 void Deinit();
160
161 protected:
163 CPLJSONObject GetObjectByPath(const std::string &osPath,
164 std::string &osName) const;
167 private:
168 JSONObjectH m_poJsonObject = nullptr;
169 std::string m_osKey{};
170};
171
175class CPL_DLL CPLJSONArray : public CPLJSONObject
176{
177 friend class CPLJSONObject;
178 friend class CPLJSONDocument;
179
180 public:
182 CPLJSONArray();
183 explicit CPLJSONArray(const std::string &osName);
184 explicit CPLJSONArray(const CPLJSONObject &other);
185
186 private:
187 explicit CPLJSONArray(const std::string &osName, JSONObjectH poJsonObject);
188
189 class CPL_DLL ConstIterator
190 {
191 const CPLJSONArray &m_oSelf;
192 int m_nIdx;
193 mutable CPLJSONObject m_oObj{};
194
195 public:
196 ConstIterator(const CPLJSONArray &oSelf, bool bStart)
197 : m_oSelf(oSelf), m_nIdx(bStart ? 0 : oSelf.Size())
198 {
199 }
200 ~ConstIterator() = default;
201 CPLJSONObject &operator*() const
202 {
203 m_oObj = m_oSelf[m_nIdx];
204 return m_oObj;
205 }
206 ConstIterator &operator++()
207 {
208 m_nIdx++;
209 return *this;
210 }
211 bool operator==(const ConstIterator &it) const
212 {
213 return m_nIdx == it.m_nIdx;
214 }
215 bool operator!=(const ConstIterator &it) const
216 {
217 return m_nIdx != it.m_nIdx;
218 }
219 };
220
222 public:
223 int Size() const;
224 void Add(const CPLJSONObject &oValue);
225 void Add(const std::string &osValue);
226 void Add(const char *pszValue);
227 void Add(double dfValue);
228 void Add(int nValue);
229 void Add(GInt64 nValue);
230 void Add(bool bValue);
231 CPLJSONObject operator[](int nIndex);
232 const CPLJSONObject operator[](int nIndex) const;
233
235 ConstIterator begin() const
236 {
237 return ConstIterator(*this, true);
238 }
240 ConstIterator end() const
241 {
242 return ConstIterator(*this, false);
243 }
244};
245
249class CPL_DLL CPLJSONDocument
250{
251 public:
255 CPLJSONDocument(const CPLJSONDocument &other);
256 CPLJSONDocument &operator=(const CPLJSONDocument &other);
258 CPLJSONDocument &operator=(CPLJSONDocument &&other);
261 bool Save(const std::string &osPath) const;
262 std::string SaveAsString() const;
263
264 CPLJSONObject GetRoot();
265 const CPLJSONObject GetRoot() const;
266 void SetRoot(const CPLJSONObject &oRoot);
267 bool Load(const std::string &osPath);
268 bool LoadMemory(const std::string &osStr);
269 bool LoadMemory(const GByte *pabyData, int nLength = -1);
270 bool LoadChunks(const std::string &osPath, size_t nChunkSize = 16384,
271 GDALProgressFunc pfnProgress = nullptr,
272 void *pProgressArg = nullptr);
273 bool LoadUrl(const std::string &osUrl, const char *const *papszOptions,
274 GDALProgressFunc pfnProgress = nullptr,
275 void *pProgressArg = nullptr);
276
277 private:
278 mutable JSONObjectH m_poRootJsonObject;
279};
280
282
283#endif // CPL_JSON_H_INCLUDED
The JSONArray class JSON array from JSONDocument.
Definition cpl_json.h:176
int Size() const
Get array size.
Definition cpl_json.cpp:1330
ConstIterator begin() const
Iterator to first element.
Definition cpl_json.h:235
ConstIterator end() const
Iterator to after last element.
Definition cpl_json.h:240
The CPLJSONDocument class Wrapper class around json-c library.
Definition cpl_json.h:250
The CPLJSONArray class holds JSON object from CPLJSONDocument.
Definition cpl_json.h:54
Type
Json object types.
Definition cpl_json.h:63
PrettyFormat
Json object format to string options.
Definition cpl_json.h:79
#define CPL_C_END
Macro to end a block of C symbols.
Definition cpl_port.h:317
#define CPL_C_START
Macro to start a block of C symbols.
Definition cpl_port.h:313
GIntBig GInt64
Signed 64 bit integer type.
Definition cpl_port.h:254
unsigned char GByte
Unsigned byte type.
Definition cpl_port.h:205