reader.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. void skipSpaces();
  172. bool match(const Char* pattern, int patternLength);
  173. bool readComment();
  174. bool readCStyleComment();
  175. bool readCppStyleComment();
  176. bool readString();
  177. void readNumber();
  178. bool readValue();
  179. bool readObject(Token& token);
  180. bool readArray(Token& token);
  181. bool decodeNumber(Token& token);
  182. bool decodeNumber(Token& token, Value& decoded);
  183. bool decodeString(Token& token);
  184. bool decodeString(Token& token, String& decoded);
  185. bool decodeDouble(Token& token);
  186. bool decodeDouble(Token& token, Value& decoded);
  187. bool decodeUnicodeCodePoint(Token& token, Location& current, Location end,
  188. unsigned int& unicode);
  189. bool decodeUnicodeEscapeSequence(Token& token, Location& current,
  190. Location end, unsigned int& unicode);
  191. bool addError(const String& message, Token& token, Location extra = nullptr);
  192. bool recoverFromError(TokenType skipUntilToken);
  193. bool addErrorAndRecover(const String& message, Token& token,
  194. TokenType skipUntilToken);
  195. void skipUntilSpace();
  196. Value& currentValue();
  197. Char getNextChar();
  198. void getLocationLineAndColumn(Location location, int& line,
  199. int& column) const;
  200. String getLocationLineAndColumn(Location location) const;
  201. void addComment(Location begin, Location end, CommentPlacement placement);
  202. void skipCommentTokens(Token& token);
  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. virtual ~CharReader() = default;
  223. /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a>
  224. * document. The document must be a UTF-8 encoded string containing the
  225. * document to read.
  226. *
  227. * \param beginDoc Pointer on the beginning of the UTF-8 encoded string
  228. * of the document to read.
  229. * \param endDoc Pointer on the end of the UTF-8 encoded string of the
  230. * document to read. Must be >= beginDoc.
  231. * \param[out] root Contains the root value of the document if it was
  232. * successfully parsed.
  233. * \param[out] errs Formatted error messages (if not NULL) a user
  234. * friendly string that lists errors in the parsed
  235. * document.
  236. * \return \c true if the document was successfully parsed, \c false if an
  237. * error occurred.
  238. */
  239. virtual bool parse(char const* beginDoc, char const* endDoc, Value* root,
  240. String* errs) = 0;
  241. class JSON_API Factory {
  242. public:
  243. virtual ~Factory() = default;
  244. /** \brief Allocate a CharReader via operator new().
  245. * \throw std::exception if something goes wrong (e.g. invalid settings)
  246. */
  247. virtual CharReader* newCharReader() const = 0;
  248. }; // Factory
  249. }; // CharReader
  250. /** \brief Build a CharReader implementation.
  251. *
  252. * Usage:
  253. * \code
  254. * using namespace Json;
  255. * CharReaderBuilder builder;
  256. * builder["collectComments"] = false;
  257. * Value value;
  258. * String errs;
  259. * bool ok = parseFromStream(builder, std::cin, &value, &errs);
  260. * \endcode
  261. */
  262. class JSON_API CharReaderBuilder : public CharReader::Factory {
  263. public:
  264. // Note: We use a Json::Value so that we can add data-members to this class
  265. // without a major version bump.
  266. /** Configuration of this builder.
  267. * These are case-sensitive.
  268. * Available settings (case-sensitive):
  269. * - `"collectComments": false or true`
  270. * - true to collect comment and allow writing them back during
  271. * serialization, false to discard comments. This parameter is ignored
  272. * if allowComments is false.
  273. * - `"allowComments": false or true`
  274. * - true if comments are allowed.
  275. * - `"allowTrailingCommas": false or true`
  276. * - true if trailing commas in objects and arrays are allowed.
  277. * - `"strictRoot": false or true`
  278. * - true if root must be either an array or an object value
  279. * - `"allowDroppedNullPlaceholders": false or true`
  280. * - true if dropped null placeholders are allowed. (See
  281. * StreamWriterBuilder.)
  282. * - `"allowNumericKeys": false or true`
  283. * - true if numeric object keys are allowed.
  284. * - `"allowSingleQuotes": false or true`
  285. * - true if '' are allowed for strings (both keys and values)
  286. * - `"stackLimit": integer`
  287. * - Exceeding stackLimit (recursive depth of `readValue()`) will cause an
  288. * exception.
  289. * - This is a security issue (seg-faults caused by deeply nested JSON), so
  290. * the default is low.
  291. * - `"failIfExtra": false or true`
  292. * - If true, `parse()` returns false when extra non-whitespace trails the
  293. * JSON value in the input string.
  294. * - `"rejectDupKeys": false or true`
  295. * - If true, `parse()` returns false when a key is duplicated within an
  296. * object.
  297. * - `"allowSpecialFloats": false or true`
  298. * - If true, special float values (NaNs and infinities) are allowed and
  299. * their values are lossfree restorable.
  300. * - `"skipBom": false or true`
  301. * - If true, if the input starts with the Unicode byte order mark (BOM),
  302. * it is skipped.
  303. *
  304. * You can examine 'settings_` yourself to see the defaults. You can also
  305. * write and read them just like any JSON Value.
  306. * \sa setDefaults()
  307. */
  308. Json::Value settings_;
  309. CharReaderBuilder();
  310. ~CharReaderBuilder() override;
  311. CharReader* newCharReader() const override;
  312. /** \return true if 'settings' are legal and consistent;
  313. * otherwise, indicate bad settings via 'invalid'.
  314. */
  315. bool validate(Json::Value* invalid) const;
  316. /** A simple way to update a specific setting.
  317. */
  318. Value& operator[](const String& key);
  319. /** Called by ctor, but you can use this to reset settings_.
  320. * \pre 'settings' != NULL (but Json::null is fine)
  321. * \remark Defaults:
  322. * snippet src/lib_json/json_reader.cpp CharReaderBuilderDefaults
  323. */
  324. static void setDefaults(Json::Value* settings);
  325. /** Same as old Features::strictMode().
  326. * \pre 'settings' != NULL (but Json::null is fine)
  327. * \remark Defaults:
  328. * snippet src/lib_json/json_reader.cpp CharReaderBuilderStrictMode
  329. */
  330. static void strictMode(Json::Value* settings);
  331. };
  332. /** Consume entire stream and use its begin/end.
  333. * Someday we might have a real StreamReader, but for now this
  334. * is convenient.
  335. */
  336. bool JSON_API parseFromStream(CharReader::Factory const&, IStream&, Value* root,
  337. String* errs);
  338. /** \brief Read from 'sin' into 'root'.
  339. *
  340. * Always keep comments from the input JSON.
  341. *
  342. * This can be used to read a file into a particular sub-object.
  343. * For example:
  344. * \code
  345. * Json::Value root;
  346. * cin >> root["dir"]["file"];
  347. * cout << root;
  348. * \endcode
  349. * Result:
  350. * \verbatim
  351. * {
  352. * "dir": {
  353. * "file": {
  354. * // The input stream JSON would be nested here.
  355. * }
  356. * }
  357. * }
  358. * \endverbatim
  359. * \throw std::exception on parse error.
  360. * \see Json::operator<<()
  361. */
  362. JSON_API IStream& operator>>(IStream&, Value&);
  363. } // namespace Json
  364. #if !defined(__SUNPRO_CC)
  365. #pragma pack(pop)
  366. #endif
  367. #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
  368. #pragma warning(pop)
  369. #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
  370. #endif // JSON_READER_H_INCLUDED