SBJsonStreamWriter.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. /// Enable JSON writing for non-native objects
  29. @interface NSObject (SBProxyForJson)
  30. /**
  31. @brief Allows generation of JSON for otherwise unsupported classes.
  32. If you have a custom class that you want to create a JSON representation
  33. for you can implement this method in your class. It should return a
  34. representation of your object defined in terms of objects that can be
  35. translated into JSON. For example, a Person object might implement it like this:
  36. @code
  37. - (id)proxyForJson {
  38. return [NSDictionary dictionaryWithObjectsAndKeys:
  39. name, @"name",
  40. phone, @"phone",
  41. email, @"email",
  42. nil];
  43. }
  44. @endcode
  45. */
  46. - (id)proxyForJson;
  47. @end
  48. @class SBJsonStreamWriter;
  49. @protocol SBJsonStreamWriterDelegate
  50. - (void)writer:(SBJsonStreamWriter*)writer appendBytes:(const void *)bytes length:(NSUInteger)length;
  51. @end
  52. @class SBJsonStreamWriterState;
  53. /**
  54. @brief The Stream Writer class.
  55. Accepts a stream of messages and writes JSON of these to its delegate object.
  56. This class provides a range of high-, mid- and low-level methods. You can mix
  57. and match calls to these. For example, you may want to call -writeArrayOpen
  58. to start an array and then repeatedly call -writeObject: with various objects
  59. before finishing off with a -writeArrayClose call.
  60. @see @ref json2objc
  61. */
  62. @interface SBJsonStreamWriter : NSObject {
  63. NSMutableDictionary *cache;
  64. }
  65. @property (nonatomic, unsafe_unretained) SBJsonStreamWriterState *state; // Internal
  66. @property (nonatomic, readonly, strong) NSMutableArray *stateStack; // Internal
  67. /**
  68. @brief delegate to receive JSON output
  69. Delegate that will receive messages with output.
  70. */
  71. @property (unsafe_unretained) id<SBJsonStreamWriterDelegate> delegate;
  72. /**
  73. @brief The maximum recursing depth.
  74. Defaults to 512. If the input is nested deeper than this the input will be deemed to be
  75. malicious and the parser returns nil, signalling an error. ("Nested too deep".) You can
  76. turn off this security feature by setting the maxDepth value to 0.
  77. */
  78. @property NSUInteger maxDepth;
  79. /**
  80. @brief Whether we are generating human-readable (multiline) JSON.
  81. Set whether or not to generate human-readable JSON. The default is NO, which produces
  82. JSON without any whitespace between tokens. If set to YES, generates human-readable
  83. JSON with linebreaks after each array value and dictionary key/value pair, indented two
  84. spaces per nesting level.
  85. */
  86. @property BOOL humanReadable;
  87. /**
  88. @brief Whether or not to sort the dictionary keys in the output.
  89. If this is set to YES, the dictionary keys in the JSON output will be in sorted order.
  90. (This is useful if you need to compare two structures, for example.) The default is NO.
  91. */
  92. @property BOOL sortKeys;
  93. /**
  94. @brief An optional comparator to be used if sortKeys is YES.
  95. If this is nil, sorting will be done via @selector(compare:).
  96. */
  97. @property (copy) NSComparator sortKeysComparator;
  98. /// Contains the error description after an error has occured.
  99. @property (copy) NSString *error;
  100. /**
  101. Write an NSDictionary to the JSON stream.
  102. @return YES if successful, or NO on failure
  103. */
  104. - (BOOL)writeObject:(NSDictionary*)dict;
  105. /**
  106. Write an NSArray to the JSON stream.
  107. @return YES if successful, or NO on failure
  108. */
  109. - (BOOL)writeArray:(NSArray *)array;
  110. /**
  111. Start writing an Object to the stream
  112. @return YES if successful, or NO on failure
  113. */
  114. - (BOOL)writeObjectOpen;
  115. /**
  116. Close the current object being written
  117. @return YES if successful, or NO on failure
  118. */
  119. - (BOOL)writeObjectClose;
  120. /** Start writing an Array to the stream
  121. @return YES if successful, or NO on failure
  122. */
  123. - (BOOL)writeArrayOpen;
  124. /** Close the current Array being written
  125. @return YES if successful, or NO on failure
  126. */
  127. - (BOOL)writeArrayClose;
  128. /** Write a null to the stream
  129. @return YES if successful, or NO on failure
  130. */
  131. - (BOOL)writeNull;
  132. /** Write a boolean to the stream
  133. @return YES if successful, or NO on failure
  134. */
  135. - (BOOL)writeBool:(BOOL)x;
  136. /** Write a Number to the stream
  137. @return YES if successful, or NO on failure
  138. */
  139. - (BOOL)writeNumber:(NSNumber*)n;
  140. /** Write a String to the stream
  141. @return YES if successful, or NO on failure
  142. */
  143. - (BOOL)writeString:(NSString*)s;
  144. @end
  145. @interface SBJsonStreamWriter (Private)
  146. - (BOOL)writeValue:(id)v;
  147. - (void)appendBytes:(const void *)bytes length:(NSUInteger)length;
  148. @end