12#ifndef CPL_JSON_H_INCLUDED
13#define CPL_JSON_H_INCLUDED
15#include "cpl_progress.h"
19#include <initializer_list>
30typedef void *JSONObjectH;
35class CPLJSONObjectProxy
38 const std::string osName;
41 explicit inline CPLJSONObjectProxy(CPLJSONObject &oObjIn,
42 const std::string &osNameIn)
43 : oObj(oObjIn), osName(osNameIn)
47 template <
class T>
inline CPLJSONObjectProxy &operator=(
const T &val);
57 friend class CPLJSONArray;
58 friend class CPLJSONDocument;
111 explicit CPLJSONObject(
const std::string &osName, JSONObjectH poJsonObject);
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);
129 template <
class T>
void Set(
const std::string &osName,
const T &val)
135 void SetNull(
const std::string &osName);
144#pragma GCC diagnostic push
145#pragma GCC diagnostic ignored "-Weffc++"
147 template <
class T>
inline CPLJSONObject &operator=(
const T &val)
150 std::string osKeyForSet = m_osKeyForSet;
151 m_osKeyForSet.clear();
152 Set(osKeyForSet, val);
156#pragma GCC diagnostic pop
160 inline CPLJSONObject &operator=(std::initializer_list<T> list);
162 JSONObjectH GetInternalHandle()
const
164 return m_poJsonObject;
170 std::string GetString(
const std::string &osName,
171 const std::string &osDefault =
"")
const;
172 double GetDouble(
const std::string &osName,
double dfDefault = 0.0)
const;
173 int GetInteger(
const std::string &osName,
int nDefault = 0)
const;
174 GInt64 GetLong(
const std::string &osName,
GInt64 nDefault = 0)
const;
175 bool GetBool(
const std::string &osName,
bool bDefault =
false)
const;
176 std::string ToString(
const std::string &osDefault =
"")
const;
177 double ToDouble(
double dfDefault = 0.0)
const;
178 int ToInteger(
int nDefault = 0)
const;
180 bool ToBool(
bool bDefault =
false)
const;
181 CPLJSONArray ToArray()
const;
182 std::string Format(PrettyFormat eFormat)
const;
185 void Delete(
const std::string &osName);
186 void DeleteNoSplitName(
const std::string &osName);
187 CPLJSONArray GetArray(
const std::string &osName)
const;
188 CPLJSONObject GetObj(
const std::string &osName)
const;
189 CPLJSONObject operator[](
const std::string &osName)
const;
190 Type GetType()
const;
193 std::string GetName()
const
200 std::vector<CPLJSONObject> GetChildren()
const;
201 bool IsValid()
const;
206 CPLJSONObject GetObjectByPath(
const std::string &osPath,
207 std::string &osName)
const;
211 JSONObjectH m_poJsonObject =
nullptr;
212 std::string m_osKey{};
213 std::string m_osKeyForSet{};
219class CPL_DLL CPLJSONArray :
public CPLJSONObject
221 friend class CPLJSONObject;
222 friend class CPLJSONDocument;
227 explicit CPLJSONArray(
const std::string &osName);
228 explicit CPLJSONArray(
const CPLJSONObject &other);
231 template <
class T>
static CPLJSONArray Build(std::initializer_list<T> list)
234 for (
const auto &val : list)
240 explicit CPLJSONArray(
const std::string &osName, JSONObjectH poJsonObject);
242 class CPL_DLL ConstIterator
244 const CPLJSONArray &m_oSelf;
246 mutable CPLJSONObject m_oObj{};
249 ConstIterator(
const CPLJSONArray &oSelf,
bool bStart)
250 : m_oSelf(oSelf), m_nIdx(bStart ? 0 : oSelf.
Size())
254 ~ConstIterator() =
default;
256 CPLJSONObject &operator*()
const
258 m_oObj = m_oSelf[m_nIdx];
262 ConstIterator &operator++()
268 bool operator==(
const ConstIterator &it)
const
270 return m_nIdx == it.m_nIdx;
273 bool operator!=(
const ConstIterator &it)
const
275 return m_nIdx != it.m_nIdx;
283 void Add(
const CPLJSONObject &oValue);
284 void Add(
const std::string &osValue);
285 void Add(
const char *pszValue);
286 void Add(
double dfValue);
287 void Add(
int nValue);
289 void Add(uint64_t nValue);
290 void Add(
bool bValue);
293 const CPLJSONObject
operator[](
int nIndex)
const;
298 return ConstIterator(*
this,
true);
304 return ConstIterator(*
this,
false);
323 bool Save(
const std::string &osPath)
const;
329 bool Load(
const std::string &osPath);
332 bool LoadChunks(
const std::string &osPath,
size_t nChunkSize = 16384,
333 GDALProgressFunc pfnProgress =
nullptr,
334 void *pProgressArg =
nullptr);
335 bool LoadUrl(
const std::string &osUrl,
const char *
const *papszOptions,
336 GDALProgressFunc pfnProgress =
nullptr,
337 void *pProgressArg =
nullptr);
340 mutable JSONObjectH m_poRootJsonObject;
345inline CPLJSONObject &CPLJSONObject::operator=(std::initializer_list<T> list)
347 return operator=(CPLJSONArray::Build(list));
The JSONArray class JSON array from JSONDocument.
Definition cpl_json.h:220
int Size() const
Get array size.
Definition cpl_json.cpp:1385
void Add(const CPLJSONObject &oValue)
Add json object to array.
Definition cpl_json.cpp:1409
void AddNull()
Add null object to array.
Definition cpl_json.cpp:1398
ConstIterator begin() const
Iterator to first element.
Definition cpl_json.h:296
CPLJSONObject operator[](int nIndex)
Get array item by index.
Definition cpl_json.cpp:1512
ConstIterator end() const
Iterator to after last element.
Definition cpl_json.h:302
The CPLJSONDocument class Wrapper class around json-c library.
Definition cpl_json.h:312
bool Load(const std::string &osPath)
Load json document from file by provided path.
Definition cpl_json.cpp:196
CPLJSONObject GetRoot()
Get json document root object.
Definition cpl_json.cpp:159
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:292
bool Save(const std::string &osPath) const
Save json document at specified path.
Definition cpl_json.cpp:113
bool LoadMemory(const std::string &osStr)
Load json document from memory buffer.
Definition cpl_json.cpp:274
void SetRoot(const CPLJSONObject &oRoot)
Set json document root object.
Definition cpl_json.cpp:182
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:410
std::string SaveAsString() const
Return the json document as a serialized string.
Definition cpl_json.cpp:137
The CPLJSONArray class holds JSON object from CPLJSONDocument.
Definition cpl_json.h:56
void Add(const std::string &osName, const std::string &osValue)
Add new key - value pair to json object.
Definition cpl_json.cpp:620
void Delete(const std::string &osName)
Delete json object by key.
Definition cpl_json.cpp:956
Type
Json object types.
Definition cpl_json.h:65
PrettyFormat
Json object format to string options.
Definition cpl_json.h:81
void Set(const std::string &osName, const T &val)
Change value by key.
Definition cpl_json.h:129
String list class designed around our use of C "char**" string lists.
Definition cpl_string.h:454
#define CPLAssert(expr)
Assert on an expression.
Definition cpl_error.h:330
CPLStringList CPLParseKeyValueJson(const char *pszJson)
Return a string list of key/value pairs extracted from a JSON doc.
Definition cpl_json.cpp:1553
GIntBig GInt64
Signed 64 bit integer type.
Definition cpl_port.h:226
unsigned char GByte
Unsigned byte type.
Definition cpl_port.h:175
Various convenience functions for working with strings and string lists.