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
43 typedef void *JSONObjectH;
44
45 CPL_C_START
46
47 class CPLJSONArray;
53 class CPL_DLL CPLJSONObject
54 {
55 friend class CPLJSONArray;
56 friend class CPLJSONDocument;
57 public:
61 enum class Type {
62 Unknown,
63 Null,
64 Object,
65 Array,
66 Boolean,
67 String,
68 Integer,
69 Long,
70 Double
71 };
72
76 enum class PrettyFormat {
77 Plain,
78 Spaced,
79 Pretty
80 };
81
82 public:
84 CPLJSONObject();
85 explicit CPLJSONObject(const std::string &osName, const CPLJSONObject &oParent);
86 ~CPLJSONObject();
87 CPLJSONObject(const CPLJSONObject &other);
88 CPLJSONObject(CPLJSONObject &&other);
89 CPLJSONObject &operator=(const CPLJSONObject &other);
90 CPLJSONObject &operator=(CPLJSONObject &&other);
91
92 private:
93 explicit CPLJSONObject(const std::string &osName, JSONObjectH poJsonObject);
96 public:
97 // setters
98 void Add(const std::string &osName, const std::string &osValue);
99 void Add(const std::string &osName, const char *pszValue);
100 void Add(const std::string &osName, double dfValue);
101 void Add(const std::string &osName, int nValue);
102 void Add(const std::string &osName, GInt64 nValue);
103 void Add(const std::string &osName, const CPLJSONArray &oValue);
104 void Add(const std::string &osName, const CPLJSONObject &oValue);
105 void AddNoSplitName(const std::string &osName, const CPLJSONObject &oValue);
106 void Add(const std::string &osName, bool bValue);
107 void AddNull(const std::string &osName);
108
109 void Set(const std::string &osName, const std::string &osValue);
110 void Set(const std::string &osName, const char *pszValue);
111 void Set(const std::string &osName, double dfValue);
112 void Set(const std::string &osName, int nValue);
113 void Set(const std::string &osName, GInt64 nValue);
114 void Set(const std::string &osName, bool bValue);
115 void SetNull(const std::string &osName);
116
118 JSONObjectH GetInternalHandle() const { return m_poJsonObject; }
121 // getters
122 std::string GetString(const std::string &osName, const std::string &osDefault = "") const;
123 double GetDouble(const std::string &osName, double dfDefault = 0.0) const;
124 int GetInteger(const std::string &osName, int nDefault = 0) const;
125 GInt64 GetLong(const std::string &osName, GInt64 nDefault = 0) const;
126 bool GetBool(const std::string &osName, bool bDefault = false) const;
127 std::string ToString(const std::string &osDefault = "") const;
128 double ToDouble(double dfDefault = 0.0) const;
129 int ToInteger(int nDefault = 0) const;
130 GInt64 ToLong(GInt64 nDefault = 0) const;
131 bool ToBool(bool bDefault = false) const;
132 CPLJSONArray ToArray() const;
133 std::string Format(PrettyFormat eFormat) const;
134
135 //
136 void Delete(const std::string &osName);
137 void DeleteNoSplitName(const std::string &osName);
138 CPLJSONArray GetArray(const std::string &osName) const;
139 CPLJSONObject GetObj(const std::string &osName) const;
140 CPLJSONObject operator[](const std::string &osName) const;
141 Type GetType() const;
143 std::string GetName() const { return m_osKey; }
146 std::vector<CPLJSONObject> GetChildren() const;
147 bool IsValid() const;
148 void Deinit();
149
150 protected:
152 CPLJSONObject GetObjectByPath(const std::string &osPath, std::string &osName) const;
155 private:
156 JSONObjectH m_poJsonObject = nullptr;
157 std::string m_osKey{};
158 };
159
163 class CPL_DLL CPLJSONArray : public CPLJSONObject
164 {
165 friend class CPLJSONObject;
166 friend class CPLJSONDocument;
167 public:
169 CPLJSONArray();
170 explicit CPLJSONArray(const std::string &osName);
171 explicit CPLJSONArray(const CPLJSONObject &other);
172
173 private:
174 explicit CPLJSONArray(const std::string &osName, JSONObjectH poJsonObject);
175
176 class CPL_DLL ConstIterator
177 {
178 const CPLJSONArray& m_oSelf;
179 int m_nIdx;
180 mutable CPLJSONObject m_oObj{};
181
182 public:
183 ConstIterator(const CPLJSONArray& oSelf, bool bStart): m_oSelf(oSelf), m_nIdx(bStart ? 0 : oSelf.Size()) {}
184 ~ConstIterator() = default;
185 CPLJSONObject& operator*() const { m_oObj = m_oSelf[m_nIdx]; return m_oObj; }
186 ConstIterator& operator++() { m_nIdx ++; return *this; }
187 bool operator==(const ConstIterator& it) const { return m_nIdx == it.m_nIdx; }
188 bool operator!=(const ConstIterator& it) const { return m_nIdx != it.m_nIdx; }
189 };
190
192 public:
193 int Size() const;
194 void Add(const CPLJSONObject &oValue);
195 void Add(const std::string &osValue);
196 void Add(const char* pszValue);
197 void Add(double dfValue);
198 void Add(int nValue);
199 void Add(GInt64 nValue);
200 void Add(bool bValue);
201 CPLJSONObject operator[](int nIndex);
202 const CPLJSONObject operator[](int nIndex) const;
203
205 ConstIterator begin() const { return ConstIterator(*this, true); }
207 ConstIterator end() const { return ConstIterator(*this, false); }
208 };
209
213 class CPL_DLL CPLJSONDocument
214 {
215 public:
217 CPLJSONDocument();
218 ~CPLJSONDocument();
219 CPLJSONDocument(const CPLJSONDocument &other);
220 CPLJSONDocument& operator=(const CPLJSONDocument &other);
221 CPLJSONDocument(CPLJSONDocument &&other);
222 CPLJSONDocument& operator=(CPLJSONDocument &&other);
225 bool Save(const std::string &osPath) const;
226 std::string SaveAsString() const;
227
228 CPLJSONObject GetRoot();
229 const CPLJSONObject GetRoot() const;
230 void SetRoot(const CPLJSONObject& oRoot);
231 bool Load(const std::string &osPath);
232 bool LoadMemory(const std::string &osStr);
233 bool LoadMemory(const GByte *pabyData, int nLength = -1);
234 bool LoadChunks(const std::string &osPath, size_t nChunkSize = 16384,
235 GDALProgressFunc pfnProgress = nullptr,
236 void *pProgressArg = nullptr);
237 bool LoadUrl(const std::string &osUrl, const char* const* papszOptions,
238 GDALProgressFunc pfnProgress = nullptr,
239 void *pProgressArg = nullptr);
240
241 private:
242 mutable JSONObjectH m_poRootJsonObject;
243 };
244
245 CPL_C_END
246
247 #endif // CPL_JSON_H_INCLUDED
CPLJSONArray
The JSONArray class JSON array from JSONDocument.
Definition: cpl_json.h:163
GByte
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:203
GInt64
GIntBig GInt64
Signed 64 bit integer type.
Definition: cpl_port.h:249
CPL_C_START
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:304
CPLJSONObject::PrettyFormat
PrettyFormat
Json object format to string options.
Definition: cpl_json.h:76
CPLJSONObject
The CPLJSONArray class holds JSON object from CPLJSONDocument.
Definition: cpl_json.h:53
CPLJSONArray::end
ConstIterator end() const
Iterator to after last element.
Definition: cpl_json.h:207
CPL_C_END
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:306
CPLJSONObject::Type
Type
Json object types.
Definition: cpl_json.h:61
CPLJSONArray::Size
int Size() const
Get array size.
Definition: cpl_json.cpp:1319
CPLJSONArray::begin
ConstIterator begin() const
Iterator to first element.
Definition: cpl_json.h:205
CPLJSONDocument
The CPLJSONDocument class Wrapper class around json-c library.
Definition: cpl_json.h:213