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 
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 &operator=(const CPLJSONObject &other);
89  CPLJSONObject &operator=(CPLJSONObject &&other);
90 
91 private:
92  explicit CPLJSONObject(const std::string &osName, JSONObjectH poJsonObject);
95 public:
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; }
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  CPLJSONArray GetArray(const std::string &osName) const;
137  CPLJSONObject GetObj(const std::string &osName) const;
138  CPLJSONObject operator[](const std::string &osName) const;
139  Type GetType() const;
141  std::string GetName() const { return m_osKey; }
144  std::vector<CPLJSONObject> GetChildren() const;
145  bool IsValid() const;
146  void Deinit();
147 
148 protected:
150  CPLJSONObject GetObjectByPath(const std::string &osPath, std::string &osName) const;
153 private:
154  JSONObjectH m_poJsonObject = nullptr;
155  std::string m_osKey{};
156 };
157 
161 class CPL_DLL CPLJSONArray : public CPLJSONObject
162 {
163  friend class CPLJSONObject;
164  friend class CPLJSONDocument;
165 public:
167  CPLJSONArray();
168  explicit CPLJSONArray(const std::string &osName);
169  explicit CPLJSONArray(const CPLJSONObject &other);
170 
171 private:
172  explicit CPLJSONArray(const std::string &osName, JSONObjectH poJsonObject);
173 
174  class CPL_DLL ConstIterator
175  {
176  const CPLJSONArray& m_oSelf;
177  int m_nIdx;
178  mutable CPLJSONObject m_oObj{};
179 
180  public:
181  ConstIterator(const CPLJSONArray& oSelf, bool bStart): m_oSelf(oSelf), m_nIdx(bStart ? 0 : oSelf.Size()) {}
182  ~ConstIterator() = default;
183  CPLJSONObject& operator*() const { m_oObj = m_oSelf[m_nIdx]; return m_oObj; }
184  ConstIterator& operator++() { m_nIdx ++; return *this; }
185  bool operator==(const ConstIterator& it) const { return m_nIdx == it.m_nIdx; }
186  bool operator!=(const ConstIterator& it) const { return m_nIdx != it.m_nIdx; }
187  };
188 
190 public:
191  int Size() const;
192  void Add(const CPLJSONObject &oValue);
193  void Add(const std::string &osValue);
194  void Add(const char* pszValue);
195  void Add(double dfValue);
196  void Add(int nValue);
197  void Add(GInt64 nValue);
198  void Add(bool bValue);
199  CPLJSONObject operator[](int nIndex);
200  const CPLJSONObject operator[](int nIndex) const;
201 
203  ConstIterator begin() const { return ConstIterator(*this, true); }
205  ConstIterator end() const { return ConstIterator(*this, false); }
206 };
207 
211 class CPL_DLL CPLJSONDocument
212 {
213 public:
215  CPLJSONDocument();
216  ~CPLJSONDocument();
217  CPLJSONDocument(const CPLJSONDocument &other);
218  CPLJSONDocument& operator=(const CPLJSONDocument &other);
221  bool Save(const std::string &osPath) const;
222  std::string SaveAsString() const;
223 
224  CPLJSONObject GetRoot();
225  const CPLJSONObject GetRoot() const;
226  bool Load(const std::string &osPath);
227  bool LoadMemory(const std::string &osStr);
228  bool LoadMemory(const GByte *pabyData, int nLength = -1);
229  bool LoadChunks(const std::string &osPath, size_t nChunkSize = 16384,
230  GDALProgressFunc pfnProgress = nullptr,
231  void *pProgressArg = nullptr);
232  bool LoadUrl(const std::string &osUrl, const char* const* papszOptions,
233  GDALProgressFunc pfnProgress = nullptr,
234  void *pProgressArg = nullptr);
235 
236 private:
237  mutable JSONObjectH m_poRootJsonObject;
238 };
239 
240 CPL_C_END
241 
242 #endif // CPL_JSON_H_INCLUDED
The JSONArray class JSON array from JSONDocument.
Definition: cpl_json.h:162
int Size() const
Get array size.
Definition: cpl_json.cpp:1247
ConstIterator begin() const
Iterator to first element.
Definition: cpl_json.h:203
ConstIterator end() const
Iterator to after last element.
Definition: cpl_json.h:205
The CPLJSONDocument class Wrapper class around json-c library.
Definition: cpl_json.h:212
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
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:339
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:337
GIntBig GInt64
Signed 64 bit integer type.
Definition: cpl_port.h:267
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:215