SBJSON.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. Copyright (C) 2007-2009 Stig Brautaset. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice, this
  6. list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of the author nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without specific
  12. prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  17. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  21. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  22. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #import "SBJSON.h"
  25. @implementation SBJSON
  26. - (id)init {
  27. self = [super init];
  28. if (self) {
  29. jsonWriter = [SBJsonWriter new];
  30. jsonParser = [SBJsonParser new];
  31. [self setMaxDepth:512];
  32. }
  33. return self;
  34. }
  35. - (void)dealloc {
  36. [jsonWriter release];
  37. [jsonParser release];
  38. [super dealloc];
  39. }
  40. #pragma mark Writer
  41. - (NSString *)stringWithObject:(id)obj {
  42. NSString *repr = [jsonWriter stringWithObject:obj];
  43. if (repr)
  44. return repr;
  45. [errorTrace release];
  46. errorTrace = [[jsonWriter errorTrace] mutableCopy];
  47. return nil;
  48. }
  49. /**
  50. Returns a string containing JSON representation of the passed in value, or nil on error.
  51. If nil is returned and @p error is not NULL, @p *error can be interrogated to find the cause of the error.
  52. @param value any instance that can be represented as a JSON fragment
  53. @param allowScalar wether to return json fragments for scalar objects
  54. @param error used to return an error by reference (pass NULL if this is not desired)
  55. @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed.
  56. */
  57. - (NSString*)stringWithObject:(id)value allowScalar:(BOOL)allowScalar error:(NSError**)error {
  58. NSString *json = allowScalar ? [jsonWriter stringWithFragment:value] : [jsonWriter stringWithObject:value];
  59. if (json)
  60. return json;
  61. [errorTrace release];
  62. errorTrace = [[jsonWriter errorTrace] mutableCopy];
  63. if (error)
  64. *error = [errorTrace lastObject];
  65. return nil;
  66. }
  67. /**
  68. Returns a string containing JSON representation of the passed in value, or nil on error.
  69. If nil is returned and @p error is not NULL, @p error can be interrogated to find the cause of the error.
  70. @param value any instance that can be represented as a JSON fragment
  71. @param error used to return an error by reference (pass NULL if this is not desired)
  72. @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed.
  73. */
  74. - (NSString*)stringWithFragment:(id)value error:(NSError**)error {
  75. return [self stringWithObject:value
  76. allowScalar:YES
  77. error:error];
  78. }
  79. /**
  80. Returns a string containing JSON representation of the passed in value, or nil on error.
  81. If nil is returned and @p error is not NULL, @p error can be interrogated to find the cause of the error.
  82. @param value a NSDictionary or NSArray instance
  83. @param error used to return an error by reference (pass NULL if this is not desired)
  84. */
  85. - (NSString*)stringWithObject:(id)value error:(NSError**)error {
  86. return [self stringWithObject:value
  87. allowScalar:NO
  88. error:error];
  89. }
  90. #pragma mark Parsing
  91. - (id)objectWithString:(NSString *)repr {
  92. id obj = [jsonParser objectWithString:repr];
  93. if (obj)
  94. return obj;
  95. [errorTrace release];
  96. errorTrace = [[jsonParser errorTrace] mutableCopy];
  97. return nil;
  98. }
  99. /**
  100. Returns the object represented by the passed-in string or nil on error. The returned object can be
  101. a string, number, boolean, null, array or dictionary.
  102. @param value the json string to parse
  103. @param allowScalar whether to return objects for JSON fragments
  104. @param error used to return an error by reference (pass NULL if this is not desired)
  105. @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed.
  106. */
  107. - (id)objectWithString:(id)value allowScalar:(BOOL)allowScalar error:(NSError**)error {
  108. id obj = allowScalar ? [jsonParser fragmentWithString:value] : [jsonParser objectWithString:value];
  109. if (obj)
  110. return obj;
  111. [errorTrace release];
  112. errorTrace = [[jsonParser errorTrace] mutableCopy];
  113. if (error)
  114. *error = [errorTrace lastObject];
  115. return nil;
  116. }
  117. /**
  118. Returns the object represented by the passed-in string or nil on error. The returned object can be
  119. a string, number, boolean, null, array or dictionary.
  120. @param repr the json string to parse
  121. @param error used to return an error by reference (pass NULL if this is not desired)
  122. @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed.
  123. */
  124. - (id)fragmentWithString:(NSString*)repr error:(NSError**)error {
  125. return [self objectWithString:repr
  126. allowScalar:YES
  127. error:error];
  128. }
  129. /**
  130. Returns the object represented by the passed-in string or nil on error. The returned object
  131. will be either a dictionary or an array.
  132. @param repr the json string to parse
  133. @param error used to return an error by reference (pass NULL if this is not desired)
  134. */
  135. - (id)objectWithString:(NSString*)repr error:(NSError**)error {
  136. return [self objectWithString:repr
  137. allowScalar:NO
  138. error:error];
  139. }
  140. #pragma mark Properties - parsing
  141. - (NSUInteger)maxDepth {
  142. return jsonParser.maxDepth;
  143. }
  144. - (void)setMaxDepth:(NSUInteger)d {
  145. jsonWriter.maxDepth = jsonParser.maxDepth = d;
  146. }
  147. #pragma mark Properties - writing
  148. - (BOOL)humanReadable {
  149. return jsonWriter.humanReadable;
  150. }
  151. - (void)setHumanReadable:(BOOL)x {
  152. jsonWriter.humanReadable = x;
  153. }
  154. - (BOOL)sortKeys {
  155. return jsonWriter.sortKeys;
  156. }
  157. - (void)setSortKeys:(BOOL)x {
  158. jsonWriter.sortKeys = x;
  159. }
  160. @end