gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

219 行
7.0KB

  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. #ifndef CPL_JSON_H_INCLUDED
  28. #define CPL_JSON_H_INCLUDED
  29. #include "cpl_progress.h"
  30. #include <string>
  31. #include <vector>
  32. /**
  33. * \file cpl_json.h
  34. *
  35. * Interface for read and write JSON documents
  36. */
  37. /*! @cond Doxygen_Suppress */
  38. typedef void *JSONObjectH;
  39. CPL_C_START
  40. class CPLJSONArray;
  41. /*! @endcond */
  42. /**
  43. * @brief The CPLJSONArray class holds JSON object from CPLJSONDocument
  44. */
  45. class CPL_DLL CPLJSONObject
  46. {
  47. friend class CPLJSONArray;
  48. friend class CPLJSONDocument;
  49. public:
  50. /**
  51. * Json object types
  52. */
  53. enum Type {
  54. Unknown,
  55. Null,
  56. Object,
  57. Array,
  58. Boolean,
  59. String,
  60. Integer,
  61. Long,
  62. Double
  63. };
  64. /**
  65. * Json object format to string options
  66. */
  67. enum PrettyFormat {
  68. Plain, ///< No extra whitespace or formatting applied
  69. Spaced, ///< Minimal whitespace inserted
  70. Pretty ///< Formatted output
  71. };
  72. public:
  73. /*! @cond Doxygen_Suppress */
  74. CPLJSONObject();
  75. explicit CPLJSONObject(const std::string &osName, const CPLJSONObject &oParent);
  76. ~CPLJSONObject();
  77. CPLJSONObject(const CPLJSONObject &other);
  78. CPLJSONObject &operator=(const CPLJSONObject &other);
  79. private:
  80. explicit CPLJSONObject(const std::string &osName, JSONObjectH poJsonObject);
  81. /*! @endcond */
  82. public:
  83. // setters
  84. void Add(const std::string &osName, const std::string &osValue);
  85. void Add(const std::string &osName, const char *pszValue);
  86. void Add(const std::string &osName, double dfValue);
  87. void Add(const std::string &osName, int nValue);
  88. void Add(const std::string &osName, GInt64 nValue);
  89. void Add(const std::string &osName, const CPLJSONArray &oValue);
  90. void Add(const std::string &osName, const CPLJSONObject &oValue);
  91. void Add(const std::string &osName, bool bValue);
  92. void AddNull(const std::string &osName);
  93. void Set(const std::string &osName, const std::string &osValue);
  94. void Set(const std::string &osName, const char *pszValue);
  95. void Set(const std::string &osName, double dfValue);
  96. void Set(const std::string &osName, int nValue);
  97. void Set(const std::string &osName, GInt64 nValue);
  98. void Set(const std::string &osName, bool bValue);
  99. void SetNull(const std::string &osName);
  100. /*! @cond Doxygen_Suppress */
  101. JSONObjectH GetInternalHandle() const { return m_poJsonObject; }
  102. /*! @endcond */
  103. // getters
  104. std::string GetString(const std::string &osName, const std::string &osDefault = "") const;
  105. double GetDouble(const std::string &osName, double dfDefault = 0.0) const;
  106. int GetInteger(const std::string &osName, int nDefault = 0) const;
  107. GInt64 GetLong(const std::string &osName, GInt64 nDefault = 0) const;
  108. bool GetBool(const std::string &osName, bool bDefault = false) const;
  109. std::string ToString(const std::string &osDefault = "") const;
  110. double ToDouble(double dfDefault = 0.0) const;
  111. int ToInteger(int nDefault = 0) const;
  112. GInt64 ToLong(GInt64 nDefault = 0) const;
  113. bool ToBool(bool bDefault = false) const;
  114. CPLJSONArray ToArray() const;
  115. std::string Format(enum PrettyFormat eFormat) const;
  116. //
  117. void Delete(const std::string &osName);
  118. CPLJSONArray GetArray(const std::string &osName) const;
  119. CPLJSONObject GetObj(const std::string &osName) const;
  120. CPLJSONObject operator[](const std::string &osName) const;
  121. enum Type GetType() const;
  122. /*! @cond Doxygen_Suppress */
  123. std::string GetName() const { return m_osKey; }
  124. /*! @endcond */
  125. std::vector<CPLJSONObject> GetChildren() const;
  126. bool IsValid() const;
  127. void Deinit();
  128. protected:
  129. /*! @cond Doxygen_Suppress */
  130. CPLJSONObject GetObjectByPath(const std::string &osPath, std::string &osName) const;
  131. /*! @endcond */
  132. private:
  133. JSONObjectH m_poJsonObject = nullptr;
  134. std::string m_osKey{};
  135. };
  136. /**
  137. * @brief The JSONArray class JSON array from JSONDocument
  138. */
  139. class CPL_DLL CPLJSONArray : public CPLJSONObject
  140. {
  141. friend class CPLJSONObject;
  142. friend class CPLJSONDocument;
  143. public:
  144. /*! @cond Doxygen_Suppress */
  145. CPLJSONArray();
  146. explicit CPLJSONArray(const std::string &osName);
  147. explicit CPLJSONArray(const CPLJSONObject &other);
  148. private:
  149. explicit CPLJSONArray(const std::string &osName, JSONObjectH poJsonObject);
  150. /*! @endcond */
  151. public:
  152. int Size() const;
  153. void Add(const CPLJSONObject &oValue);
  154. void Add(const std::string &osValue);
  155. void Add(const char* pszValue);
  156. void Add(double dfValue);
  157. void Add(int nValue);
  158. void Add(GInt64 nValue);
  159. void Add(bool bValue);
  160. CPLJSONObject operator[](int nIndex);
  161. const CPLJSONObject operator[](int nIndex) const;
  162. };
  163. /**
  164. * @brief The CPLJSONDocument class Wrapper class around json-c library
  165. */
  166. class CPL_DLL CPLJSONDocument
  167. {
  168. public:
  169. /*! @cond Doxygen_Suppress */
  170. CPLJSONDocument();
  171. ~CPLJSONDocument();
  172. CPLJSONDocument(const CPLJSONDocument &other);
  173. CPLJSONDocument& operator=(const CPLJSONDocument &other);
  174. /*! @endcond */
  175. bool Save(const std::string &osPath);
  176. std::string SaveAsString();
  177. CPLJSONObject GetRoot();
  178. bool Load(const std::string &osPath);
  179. bool LoadMemory(const std::string &osStr);
  180. bool LoadMemory(const GByte *pabyData, int nLength = -1);
  181. bool LoadChunks(const std::string &osPath, size_t nChunkSize = 16384,
  182. GDALProgressFunc pfnProgress = nullptr,
  183. void *pProgressArg = nullptr);
  184. bool LoadUrl(const std::string &osUrl, char **papszOptions,
  185. GDALProgressFunc pfnProgress = nullptr,
  186. void *pProgressArg = nullptr);
  187. private:
  188. JSONObjectH m_poRootJsonObject;
  189. };
  190. CPL_C_END
  191. #endif // CPL_JSON_H_INCLUDED
上海开阖软件有限公司 沪ICP备12045867号-1