reader.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
  2. // Distributed under MIT license, or public domain if desired and
  3. // recognized in your jurisdiction.
  4. // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
  5. #ifndef JSON_READER_H_INCLUDED
  6. #define JSON_READER_H_INCLUDED
  7. #if !defined(JSON_IS_AMALGAMATION)
  8. #include "json_features.h"
  9. #include "value.h"
  10. #endif // if !defined(JSON_IS_AMALGAMATION)
  11. #include <deque>
  12. #include <iosfwd>
  13. #include <istream>
  14. #include <stack>
  15. #include <string>
  16. // Disable warning C4251: <data member>: <type> needs to have dll-interface to
  17. // be used by...
  18. #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
  19. #pragma warning(push)
  20. #pragma warning(disable : 4251)
  21. #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
  22. #pragma pack(push)
  23. #pragma pack()
  24. namespace Json {
  25. /** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a
  26. * Value.
  27. *
  28. * \deprecated Use CharReader and CharReaderBuilder.
  29. */
  30. class JSON_API Reader {
  31. public:
  32. using Char = char;
  33. using Location = const Char*;
  34. /** \brief An error tagged with where in the JSON text it was encountered.
  35. *
  36. * The offsets give the [start, limit) range of bytes within the text. Note
  37. * that this is bytes, not codepoints.
  38. */
  39. struct StructuredError {
  40. ptrdiff_t offset_start;
  41. ptrdiff_t offset_limit;
  42. String message;
  43. };
  44. /** \brief Constructs a Reader allowing all features for parsing.
  45. * \deprecated Use CharReader and CharReaderBuilder.
  46. */
  47. Reader();
  48. /** \brief Constructs a Reader allowing the specified feature set for parsing.
  49. * \deprecated Use CharReader and CharReaderBuilder.
  50. */
  51. Reader(const Features& features);
  52. /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a>
  53. * document.
  54. *
  55. * \param document UTF-8 encoded string containing the document
  56. * to read.
  57. * \param[out] root Contains the root value of the document if it
  58. * was successfully parsed.
  59. * \param collectComments \c true to collect comment and allow writing
  60. * them back during serialization, \c false to
  61. * discard comments. This parameter is ignored
  62. * if Features::allowComments_ is \c false.
  63. * \return \c true if the document was successfully parsed, \c false if an
  64. * error occurred.
  65. */
  66. bool parse(const std::string& document, Value& root,
  67. bool collectComments = true);
  68. /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a>
  69. * document.
  70. *
  71. * \param beginDoc Pointer on the beginning of the UTF-8 encoded
  72. * string of the document to read.
  73. * \param endDoc Pointer on the end of the UTF-8 encoded string
  74. * of the document to read. Must be >= beginDoc.
  75. * \param[out] root Contains the root value of the document if it
  76. * was successfully parsed.
  77. * \param collectComments \c true to collect comment and allow writing
  78. * them back during serialization, \c false to
  79. * discard comments. This parameter is ignored
  80. * if Features::allowComments_ is \c false.
  81. * \return \c true if the document was successfully parsed, \c false if an
  82. * error occurred.
  83. */
  84. bool parse(const char* beginDoc, const char* endDoc, Value& root,
  85. bool collectComments = true);
  86. /// \brief Parse from input stream.
  87. /// \see Json::operator>>(std::istream&, Json::Value&).
  88. bool parse(IStream& is, Value& root, bool collectComments = true);
  89. /** \brief Returns a user friendly string that list errors in the parsed
  90. * document.
  91. *
  92. * \return Formatted error message with the list of errors with their
  93. * location in the parsed document. An empty string is returned if no error
  94. * occurred during parsing.
  95. * \deprecated Use getFormattedErrorMessages() instead (typo fix).
  96. */
  97. JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.")
  98. String getFormatedErrorMessages() const;
  99. /** \brief Returns a user friendly string that list errors in the parsed
  100. * document.
  101. *
  102. * \return Formatted error message with the list of errors with their
  103. * location in the parsed document. An empty string is returned if no error
  104. * occurred during parsing.
  105. */
  106. String getFormattedErrorMessages() const;
  107. /** \brief Returns a vector of structured errors encountered while parsing.
  108. *
  109. * \return A (possibly empty) vector of StructuredError objects. Currently
  110. * only one error can be returned, but the caller should tolerate multiple
  111. * errors. This can occur if the parser recovers from a non-fatal parse
  112. * error and then encounters additional errors.
  113. */
  114. std::vector<StructuredError> getStructuredErrors() const;
  115. /** \brief Add a semantic error message.
  116. *
  117. * \param value JSON Value location associated with the error
  118. * \param message The error message.
  119. * \return \c true if the error was successfully added, \c false if the Value
  120. * offset exceeds the document size.
  121. */
  122. bool pushError(const Value& value, const String& message);
  123. /** \brief Add a semantic error message with extra context.
  124. *
  125. * \param value JSON Value location associated with the error
  126. * \param message The error message.
  127. * \param extra Additional JSON Value location to contextualize the error
  128. * \return \c true if the error was successfully added, \c false if either
  129. * Value offset exceeds the document size.
  130. */
  131. bool pushError(const Value& value, const String& message, const Value& extra);
  132. /** \brief Return whether there are any errors.
  133. *
  134. * \return \c true if there are no errors to report \c false if errors have
  135. * occurred.
  136. */
  137. bool good() const;
  138. private:
  139. enum TokenType {
  140. tokenEndOfStream = 0,
  141. tokenObjectBegin,
  142. tokenObjectEnd,
  143. tokenArrayBegin,
  144. tokenArrayEnd,
  145. tokenString,
  146. tokenNumber,
  147. tokenTrue,
  148. tokenFalse,
  149. tokenNull,
  150. tokenArraySeparator,
  151. tokenMemberSeparator,
  152. tokenComment,
  153. tokenError
  154. };
  155. class Token {
  156. public:
  157. TokenType type_;
  158. Location start_;
  159. Location end_;
  160. };
  161. class ErrorInfo {
  162. public:
  163. Token token_;
  164. String message_;
  165. Location extra_;
  166. };
  167. using Errors = std::deque<ErrorInfo>;
  168. bool readToken(Token& token);
  169. void skipSpaces();
  170. bool match(const Char* pattern, int patternLength);
  171. bool readComment();
  172. bool readCStyleComment();
  173. bool readCppStyleComment();
  174. bool readString();
  175. void readNumber();
  176. bool readValue();
  177. bool readObject(Token& token);
  178. bool readArray(Token& token);
  179. bool decodeNumber(Token& token);
  180. bool decodeNumber(Token& token, Value& decoded);
  181. bool decodeString(Token& token);
  182. bool decodeString(Token& token, String& decoded);
  183. bool decodeDouble(Token& token);
  184. bool decodeDouble(Token& token, Value& decoded);
  185. bool decodeUnicodeCodePoint(Token& token, Location& current, Location end,
  186. unsigned int& unicode);
  187. bool decodeUnicodeEscapeSequence(Token& token, Location& current,
  188. Location end, unsigned int& unicode);
  189. bool addError(const String& message, Token& token, Location extra = nullptr);
  190. bool recoverFromError(TokenType skipUntilToken);
  191. bool addErrorAndRecover(const String& message, Token& token,
  192. TokenType skipUntilToken);
  193. void skipUntilSpace();
  194. Value& currentValue();
  195. Char getNextChar();
  196. void getLocationLineAndColumn(Location location, int& line,
  197. int& column) const;
  198. String getLocationLineAndColumn(Location location) const;
  199. void addComment(Location begin, Location end, CommentPlacement placement);
  200. void skipCommentTokens(Token& token);
  201. static bool containsNewLine(Location begin, Location end);
  202. static String normalizeEOL(Location begin, Location end);
  203. using Nodes = std::stack<Value*>;
  204. Nodes nodes_;
  205. Errors errors_;
  206. String document_;
  207. Location begin_{};
  208. Location end_{};
  209. Location current_{};
  210. Location lastValueEnd_{};
  211. Value* lastValue_{};
  212. String commentsBefore_;
  213. Features features_;
  214. bool collectComments_{};
  215. }; // Reader
  216. /** Interface for reading JSON from a char array.
  217. */
  218. class JSON_API CharReader {
  219. public:
  220. virtual ~CharReader() = default;
  221. /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a>
  222. * document. The document must be a UTF-8 encoded string containing the
  223. * document to read.
  224. *
  225. * \param beginDoc Pointer on the beginning of the UTF-8 encoded string
  226. * of the document to read.
  227. * \param endDoc Pointer on the end of the UTF-8 encoded string of the
  228. * document to read. Must be >= beginDoc.
  229. * \param[out] root Contains the root value of the document if it was
  230. * successfully parsed.
  231. * \param[out] errs Formatted error messages (if not NULL) a user
  232. * friendly string that lists errors in the parsed
  233. * document.
  234. * \return \c true if the document was successfully parsed, \c false if an
  235. * error occurred.
  236. */
  237. virtual bool parse(char const* beginDoc, char const* endDoc, Value* root,
  238. String* errs) = 0;
  239. class JSON_API Factory {
  240. public:
  241. virtual ~Factory() = default;
  242. /** \brief Allocate a CharReader via operator new().
  243. * \throw std::exception if something goes wrong (e.g. invalid settings)
  244. */
  245. virtual CharReader* newCharReader() const = 0;
  246. }; // Factory
  247. }; // CharReader
  248. /** \brief Build a CharReader implementation.
  249. *
  250. * Usage:
  251. * \code
  252. * using namespace Json;
  253. * CharReaderBuilder builder;
  254. * builder["collectComments"] = false;
  255. * Value value;
  256. * String errs;
  257. * bool ok = parseFromStream(builder, std::cin, &value, &errs);
  258. * \endcode
  259. */
  260. class JSON_API CharReaderBuilder : public CharReader::Factory {
  261. public:
  262. // Note: We use a Json::Value so that we can add data-members to this class
  263. // without a major version bump.
  264. /** Configuration of this builder.
  265. * These are case-sensitive.
  266. * Available settings (case-sensitive):
  267. * - `"collectComments": false or true`
  268. * - true to collect comment and allow writing them back during
  269. * serialization, false to discard comments. This parameter is ignored
  270. * if allowComments is false.
  271. * - `"allowComments": false or true`
  272. * - true if comments are allowed.
  273. * - `"allowTrailingCommas": false or true`
  274. * - true if trailing commas in objects and arrays are allowed.
  275. * - `"strictRoot": false or true`
  276. * - true if root must be either an array or an object value
  277. * - `"allowDroppedNullPlaceholders": false or true`
  278. * - true if dropped null placeholders are allowed. (See
  279. * StreamWriterBuilder.)
  280. * - `"allowNumericKeys": false or true`
  281. * - true if numeric object keys are allowed.
  282. * - `"allowSingleQuotes": false or true`
  283. * - true if '' are allowed for strings (both keys and values)
  284. * - `"stackLimit": integer`
  285. * - Exceeding stackLimit (recursive depth of `readValue()`) will cause an
  286. * exception.
  287. * - This is a security issue (seg-faults caused by deeply nested JSON), so
  288. * the default is low.
  289. * - `"failIfExtra": false or true`
  290. * - If true, `parse()` returns false when extra non-whitespace trails the
  291. * JSON value in the input string.
  292. * - `"rejectDupKeys": false or true`
  293. * - If true, `parse()` returns false when a key is duplicated within an
  294. * object.
  295. * - `"allowSpecialFloats": false or true`
  296. * - If true, special float values (NaNs and infinities) are allowed and
  297. * their values are lossfree restorable.
  298. * - `"skipBom": false or true`
  299. * - If true, if the input starts with the Unicode byte order mark (BOM),
  300. * it is skipped.
  301. *
  302. * You can examine 'settings_` yourself to see the defaults. You can also
  303. * write and read them just like any JSON Value.
  304. * \sa setDefaults()
  305. */
  306. Json::Value settings_;
  307. CharReaderBuilder();
  308. ~CharReaderBuilder() override;
  309. CharReader* newCharReader() const override;
  310. /** \return true if 'settings' are legal and consistent;
  311. * otherwise, indicate bad settings via 'invalid'.
  312. */
  313. bool validate(Json::Value* invalid) const;
  314. /** A simple way to update a specific setting.
  315. */
  316. Value& operator[](const String& key);
  317. /** Called by ctor, but you can use this to reset settings_.
  318. * \pre 'settings' != NULL (but Json::null is fine)
  319. * \remark Defaults:
  320. * \snippet src/lib_json/json_reader.cpp CharReaderBuilderDefaults
  321. */
  322. static void setDefaults(Json::Value* settings);
  323. /** Same as old Features::strictMode().
  324. * \pre 'settings' != NULL (but Json::null is fine)
  325. * \remark Defaults:
  326. * \snippet src/lib_json/json_reader.cpp CharReaderBuilderStrictMode
  327. */
  328. static void strictMode(Json::Value* settings);
  329. };
  330. /** Consume entire stream and use its begin/end.
  331. * Someday we might have a real StreamReader, but for now this
  332. * is convenient.
  333. */
  334. bool JSON_API parseFromStream(CharReader::Factory const&, IStream&, Value* root,
  335. String* errs);
  336. /** \brief Read from 'sin' into 'root'.
  337. *
  338. * Always keep comments from the input JSON.
  339. *
  340. * This can be used to read a file into a particular sub-object.
  341. * For example:
  342. * \code
  343. * Json::Value root;
  344. * cin >> root["dir"]["file"];
  345. * cout << root;
  346. * \endcode
  347. * Result:
  348. * \verbatim
  349. * {
  350. * "dir": {
  351. * "file": {
  352. * // The input stream JSON would be nested here.
  353. * }
  354. * }
  355. * }
  356. * \endverbatim
  357. * \throw std::exception on parse error.
  358. * \see Json::operator<<()
  359. */
  360. JSON_API IStream& operator>>(IStream&, Value&);
  361. } // namespace Json
  362. #pragma pack(pop)
  363. #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
  364. #pragma warning(pop)
  365. #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
  366. #endif // JSON_READER_H_INCLUDED