json.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * \file json.h
  3. */
  4. #ifndef JSON_H
  5. #define JSON_H
  6. #include <QVariant>
  7. #include <QString>
  8. /**
  9. * \enum JsonToken
  10. */
  11. enum JsonToken
  12. {
  13. JsonTokenNone = 0,
  14. JsonTokenCurlyOpen = 1,
  15. JsonTokenCurlyClose = 2,
  16. JsonTokenSquaredOpen = 3,
  17. JsonTokenSquaredClose = 4,
  18. JsonTokenColon = 5,
  19. JsonTokenComma = 6,
  20. JsonTokenString = 7,
  21. JsonTokenNumber = 8,
  22. JsonTokenTrue = 9,
  23. JsonTokenFalse = 10,
  24. JsonTokenNull = 11
  25. };
  26. /**
  27. * \class Json
  28. * \brief A JSON data parser
  29. *
  30. * Json parses a JSON data into a QVariant hierarchy.
  31. */
  32. class Json
  33. {
  34. public:
  35. /**
  36. * Parse a JSON string
  37. *
  38. * \param json The JSON data
  39. */
  40. static QVariant parse(const QString &json);
  41. /**
  42. * Parse a JSON string
  43. *
  44. * \param json The JSON data
  45. * \param success The success of the parsing
  46. */
  47. static QVariant parse(const QString &json, bool &success);
  48. /**
  49. * This method generates a textual JSON representation
  50. *
  51. * \param data The JSON data generated by the parser.
  52. * \param success The success of the serialization
  53. */
  54. static QByteArray serialize(const QVariant &data);
  55. /**
  56. * This method generates a textual JSON representation
  57. *
  58. * \param data The JSON data generated by the parser.
  59. * \param success The success of the serialization
  60. *
  61. * \return QByteArray Textual JSON representation
  62. */
  63. static QByteArray serialize(const QVariant &data, bool &success);
  64. private:
  65. /**
  66. * Parses a value starting from index
  67. *
  68. * \param json The JSON data
  69. * \param index The start index
  70. * \param success The success of the parse process
  71. *
  72. * \return QVariant The parsed value
  73. */
  74. static QVariant parseValue(const QString &json, int &index,
  75. bool &success);
  76. /**
  77. * Parses an object starting from index
  78. *
  79. * \param json The JSON data
  80. * \param index The start index
  81. * \param success The success of the object parse
  82. *
  83. * \return QVariant The parsed object map
  84. */
  85. static QVariant parseObject(const QString &json, int &index,
  86. bool &success);
  87. /**
  88. * Parses an array starting from index
  89. *
  90. * \param json The JSON data
  91. * \param index The starting index
  92. * \param success The success of the array parse
  93. *
  94. * \return QVariant The parsed variant array
  95. */
  96. static QVariant parseArray(const QString &json, int &index,
  97. bool &success);
  98. /**
  99. * Parses a string starting from index
  100. *
  101. * \param json The JSON data
  102. * \param index The starting index
  103. * \param success The success of the string parse
  104. *
  105. * \return QVariant The parsed string
  106. */
  107. static QVariant parseString(const QString &json, int &index,
  108. bool &success);
  109. /**
  110. * Parses a number starting from index
  111. *
  112. * \param json The JSON data
  113. * \param index The starting index
  114. *
  115. * \return QVariant The parsed number
  116. */
  117. static QVariant parseNumber(const QString &json, int &index);
  118. /**
  119. * Get the last index of a number starting from index
  120. *
  121. * \param json The JSON data
  122. * \param index The starting index
  123. *
  124. * \return The last index of the number
  125. */
  126. static int lastIndexOfNumber(const QString &json, int index);
  127. /**
  128. * Skip unwanted whitespace symbols starting from index
  129. *
  130. * \param json The JSON data
  131. * \param index The start index
  132. */
  133. static void eatWhitespace(const QString &json, int &index);
  134. /**
  135. * Check what token lies ahead
  136. *
  137. * \param json The JSON data
  138. * \param index The starting index
  139. *
  140. * \return int The upcoming token
  141. */
  142. static int lookAhead(const QString &json, int index);
  143. /**
  144. * Get the next JSON token
  145. *
  146. * \param json The JSON data
  147. * \param index The starting index
  148. *
  149. * \return int The next JSON token
  150. */
  151. static int nextToken(const QString &json, int &index);
  152. };
  153. #endif //JSON_H