OGR
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  void AddNull(const std::string &osName);
105 
106  void Set(const std::string &osName, const std::string &osValue);
107  void Set(const std::string &osName, const char *pszValue);
108  void Set(const std::string &osName, double dfValue);
109  void Set(const std::string &osName, int nValue);
110  void Set(const std::string &osName, GInt64 nValue);
111  void Set(const std::string &osName, bool bValue);
112  void SetNull(const std::string &osName);
113 
115  JSONObjectH GetInternalHandle() const { return m_poJsonObject; }
118  // getters
119  std::string GetString(const std::string &osName, const std::string &osDefault = "") const;
120  double GetDouble(const std::string &osName, double dfDefault = 0.0) const;
121  int GetInteger(const std::string &osName, int nDefault = 0) const;
122  GInt64 GetLong(const std::string &osName, GInt64 nDefault = 0) const;
123  bool GetBool(const std::string &osName, bool bDefault = false) const;
124  std::string ToString(const std::string &osDefault = "") const;
125  double ToDouble(double dfDefault = 0.0) const;
126  int ToInteger(int nDefault = 0) const;
127  GInt64 ToLong(GInt64 nDefault = 0) const;
128  bool ToBool(bool bDefault = false) const;
129  CPLJSONArray ToArray() const;
130  std::string Format(enum PrettyFormat eFormat) const;
131 
132  //
133  void Delete(const std::string &osName);
134  CPLJSONArray GetArray(const std::string &osName) const;
135  CPLJSONObject GetObj(const std::string &osName) const;
136  CPLJSONObject operator[](const std::string &osName) const;
137  enum Type GetType() const;
139  std::string GetName() const { return m_osKey; }
142  std::vector<CPLJSONObject> GetChildren() const;
143  bool IsValid() const;
144  void Deinit();
145 
146 protected:
148  CPLJSONObject GetObjectByPath(const std::string &osPath, std::string &osName) const;
151 private:
152  JSONObjectH m_poJsonObject = nullptr;
153  std::string m_osKey{};
154 };
155 
159 class CPL_DLL CPLJSONArray : public CPLJSONObject
160 {
161  friend class CPLJSONObject;
162  friend class CPLJSONDocument;
163 public:
165  CPLJSONArray();
166  explicit CPLJSONArray(const std::string &osName);
167  explicit CPLJSONArray(const CPLJSONObject &other);
168 
169 private:
170  explicit CPLJSONArray(const std::string &osName, JSONObjectH poJsonObject);
172 public:
173  int Size() const;
174  void Add(const CPLJSONObject &oValue);
175  void Add(const std::string &osValue);
176  void Add(const char* pszValue);
177  void Add(double dfValue);
178  void Add(int nValue);
179  void Add(GInt64 nValue);
180  void Add(bool bValue);
181  CPLJSONObject operator[](int nIndex);
182  const CPLJSONObject operator[](int nIndex) const;
183 };
184 
188 class CPL_DLL CPLJSONDocument
189 {
190 public:
192  CPLJSONDocument();
193  ~CPLJSONDocument();
194  CPLJSONDocument(const CPLJSONDocument &other);
195  CPLJSONDocument& operator=(const CPLJSONDocument &other);
198  bool Save(const std::string &osPath);
199  std::string SaveAsString();
200 
201  CPLJSONObject GetRoot();
202  bool Load(const std::string &osPath);
203  bool LoadMemory(const std::string &osStr);
204  bool LoadMemory(const GByte *pabyData, int nLength = -1);
205  bool LoadChunks(const std::string &osPath, size_t nChunkSize = 16384,
206  GDALProgressFunc pfnProgress = nullptr,
207  void *pProgressArg = nullptr);
208  bool LoadUrl(const std::string &osUrl, char **papszOptions,
209  GDALProgressFunc pfnProgress = nullptr,
210  void *pProgressArg = nullptr);
211 
212 private:
213  JSONObjectH m_poRootJsonObject;
214 };
215 
216 CPL_C_END
217 
218 #endif // CPL_JSON_H_INCLUDED
CPLJSONObject::ToBool
bool ToBool(bool bDefault=false) const
Definition: cpl_json.cpp:966
CPLJSONArray
The JSONArray class JSON array from JSONDocument.
Definition: cpl_json.h:160
CPLJSONDocument::LoadChunks
bool LoadChunks(const std::string &osPath, size_t nChunkSize=16384, GDALProgressFunc pfnProgress=nullptr, void *pProgressArg=nullptr)
Definition: cpl_json.cpp:217
CPLJSONObject::GetInteger
int GetInteger(const std::string &osName, int nDefault=0) const
Definition: cpl_json.cpp:865
CSLTokenizeString2
char ** CSLTokenizeString2(const char *pszString, const char *pszDelimiter, int nCSLTFlags)
Definition: cpl_string.cpp:836
GByte
unsigned char GByte
Definition: cpl_port.h:215
VSIFReadL
size_t VSIFReadL(void *, size_t, size_t, VSILFILE *) EXPERIMENTAL_CPL_WARN_UNUSED_RESULT
Read bytes from file.
Definition: cpl_vsil.cpp:1690
CPLJSONObject::ToDouble
double ToDouble(double dfDefault=0.0) const
Definition: cpl_json.cpp:849
cpl_error.h
CPLJSONDocument::LoadMemory
bool LoadMemory(const std::string &osStr)
Definition: cpl_json.cpp:199
CPLHTTPDestroyResult
void CPLHTTPDestroyResult(CPLHTTPResult *psResult)
Clean the memory associated with the return value of CPLHTTPFetch()
Definition: cpl_http.cpp:2080
CPLJSONArray::Add
void Add(const CPLJSONObject &oValue)
Definition: cpl_json.cpp:1178
CPLStringList
String list class designed around our use of C "char**" string lists.
Definition: cpl_string.h:439
CPLJSONObject::Plain
@ Plain
No extra whitespace or formatting applied.
Definition: cpl_json.h:77
CPLJSONObject::Deinit
void Deinit()
Definition: cpl_json.cpp:1122
cpl_vsi.h
CSLFetchNameValueDef
const char * CSLFetchNameValueDef(CSLConstList papszStrList, const char *pszName, const char *pszDefault)
Definition: cpl_string.cpp:1646
CPLJSONArray::operator[]
CPLJSONObject operator[](int nIndex)
Definition: cpl_json.cpp:1272
CPLJSONDocument::Load
bool Load(const std::string &osPath)
Definition: cpl_json.cpp:143
CPLJSONObject::ToArray
CPLJSONArray ToArray() const
Definition: cpl_json.cpp:980
GInt64
GIntBig GInt64
Definition: cpl_port.h:267
CPLJSONObject::Spaced
@ Spaced
Minimal whitespace inserted.
Definition: cpl_json.h:78
VSIFWriteL
size_t VSIFWriteL(const void *, size_t, size_t, VSILFILE *) EXPERIMENTAL_CPL_WARN_UNUSED_RESULT
Write bytes to file.
Definition: cpl_vsil.cpp:1804
CPLE_NoWriteAccess
#define CPLE_NoWriteAccess
Definition: cpl_error.h:113
CPLJSONObject::GetString
std::string GetString(const std::string &osName, const std::string &osDefault="") const
Definition: cpl_json.cpp:800
VSIFCloseL
int VSIFCloseL(VSILFILE *) EXPERIMENTAL_CPL_WARN_UNUSED_RESULT
Close file.
Definition: cpl_vsil.cpp:1472
CPLJSONObject::ToLong
GInt64 ToLong(GInt64 nDefault=0) const
Definition: cpl_json.cpp:907
cpl_http.h
CPLJSONObject::ToString
std::string ToString(const std::string &osDefault="") const
Definition: cpl_json.cpp:814
CPLHTTPFetchEx
CPLHTTPResult * CPLHTTPFetchEx(const char *pszURL, CSLConstList papszOptions, GDALProgressFunc pfnProgress, void *pProgressArg, CPLHTTPFetchWriteFunc pfnWrite, void *pWriteArg)
Definition: cpl_http.cpp:664
CPLJSONObject::Set
void Set(const std::string &osName, const std::string &osValue)
Definition: cpl_json.cpp:629
CPLJSONObject::Pretty
@ Pretty
Formatted output.
Definition: cpl_json.h:79
CPL_C_START
#define CPL_C_START
Definition: cpl_port.h:337
CPLJSONObject::PrettyFormat
PrettyFormat
Definition: cpl_json.h:76
CPLJSONObject::Format
std::string Format(enum PrettyFormat eFormat) const
Definition: cpl_json.cpp:995
CPLJSONObject
The CPLJSONArray class holds JSON object from CPLJSONDocument.
Definition: cpl_json.h:54
CPLHTTPResult
Definition: cpl_http.h:66
CPLJSONDocument::SaveAsString
std::string SaveAsString()
Definition: cpl_json.cpp:107
CPL_C_END
#define CPL_C_END
Definition: cpl_port.h:339
VSIStatL
int VSIStatL(const char *, VSIStatBufL *)
Get filesystem object info.
Definition: cpl_vsil.cpp:737
CPLMalloc
void * CPLMalloc(size_t)
Definition: cpl_conv.cpp:168
CPLJSONObject::Type
Type
Definition: cpl_json.h:61
CPLJSONObject::GetBool
bool GetBool(const std::string &osName, bool bDefault=false) const
Definition: cpl_json.cpp:923
CPLJSONObject::ToInteger
int ToInteger(int nDefault=0) const
Definition: cpl_json.cpp:878
CPLJSONObject::GetArray
CPLJSONArray GetArray(const std::string &osName) const
Definition: cpl_json.cpp:721
cpl_json.h
CPLSPrintf
const char * CPLSPrintf(const char *fmt,...)
Definition: cpl_string.cpp:977
CPLJSONObject::Add
void Add(const std::string &osName, const std::string &osValue)
Definition: cpl_json.cpp:431
CPLJSONArray::Size
int Size() const
Definition: cpl_json.cpp:1165
CPLError
void CPLError(CPLErr eErrClass, CPLErrorNum err_no, const char *fmt,...)
Definition: cpl_error.cpp:308
vsi_l_offset
GUIntBig vsi_l_offset
Definition: cpl_vsi.h:140
VSIFree
void VSIFree(void *)
Definition: cpl_vsisimple.cpp:827
CPLJSONObject::GetLong
GInt64 GetLong(const std::string &osName, GInt64 nDefault=0) const
Definition: cpl_json.cpp:894
CPLE_NotSupported
#define CPLE_NotSupported
Definition: cpl_error.h:109
CPLJSONObject::GetObj
CPLJSONObject GetObj(const std::string &osName) const
Definition: cpl_json.cpp:747
CPLJSONDocument::GetRoot
CPLJSONObject GetRoot()
Definition: cpl_json.cpp:119
CPLHTTPResult::pszErrBuf
char * pszErrBuf
Definition: cpl_http.h:74
CPLJSONObject::operator[]
CPLJSONObject operator[](const std::string &osName) const
Definition: cpl_json.cpp:770
CPLHTTPResult::nStatus
int nStatus
Definition: cpl_http.h:68
VSIStatBufL
struct stat64 VSIStatBufL
Definition: cpl_vsi.h:194
CPLJSONDocument
The CPLJSONDocument class Wrapper class around json-c library.
Definition: cpl_json.h:189
CPLJSONObject::GetChildren
std::vector< CPLJSONObject > GetChildren() const
Get json object children.
Definition: cpl_json.cpp:939
VSIFOpenL
VSILFILE * VSIFOpenL(const char *, const char *)
Open file.
Definition: cpl_vsil.cpp:997
CPLJSONDocument::LoadUrl
bool LoadUrl(const std::string &osUrl, char **papszOptions, GDALProgressFunc pfnProgress=nullptr, void *pProgressArg=nullptr)
Definition: cpl_json.cpp:329
CPLJSONDocument::Save
bool Save(const std::string &osPath)
Definition: cpl_json.cpp:82
CPLJSONObject::GetType
enum Type GetType() const
Definition: cpl_json.cpp:1081
CPLJSONObject::Delete
void Delete(const std::string &osName)
Definition: cpl_json.cpp:781
CPLJSONObject::SetNull
void SetNull(const std::string &osName)
Definition: cpl_json.cpp:708
CPLJSONObject::IsValid
bool IsValid() const
Definition: cpl_json.cpp:1111
VSIIngestFile
int VSIIngestFile(VSILFILE *fp, const char *pszFilename, GByte **ppabyRet, vsi_l_offset *pnSize, GIntBig nMaxSize)
Ingest a file into memory.
Definition: cpl_vsil.cpp:2044
CPLE_FileIO
#define CPLE_FileIO
Definition: cpl_error.h:103
CPLJSONObject::AddNull
void AddNull(const std::string &osName)
Definition: cpl_json.cpp:609
CPLFree
#define CPLFree
Definition: cpl_conv.h:81
VSILFILE
FILE VSILFILE
Definition: cpl_vsi.h:156
CPLJSONObject::GetDouble
double GetDouble(const std::string &osName, double dfDefault=0.0) const
Definition: cpl_json.cpp:836
CPLE_AppDefined
#define CPLE_AppDefined
Definition: cpl_error.h:99
GUInt32
unsigned int GUInt32
Definition: cpl_port.h:207

Generated for GDAL by doxygen 1.8.20.