/qjson/src/serializer.h

https://github.com/kitech/karia2 · C Header · 189 lines · 34 code · 16 blank · 139 comment · 0 complexity · 19c9ad94f1b02b49998469d3d2033441 MD5 · raw file

  1. /* This file is part of qjson
  2. *
  3. * Copyright (C) 2009 Till Adam <adam@kde.org>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License version 2.1, as published by the Free Software Foundation.
  8. *
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this library; see the file COPYING.LIB. If not, write to
  17. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. */
  20. #ifndef QJSON_SERIALIZER_H
  21. #define QJSON_SERIALIZER_H
  22. #include "qjson_export.h"
  23. class QIODevice;
  24. class QString;
  25. class QVariant;
  26. namespace QJson {
  27. /**
  28. @brief How the indentation should work.
  29. \verbatim
  30. none (default) : { "foo" : 0, "foo1" : 1, "foo2" : [ { "foo3" : 3, "foo4" : 4 } ] }
  31. compact : {"foo":0,"foo1":1,"foo2":[{"foo3":3,"foo4":4}]}
  32. minimum : { "foo" : 0, "foo1" : 1, "foo2" : [
  33. { "foo3" : 3, "foo4" : 4 }
  34. ] }
  35. medium : {
  36. "foo" : 0, "foo1" : 1, "foo2" : [
  37. {
  38. "foo3" : 3, "foo4" : 4
  39. }
  40. ]
  41. }
  42. full : {
  43. "foo" : 0,
  44. "foo1" : 1,
  45. "foo2" : [
  46. {
  47. "foo3" : 3,
  48. "foo4" : 4
  49. }
  50. ]
  51. }
  52. \endverbatim
  53. */
  54. enum IndentMode {
  55. IndentNone,
  56. IndentCompact,
  57. IndentMinimum,
  58. IndentMedium,
  59. IndentFull
  60. };
  61. /**
  62. * @brief Main class used to convert QVariant objects to JSON data.
  63. *
  64. * QVariant objects are converted to a string containing the JSON data.
  65. *
  66. *
  67. * Usage:
  68. *
  69. * \code
  70. * QVariantList people;
  71. *
  72. * QVariantMap bob;
  73. * bob.insert("Name", "Bob");
  74. * bob.insert("Phonenumber", 123);
  75. *
  76. * QVariantMap alice;
  77. * alice.insert("Name", "Alice");
  78. * alice.insert("Phonenumber", 321);
  79. *
  80. * people << bob << alice;
  81. *
  82. * QJson::Serializer serializer;
  83. * bool ok;
  84. * QByteArray json = serializer.serialize(people, &ok);
  85. *
  86. * if (ok) {
  87. * qDebug() << json;
  88. * } else {
  89. * qCritical() << "Something went wrong:" << serializer.errorMessage();
  90. * }
  91. * \endcode
  92. *
  93. * The output will be:
  94. *
  95. * \code
  96. * "[ { "Name" : "Bob", "Phonenumber" : 123 },
  97. * { "Name" : "Alice", "Phonenumber" : 321 } ]"
  98. * \endcode
  99. *
  100. * It's possible to tune the indentation level of the resulting string. \sa setIndentMode
  101. */
  102. class QJSON_EXPORT Serializer {
  103. public:
  104. Serializer();
  105. ~Serializer();
  106. /**
  107. * This method generates a textual JSON representation and outputs it to the
  108. * passed in I/O Device.
  109. * @param variant The JSON document in its in-memory representation as generated by the
  110. * parser.
  111. * @param out Input output device
  112. * @param ok if a conversion error occurs, *ok is set to false; otherwise *ok is set to true
  113. */
  114. void serialize( const QVariant& variant, QIODevice* out, bool* ok);
  115. /**
  116. * This is a method provided for convenience. It turns the passed in in-memory
  117. * representation of the JSON document into a textual one, which is returned.
  118. * If the returned string is empty, the document was empty. If it was null, there
  119. * was a parsing error.
  120. *
  121. * @param variant The JSON document in its in-memory representation as generated by the
  122. * parser.
  123. *
  124. * \deprecated This method is going to be removed with the next major release of QJson.
  125. */
  126. QByteArray serialize( const QVariant& variant);
  127. /**
  128. * This is a method provided for convenience. It turns the passed in in-memory
  129. * representation of the JSON document into a textual one, which is returned.
  130. * If the returned string is empty, the document was empty. If it was null, there
  131. * was a parsing error.
  132. *
  133. * @param variant The JSON document in its in-memory representation as generated by the
  134. * parser.
  135. * @param ok if a conversion error occurs, *ok is set to false; otherwise *ok is set to true
  136. */
  137. QByteArray serialize( const QVariant& variant, bool *ok);
  138. /**
  139. * Allow or disallow writing of NaN and/or Infinity (as an extension to QJson)
  140. */
  141. void allowSpecialNumbers(bool allow);
  142. /**
  143. * Is Nan and/or Infinity allowed?
  144. */
  145. bool specialNumbersAllowed() const;
  146. /**
  147. * set output indentation mode as defined in QJson::IndentMode
  148. */
  149. void setIndentMode(IndentMode mode = QJson::IndentNone);
  150. /**
  151. * set double precision used while converting Double
  152. * \sa QByteArray::number
  153. */
  154. void setDoublePrecision(int precision);
  155. /**
  156. * Returns one of the indentation modes defined in QJson::IndentMode
  157. */
  158. IndentMode indentMode() const;
  159. /**
  160. * Returns the error message
  161. */
  162. QString errorMessage() const;
  163. private:
  164. Q_DISABLE_COPY(Serializer)
  165. class SerializerPrivate;
  166. SerializerPrivate* const d;
  167. };
  168. }
  169. #endif // QJSON_SERIALIZER_H