reader.h 13 KB

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