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 Type {
62  Unknown,
63  Null,
64  Object,
65  Array,
66  Boolean,
67  String,
68  Integer,
69  Long,
70  Double
71  };
72 
76  enum PrettyFormat {
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 
90 private:
91  explicit CPLJSONObject(const std::string &osName, JSONObjectH poJsonObject);
94 public:
95  // setters
96  void Add(const std::string &osName, const std::string &osValue);
97  void Add(const std::string &osName, const char *pszValue);
98  void Add(const std::string &osName, double dfValue);
99  void Add(const std::string &osName, int nValue);
100  void Add(const std::string &osName, GInt64 nValue);
101  void Add(const std::string &osName, const CPLJSONArray &oValue);
102  void Add(const std::string &osName, const CPLJSONObject &oValue);
103  void Add(const std::string &osName, bool bValue);
104 
105  void Set(const std::string &osName, const std::string &osValue);
106  void Set(const std::string &osName, const char *pszValue);
107  void Set(const std::string &osName, double dfValue);
108  void Set(const std::string &osName, int nValue);
109  void Set(const std::string &osName, GInt64 nValue);
110  void Set(const std::string &osName, bool bValue);
111 
113  JSONObjectH GetInternalHandle() const { return m_poJsonObject; }
116  // getters
117  std::string GetString(const std::string &osName, const std::string &osDefault = "") const;
118  double GetDouble(const std::string &osName, double dfDefault = 0.0) const;
119  int GetInteger(const std::string &osName, int nDefault = 0) const;
120  GInt64 GetLong(const std::string &osName, GInt64 nDefault = 0) const;
121  bool GetBool(const std::string &osName, bool bDefault = false) const;
122  std::string ToString(const std::string &osDefault = "") const;
123  double ToDouble(double dfDefault = 0.0) const;
124  int ToInteger(int nDefault = 0) const;
125  GInt64 ToLong(GInt64 nDefault = 0) const;
126  bool ToBool(bool bDefault = false) const;
127  std::string Format(enum PrettyFormat eFormat) const;
128 
129  //
130  void Delete(const std::string &osName);
131  CPLJSONArray GetArray(const std::string &osName) const;
132  CPLJSONObject GetObj(const std::string &osName) const;
133  CPLJSONObject operator[](const std::string &osName) const;
134  enum Type GetType() const;
136  std::string GetName() const { return m_osKey; }
139  std::vector<CPLJSONObject> GetChildren() const;
140  bool IsValid() const;
141  void Deinit();
142 
143 protected:
145  CPLJSONObject GetObjectByPath(const std::string &osPath, std::string &osName) const;
148 private:
149  JSONObjectH m_poJsonObject;
150  std::string m_osKey;
151 };
152 
156 class CPL_DLL CPLJSONArray : public CPLJSONObject
157 {
158  friend class CPLJSONObject;
159  friend class CPLJSONDocument;
160 public:
162  CPLJSONArray();
163  explicit CPLJSONArray(const std::string &osName);
164  explicit CPLJSONArray(const CPLJSONObject &other);
165 
166 private:
167  explicit CPLJSONArray(const std::string &osName, JSONObjectH poJsonObject);
169 public:
170  int Size() const;
171  void Add(const CPLJSONObject &oValue);
172  void Add(const std::string &osValue);
173  void Add(const char* pszValue);
174  void Add(double dfValue);
175  void Add(int nValue);
176  void Add(GInt64 nValue);
177  void Add(bool bValue);
178  CPLJSONObject operator[](int nIndex);
179  const CPLJSONObject operator[](int nIndex) const;
180 };
181 
185 class CPL_DLL CPLJSONDocument
186 {
187 public:
189  CPLJSONDocument();
190  ~CPLJSONDocument();
191  CPLJSONDocument(const CPLJSONDocument &other);
192  CPLJSONDocument& operator=(const CPLJSONDocument &other);
195  bool Save(const std::string &osPath);
196  CPLJSONObject GetRoot();
197  bool Load(const std::string &osPath);
198  bool LoadMemory(const GByte *pabyData, int nLength = -1);
199  bool LoadChunks(const std::string &osPath, size_t nChunkSize = 16384,
200  GDALProgressFunc pfnProgress = nullptr,
201  void *pProgressArg = nullptr);
202  bool LoadUrl(const std::string &osUrl, char **papszOptions,
203  GDALProgressFunc pfnProgress = nullptr,
204  void *pProgressArg = nullptr);
205 
206 private:
207  JSONObjectH m_poRootJsonObject;
208 };
209 
210 CPL_C_END
211 
212 #endif // CPL_JSON_H_INCLUDED
void Add(const std::string &osName, const std::string &osValue)
Add new key - value pair to json object.
Definition: cpl_json.cpp:399
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:331
PrettyFormat
Json object format to string options.
Definition: cpl_json.h:76
No extra whitespace or formatting applied.
Definition: cpl_json.h:77
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:213
Type
Json object types.
Definition: cpl_json.h:61
Minimal whitespace inserted.
Definition: cpl_json.h:78
The CPLJSONArray class holds JSON object from CPLJSONDocument.
Definition: cpl_json.h:53
CPLJSONObject operator[](const std::string &osName) const
Get value by key.
Definition: cpl_json.cpp:700
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:333
The CPLJSONDocument class Wrapper class around json-c library.
Definition: cpl_json.h:185
GIntBig GInt64
Signed 64 bit integer type.
Definition: cpl_port.h:255
The JSONArray class JSON array from JSONDocument.
Definition: cpl_json.h:156

Generated for GDAL by doxygen 1.8.8.