SBJsonStreamParserAdapter.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #import "SBJsonStreamParser.h"
  29. typedef enum {
  30. SBJsonStreamParserAdapterNone,
  31. SBJsonStreamParserAdapterArray,
  32. SBJsonStreamParserAdapterObject,
  33. } SBJsonStreamParserAdapterType;
  34. /**
  35. @brief Delegate for getting objects & arrays from the stream parser adapter
  36. @see The TweetStream example project.
  37. */
  38. @protocol SBJsonStreamParserAdapterDelegate
  39. /**
  40. @brief Called if a JSON array is found
  41. This method is called if a JSON array is found.
  42. */
  43. - (void)parser:(SBJsonStreamParser*)parser foundArray:(NSArray*)array;
  44. /**
  45. @brief Called when a JSON object is found
  46. This method is called if a JSON object is found.
  47. */
  48. - (void)parser:(SBJsonStreamParser*)parser foundObject:(NSDictionary*)dict;
  49. @end
  50. /**
  51. @brief SBJsonStreamParserDelegate protocol adapter
  52. Rather than implementing the SBJsonStreamParserDelegate protocol yourself you will
  53. most likely find it much more convenient to use an instance of this class and
  54. implement the SBJsonStreamParserAdapterDelegate protocol instead.
  55. The default behaviour is that the delegate only receives one call from
  56. either the -parser:foundArray: or -parser:foundObject: method when the
  57. document is fully parsed. However, if your inputs contains multiple JSON
  58. documents and you set the parser's -supportMultipleDocuments property to YES
  59. you will get one call for each full method.
  60. @code
  61. SBJsonStreamParserAdapter *adapter = [[[SBJsonStreamParserAdapter alloc] init] autorelease];
  62. adapter.delegate = self;
  63. SBJsonStreamParser *parser = [[[SBJsonStreamParser alloc] init] autorelease];
  64. parser.delegate = adapter;
  65. parser.supportMultipleDocuments = YES;
  66. // Note that this input contains multiple top-level JSON documents
  67. NSData *json = [@"[]{}[]{}" dataWithEncoding:NSUTF8StringEncoding];
  68. [parser parse:data];
  69. @endcode
  70. In the above example @p self will have the following sequence of methods called on it:
  71. @li -parser:foundArray:
  72. @li -parser:foundObject:
  73. @li -parser:foundArray:
  74. @li -parser:foundObject:
  75. Often you won't have control over the input you're parsing, so can't make use of
  76. this feature. But, all is not lost: this class will let you get the same effect by
  77. allowing you to skip one or more of the outer enclosing objects. Thus, the next
  78. example results in the same sequence of -parser:foundArray: / -parser:foundObject:
  79. being called on your delegate.
  80. @code
  81. SBJsonStreamParserAdapter *adapter = [[[SBJsonStreamParserAdapter alloc] init] autorelease];
  82. adapter.delegate = self;
  83. adapter.levelsToSkip = 1;
  84. SBJsonStreamParser *parser = [[[SBJsonStreamParser alloc] init] autorelease];
  85. parser.delegate = adapter;
  86. // Note that this input contains A SINGLE top-level document
  87. NSData *json = [@"[[],{},[],{}]" dataWithEncoding:NSUTF8StringEncoding];
  88. [parser parse:data];
  89. @endcode
  90. */
  91. @interface SBJsonStreamParserAdapter : NSObject <SBJsonStreamParserDelegate> {
  92. @private
  93. NSUInteger depth;
  94. NSMutableArray *array;
  95. NSMutableDictionary *dict;
  96. NSMutableArray *keyStack;
  97. NSMutableArray *stack;
  98. SBJsonStreamParserAdapterType currentType;
  99. }
  100. /**
  101. @brief How many levels to skip
  102. This is useful for parsing huge JSON documents, or documents coming in over a very slow link.
  103. If you set this to N it will skip the outer N levels and call the -parser:foundArray:
  104. or -parser:foundObject: methods for each of the inner objects, as appropriate.
  105. @see The StreamParserIntegrationTest.m file for examples
  106. */
  107. @property NSUInteger levelsToSkip;
  108. /**
  109. @brief Your delegate object
  110. Set this to the object you want to receive the SBJsonStreamParserAdapterDelegate messages.
  111. */
  112. @property (unsafe_unretained) id<SBJsonStreamParserAdapterDelegate> delegate;
  113. @end