json.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (C) 2013 Teluu Inc. (http://www.teluu.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #ifndef __PJSUA2_JSON_HPP__
  19. #define __PJSUA2_JSON_HPP__
  20. /**
  21. * @file pjsua2/persistent.hpp
  22. * @brief PJSUA2 Persistent Services
  23. */
  24. #include <pjsua2/persistent.hpp>
  25. #include <pjlib-util/json.h>
  26. #include <pj/pool.h>
  27. #include <string>
  28. /** PJSUA2 API is inside pj namespace */
  29. namespace pj
  30. {
  31. /**
  32. * @defgroup PJSUA2_JSON JSON Persistent Support
  33. * @ingroup PJSUA2_PERSISTENT
  34. * @{
  35. * Provides object serialization and deserialization to/from JSON document.
  36. */
  37. using std::string;
  38. /**
  39. * Persistent document (file) with JSON format.
  40. */
  41. class JsonDocument : public PersistentDocument
  42. {
  43. public:
  44. /** Default constructor */
  45. JsonDocument();
  46. /** Destructor */
  47. ~JsonDocument();
  48. /**
  49. * Load this document from a file.
  50. *
  51. * @param filename The file name.
  52. */
  53. virtual void loadFile(const string &filename) PJSUA2_THROW(Error);
  54. /**
  55. * Load this document from string.
  56. *
  57. * @param input The string.
  58. */
  59. virtual void loadString(const string &input) PJSUA2_THROW(Error);
  60. /**
  61. * Write this document to a file.
  62. *
  63. * @param filename The file name.
  64. */
  65. virtual void saveFile(const string &filename) PJSUA2_THROW(Error);
  66. /**
  67. * Write this document to string.
  68. */
  69. virtual string saveString() PJSUA2_THROW(Error);
  70. /**
  71. * Get the root container node for this document
  72. */
  73. virtual ContainerNode & getRootContainer() const;
  74. /**
  75. * An internal function to create JSON element.
  76. */
  77. pj_json_elem* allocElement() const;
  78. /**
  79. * An internal function to get the pool.
  80. */
  81. pj_pool_t* getPool();
  82. private:
  83. pj_caching_pool cp;
  84. mutable ContainerNode rootNode;
  85. mutable pj_json_elem *root;
  86. mutable pj_pool_t *pool;
  87. void initRoot() const;
  88. };
  89. /**
  90. * @} PJSUA2
  91. */
  92. } // namespace pj
  93. #endif /* __PJSUA2_JSON_HPP__ */