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 #include "cpl_string.h"
33 
34 #include <string>
35 #include <vector>
36 
44 typedef void *JSONObjectH;
45 
47 
48 class CPLJSONArray;
54 class CPL_DLL CPLJSONObject
55 {
56  friend class CPLJSONArray;
57  friend class CPLJSONDocument;
58 
59  public:
63  enum class Type
64  {
65  Unknown,
66  Null,
67  Object,
68  Array,
69  Boolean,
70  String,
71  Integer,
72  Long,
73  Double
74  };
75 
79  enum class PrettyFormat
80  {
81  Plain,
82  Spaced,
83  Pretty
84  };
85 
86  public:
88  CPLJSONObject();
89  explicit CPLJSONObject(const std::string &osName,
90  const CPLJSONObject &oParent);
91  explicit CPLJSONObject(std::nullptr_t);
92  explicit CPLJSONObject(const std::string &osVal);
93  explicit CPLJSONObject(const char *pszValue);
94  explicit CPLJSONObject(bool bVal);
95  explicit CPLJSONObject(int nVal);
96  explicit CPLJSONObject(int64_t nVal);
97  explicit CPLJSONObject(uint64_t nVal);
98  explicit CPLJSONObject(double dfVal);
99  ~CPLJSONObject();
100  CPLJSONObject(const CPLJSONObject &other);
101  CPLJSONObject(CPLJSONObject &&other);
102  CPLJSONObject &operator=(const CPLJSONObject &other);
103  CPLJSONObject &operator=(CPLJSONObject &&other);
104 
105  // This method is not thread-safe
106  CPLJSONObject Clone() const;
107 
108  private:
109  explicit CPLJSONObject(const std::string &osName, JSONObjectH poJsonObject);
112  public:
113  // setters
114  void Add(const std::string &osName, const std::string &osValue);
115  void Add(const std::string &osName, const char *pszValue);
116  void Add(const std::string &osName, double dfValue);
117  void Add(const std::string &osName, int nValue);
118  void Add(const std::string &osName, GInt64 nValue);
119  void Add(const std::string &osName, uint64_t nValue);
120  void Add(const std::string &osName, const CPLJSONArray &oValue);
121  void Add(const std::string &osName, const CPLJSONObject &oValue);
122  void AddNoSplitName(const std::string &osName, const CPLJSONObject &oValue);
123  void Add(const std::string &osName, bool bValue);
124  void AddNull(const std::string &osName);
125 
126  void Set(const std::string &osName, const std::string &osValue);
127  void Set(const std::string &osName, const char *pszValue);
128  void Set(const std::string &osName, double dfValue);
129  void Set(const std::string &osName, int nValue);
130  void Set(const std::string &osName, GInt64 nValue);
131  void Set(const std::string &osName, uint64_t nValue);
132  void Set(const std::string &osName, bool bValue);
133  void SetNull(const std::string &osName);
134 
136  JSONObjectH GetInternalHandle() const
137  {
138  return m_poJsonObject;
139  }
142  // getters
143  std::string GetString(const std::string &osName,
144  const std::string &osDefault = "") const;
145  double GetDouble(const std::string &osName, double dfDefault = 0.0) const;
146  int GetInteger(const std::string &osName, int nDefault = 0) const;
147  GInt64 GetLong(const std::string &osName, GInt64 nDefault = 0) const;
148  bool GetBool(const std::string &osName, bool bDefault = false) const;
149  std::string ToString(const std::string &osDefault = "") const;
150  double ToDouble(double dfDefault = 0.0) const;
151  int ToInteger(int nDefault = 0) const;
152  GInt64 ToLong(GInt64 nDefault = 0) const;
153  bool ToBool(bool bDefault = false) const;
154  CPLJSONArray ToArray() const;
155  std::string Format(PrettyFormat eFormat) const;
156 
157  //
158  void Delete(const std::string &osName);
159  void DeleteNoSplitName(const std::string &osName);
160  CPLJSONArray GetArray(const std::string &osName) const;
161  CPLJSONObject GetObj(const std::string &osName) const;
162  CPLJSONObject operator[](const std::string &osName) const;
163  Type GetType() const;
165  std::string GetName() const
166  {
167  return m_osKey;
168  }
171  std::vector<CPLJSONObject> GetChildren() const;
172  bool IsValid() const;
173  void Deinit();
174 
175  protected:
177  CPLJSONObject GetObjectByPath(const std::string &osPath,
178  std::string &osName) const;
181  private:
182  JSONObjectH m_poJsonObject = nullptr;
183  std::string m_osKey{};
184 };
185 
189 class CPL_DLL CPLJSONArray : public CPLJSONObject
190 {
191  friend class CPLJSONObject;
192  friend class CPLJSONDocument;
193 
194  public:
196  CPLJSONArray();
197  explicit CPLJSONArray(const std::string &osName);
198  explicit CPLJSONArray(const CPLJSONObject &other);
199 
200  private:
201  explicit CPLJSONArray(const std::string &osName, JSONObjectH poJsonObject);
202 
203  class CPL_DLL ConstIterator
204  {
205  const CPLJSONArray &m_oSelf;
206  int m_nIdx;
207  mutable CPLJSONObject m_oObj{};
208 
209  public:
210  ConstIterator(const CPLJSONArray &oSelf, bool bStart)
211  : m_oSelf(oSelf), m_nIdx(bStart ? 0 : oSelf.Size())
212  {
213  }
214  ~ConstIterator() = default;
215  CPLJSONObject &operator*() const
216  {
217  m_oObj = m_oSelf[m_nIdx];
218  return m_oObj;
219  }
220  ConstIterator &operator++()
221  {
222  m_nIdx++;
223  return *this;
224  }
225  bool operator==(const ConstIterator &it) const
226  {
227  return m_nIdx == it.m_nIdx;
228  }
229  bool operator!=(const ConstIterator &it) const
230  {
231  return m_nIdx != it.m_nIdx;
232  }
233  };
234 
236  public:
237  int Size() const;
238  void AddNull();
239  void Add(const CPLJSONObject &oValue);
240  void Add(const std::string &osValue);
241  void Add(const char *pszValue);
242  void Add(double dfValue);
243  void Add(int nValue);
244  void Add(GInt64 nValue);
245  void Add(uint64_t nValue);
246  void Add(bool bValue);
247  CPLJSONObject operator[](int nIndex);
248  const CPLJSONObject operator[](int nIndex) const;
249 
251  ConstIterator begin() const
252  {
253  return ConstIterator(*this, true);
254  }
256  ConstIterator end() const
257  {
258  return ConstIterator(*this, false);
259  }
260 };
261 
265 class CPL_DLL CPLJSONDocument
266 {
267  public:
269  CPLJSONDocument();
270  ~CPLJSONDocument();
271  CPLJSONDocument(const CPLJSONDocument &other);
272  CPLJSONDocument &operator=(const CPLJSONDocument &other);
274  CPLJSONDocument &operator=(CPLJSONDocument &&other);
277  bool Save(const std::string &osPath) const;
278  std::string SaveAsString() const;
279 
280  CPLJSONObject GetRoot();
281  const CPLJSONObject GetRoot() const;
282  void SetRoot(const CPLJSONObject &oRoot);
283  bool Load(const std::string &osPath);
284  bool LoadMemory(const std::string &osStr);
285  bool LoadMemory(const GByte *pabyData, int nLength = -1);
286  bool LoadChunks(const std::string &osPath, size_t nChunkSize = 16384,
287  GDALProgressFunc pfnProgress = nullptr,
288  void *pProgressArg = nullptr);
289  bool LoadUrl(const std::string &osUrl, const char *const *papszOptions,
290  GDALProgressFunc pfnProgress = nullptr,
291  void *pProgressArg = nullptr);
292 
293  private:
294  mutable JSONObjectH m_poRootJsonObject;
295 };
296 
297 CPL_C_END
298 
299 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
300 extern "C++"
301 {
302  CPLStringList CPLParseKeyValueJson(const char *pszJson);
303 }
304 #endif
305 
306 #endif // CPL_JSON_H_INCLUDED
CPLJSONArray
The JSONArray class JSON array from JSONDocument.
Definition: cpl_json.h:189
GByte
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:196
CPLStringList
String list class designed around our use of C "char**" string lists.
Definition: cpl_string.h:437
CPLParseKeyValueJson
CPLStringList CPLParseKeyValueJson(const char *pszJson)
Return a string list of key/value pairs extracted from a JSON doc.
Definition: cpl_json.cpp:1629
GInt64
GIntBig GInt64
Signed 64 bit integer type.
Definition: cpl_port.h:247
CPL_C_START
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:306
CPLJSONObject::PrettyFormat
PrettyFormat
Json object format to string options.
Definition: cpl_json.h:79
CPLJSONObject
The CPLJSONArray class holds JSON object from CPLJSONDocument.
Definition: cpl_json.h:54
CPLJSONArray::end
ConstIterator end() const
Iterator to after last element.
Definition: cpl_json.h:256
CPL_C_END
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:310
CPLJSONObject::Type
Type
Json object types.
Definition: cpl_json.h:63
cpl_string.h
CPLJSONArray::Size
int Size() const
Get array size.
Definition: cpl_json.cpp:1452
CPLJSONArray::begin
ConstIterator begin() const
Iterator to first element.
Definition: cpl_json.h:251
CPLJSONDocument
The CPLJSONDocument class Wrapper class around json-c library.
Definition: cpl_json.h:265