12#ifndef CPL_JSON_H_INCLUDED
13#define CPL_JSON_H_INCLUDED
15#include "cpl_progress.h"
19#include <initializer_list>
22#if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
34typedef void *JSONObjectH;
39class CPLJSONObjectProxy
42 const std::string osName;
46 const std::string &osNameIn)
47 : oObj(oObjIn), osName(osNameIn)
51 template <
class T>
inline CPLJSONObjectProxy &operator=(
const T &val);
115 explicit CPLJSONObject(
const std::string &osName, JSONObjectH poJsonObject);
120 void Add(
const std::string &osName,
const std::string &osValue);
121#if defined(DOXYGEN_SKIP) || __cplusplus >= 201703L || \
122 (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
124 void Add(
const std::string &osName, std::string_view svValue);
126 void Add(
const std::string &osName,
const char *pszValue);
127 void Add(
const std::string &osName,
double dfValue);
128 void Add(
const std::string &osName,
int nValue);
129 void Add(
const std::string &osName,
GInt64 nValue);
130 void Add(
const std::string &osName, uint64_t nValue);
131 void Add(
const std::string &osName,
const CPLJSONArray &oValue);
132 void Add(
const std::string &osName,
const CPLJSONObject &oValue);
133 void AddNoSplitName(
const std::string &osName,
const CPLJSONObject &oValue);
134 void Add(
const std::string &osName,
bool bValue);
135 void AddNull(
const std::string &osName);
138 template <
class T>
void Set(
const std::string &osName,
const T &val)
144 void SetNull(
const std::string &osName);
153#pragma GCC diagnostic push
154#pragma GCC diagnostic ignored "-Weffc++"
156 template <
class T>
inline CPLJSONObject &operator=(
const T &val)
159 std::string osKeyForSet = m_osKeyForSet;
160 m_osKeyForSet.clear();
161 Set(osKeyForSet, val);
165#pragma GCC diagnostic pop
169 inline CPLJSONObject &operator=(std::initializer_list<T> list);
171 JSONObjectH GetInternalHandle()
const
173 return m_poJsonObject;
179 std::string GetString(
const std::string &osName,
180 const std::string &osDefault =
"")
const;
181 double GetDouble(
const std::string &osName,
double dfDefault = 0.0)
const;
182 int GetInteger(
const std::string &osName,
int nDefault = 0)
const;
183 GInt64 GetLong(
const std::string &osName,
GInt64 nDefault = 0)
const;
184 bool GetBool(
const std::string &osName,
bool bDefault =
false)
const;
185 std::string ToString(
const std::string &osDefault =
"")
const;
186 double ToDouble(
double dfDefault = 0.0)
const;
187 int ToInteger(
int nDefault = 0)
const;
189 bool ToBool(
bool bDefault =
false)
const;
191 std::string Format(PrettyFormat eFormat)
const;
194 void Delete(
const std::string &osName);
195 void DeleteNoSplitName(
const std::string &osName);
198 CPLJSONObject GetObjNoSplitName(
const std::string &osName)
const;
200 Type GetType()
const;
203 std::string GetName()
const
210 std::vector<CPLJSONObject> GetChildren()
const;
211 bool IsValid()
const;
217 std::string &osName)
const;
221 JSONObjectH m_poJsonObject =
nullptr;
222 std::string m_osKey{};
223 std::string m_osKeyForSet{};
241 template <
class T>
static CPLJSONArray Build(std::initializer_list<T> list)
244 for (
const auto &val : list)
250 explicit CPLJSONArray(
const std::string &osName, JSONObjectH poJsonObject);
252 class CPL_DLL ConstIterator
259 using iterator_category = std::input_iterator_tag;
261 using difference_type = std::ptrdiff_t;
266 : m_oSelf(oSelf), m_nIdx(bStart ? 0 : oSelf.
Size())
270 ConstIterator(
const ConstIterator &) =
default;
272 ConstIterator &operator=(
const ConstIterator &other)
277 m_nIdx = other.m_nIdx;
278 m_oObj = other.m_oObj;
283 ~ConstIterator() =
default;
287 m_oObj = m_oSelf[m_nIdx];
291 ConstIterator &operator++()
297 bool operator==(
const ConstIterator &it)
const
299 return m_nIdx == it.m_nIdx;
302 bool operator!=(
const ConstIterator &it)
const
304 return m_nIdx != it.m_nIdx;
315 return static_cast<size_t>(Size());
320 void Add(
const std::string &osValue);
321#if defined(DOXYGEN_SKIP) || __cplusplus >= 201703L || \
322 (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
324 void Add(std::string_view svValue);
326 void Add(
const char *pszValue);
327 void Add(
double dfValue);
328 void Add(
int nValue);
330 void Add(uint64_t nValue);
331 void Add(
bool bValue);
342 return ConstIterator(*
this,
true);
348 return ConstIterator(*
this,
false);
367 bool Save(
const std::string &osPath)
const;
368 std::string SaveAsString()
const;
373 bool Load(
const std::string &osPath);
374 bool LoadMemory(
const std::string &osStr);
375 bool LoadMemory(
const GByte *pabyData,
int nLength = -1);
376 bool LoadChunks(
const std::string &osPath,
size_t nChunkSize = 16384,
377 GDALProgressFunc pfnProgress =
nullptr,
378 void *pProgressArg =
nullptr);
379 bool LoadUrl(
const std::string &osUrl,
const char *
const *papszOptions,
380 GDALProgressFunc pfnProgress =
nullptr,
381 void *pProgressArg =
nullptr);
384 mutable JSONObjectH m_poRootJsonObject;
389inline CPLJSONObject &CPLJSONObject::operator=(std::initializer_list<T> list)
391 return operator=(CPLJSONArray::Build(list));
The JSONArray class JSON array from JSONDocument.
Definition cpl_json.h:230
int Size() const
Get array size.
Definition cpl_json.cpp:1432
void Add(const CPLJSONObject &oValue)
Add json object to array.
Definition cpl_json.cpp:1456
ConstIterator begin() const
Iterator to first element.
Definition cpl_json.h:340
size_t size() const
Return the size of the array.
Definition cpl_json.h:313
ConstIterator end() const
Iterator to after last element.
Definition cpl_json.h:346
The CPLJSONDocument class Wrapper class around json-c library.
Definition cpl_json.h:356
The CPLJSONArray class holds JSON object from CPLJSONDocument.
Definition cpl_json.h:60
Type
Json object types.
Definition cpl_json.h:69
PrettyFormat
Json object format to string options.
Definition cpl_json.h:85
void Set(const std::string &osName, const T &val)
Change value by key.
Definition cpl_json.h:138
String list class designed around our use of C "char**" string lists.
Definition cpl_string.h:476
#define CPLAssert(expr)
Assert on an expression.
Definition cpl_error.h:353
CPLStringList CPLParseKeyValueJson(const char *pszJson)
Return a string list of key/value pairs extracted from a JSON doc.
Definition cpl_json.cpp:1648
GIntBig GInt64
Signed 64 bit integer type.
Definition cpl_port.h:216
unsigned char GByte
Unsigned byte type.
Definition cpl_port.h:165
Various convenience functions for working with strings and string lists.