reader.h 15 KB

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