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