SBJsonStreamParser.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Copyright (c) 2010, Stig Brautaset.
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. Neither the name of the the author nor the names of its contributors
  13. may be used to endorse or promote products derived from this software
  14. without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  16. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  17. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  18. PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #import <Foundation/Foundation.h>
  28. @class SBJsonTokeniser;
  29. @class SBJsonStreamParser;
  30. @class SBJsonStreamParserState;
  31. typedef enum {
  32. SBJsonStreamParserComplete,
  33. SBJsonStreamParserWaitingForData,
  34. SBJsonStreamParserError,
  35. } SBJsonStreamParserStatus;
  36. /**
  37. @brief Delegate for interacting directly with the stream parser
  38. You will most likely find it much more convenient to implement the
  39. SBJsonStreamParserAdapterDelegate protocol instead.
  40. */
  41. @protocol SBJsonStreamParserDelegate
  42. /// Called when object start is found
  43. - (void)parserFoundObjectStart:(SBJsonStreamParser*)parser;
  44. /// Called when object key is found
  45. - (void)parser:(SBJsonStreamParser*)parser foundObjectKey:(NSString*)key;
  46. /// Called when object end is found
  47. - (void)parserFoundObjectEnd:(SBJsonStreamParser*)parser;
  48. /// Called when array start is found
  49. - (void)parserFoundArrayStart:(SBJsonStreamParser*)parser;
  50. /// Called when array end is found
  51. - (void)parserFoundArrayEnd:(SBJsonStreamParser*)parser;
  52. /// Called when a boolean value is found
  53. - (void)parser:(SBJsonStreamParser*)parser foundBoolean:(BOOL)x;
  54. /// Called when a null value is found
  55. - (void)parserFoundNull:(SBJsonStreamParser*)parser;
  56. /// Called when a number is found
  57. - (void)parser:(SBJsonStreamParser*)parser foundNumber:(NSNumber*)num;
  58. /// Called when a string is found
  59. - (void)parser:(SBJsonStreamParser*)parser foundString:(NSString*)string;
  60. @end
  61. /**
  62. @brief Parse a stream of JSON data.
  63. Using this class directly you can reduce the apparent latency for each
  64. download/parse cycle of documents over a slow connection. You can start
  65. parsing *and return chunks of the parsed document* before the entire
  66. document is downloaded.
  67. Using this class is also useful to parse huge documents on disk
  68. bit by bit so you don't have to keep them all in memory.
  69. @see SBJsonStreamParserAdapter for more information.
  70. @see @ref objc2json
  71. */
  72. @interface SBJsonStreamParser : NSObject {
  73. @private
  74. SBJsonTokeniser *tokeniser;
  75. }
  76. @property (nonatomic, unsafe_unretained) SBJsonStreamParserState *state; // Private
  77. @property (nonatomic, readonly, strong) NSMutableArray *stateStack; // Private
  78. /**
  79. @brief Expect multiple documents separated by whitespace
  80. Normally the @p -parse: method returns SBJsonStreamParserComplete when it's found a complete JSON document.
  81. Attempting to parse any more data at that point is considered an error. ("Garbage after JSON".)
  82. If you set this property to true the parser will never return SBJsonStreamParserComplete. Rather,
  83. once an object is completed it will expect another object to immediately follow, separated
  84. only by (optional) whitespace.
  85. @see The TweetStream app in the Examples
  86. */
  87. @property BOOL supportMultipleDocuments;
  88. /**
  89. @brief Delegate to receive messages
  90. The object set here receives a series of messages as the parser breaks down the JSON stream
  91. into valid tokens.
  92. @note
  93. Usually this should be an instance of SBJsonStreamParserAdapter, but you can
  94. substitute your own implementation of the SBJsonStreamParserDelegate protocol if you need to.
  95. */
  96. @property (unsafe_unretained) id<SBJsonStreamParserDelegate> delegate;
  97. /**
  98. @brief The max parse depth
  99. If the input is nested deeper than this the parser will halt parsing and return an error.
  100. Defaults to 32.
  101. */
  102. @property NSUInteger maxDepth;
  103. /// Holds the error after SBJsonStreamParserError was returned
  104. @property (copy) NSString *error;
  105. /**
  106. @brief Parse some JSON
  107. The JSON is assumed to be UTF8 encoded. This can be a full JSON document, or a part of one.
  108. @param data An NSData object containing the next chunk of JSON
  109. @return
  110. @li SBJsonStreamParserComplete if a full document was found
  111. @li SBJsonStreamParserWaitingForData if a partial document was found and more data is required to complete it
  112. @li SBJsonStreamParserError if an error occured. (See the error property for details in this case.)
  113. */
  114. - (SBJsonStreamParserStatus)parse:(NSData*)data;
  115. @end