/vendor/TouchJSON/JSON/CJSONDeserializer.m

http://github.com/tgunr/passengerpane · Objective C · 161 lines · 108 code · 24 blank · 29 comment · 31 complexity · 0eed2f2e563cd98ea29dff6d633ef8bd MD5 · raw file

  1. //
  2. // CJSONDeserializer.m
  3. // TouchCode
  4. //
  5. // Created by Jonathan Wight on 12/15/2005.
  6. // Copyright 2005 toxicsoftware.com. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person
  9. // obtaining a copy of this software and associated documentation
  10. // files (the "Software"), to deal in the Software without
  11. // restriction, including without limitation the rights to use,
  12. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the
  14. // Software is furnished to do so, subject to the following
  15. // conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  22. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  24. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  25. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  27. // OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #import "CJSONDeserializer.h"
  30. #import "CJSONScanner.h"
  31. #import "CDataScanner.h"
  32. NSString *const kJSONDeserializerErrorDomain = @"CJSONDeserializerErrorDomain";
  33. @interface CJSONDeserializer ()
  34. @end
  35. @implementation CJSONDeserializer
  36. @synthesize scanner;
  37. @synthesize options;
  38. + (id)deserializer
  39. {
  40. return([[[self alloc] init] autorelease]);
  41. }
  42. - (id)init
  43. {
  44. if ((self = [super init]) != NULL)
  45. {
  46. }
  47. return(self);
  48. }
  49. - (void)dealloc
  50. {
  51. [scanner release];
  52. scanner = NULL;
  53. //
  54. [super dealloc];
  55. }
  56. #pragma mark -
  57. - (CJSONScanner *)scanner
  58. {
  59. if (scanner == NULL)
  60. {
  61. scanner = [[CJSONScanner alloc] init];
  62. }
  63. return(scanner);
  64. }
  65. - (id)nullObject
  66. {
  67. return(self.scanner.nullObject);
  68. }
  69. - (void)setNullObject:(id)inNullObject
  70. {
  71. self.scanner.nullObject = inNullObject;
  72. }
  73. #pragma mark -
  74. - (NSStringEncoding)allowedEncoding
  75. {
  76. return(self.scanner.allowedEncoding);
  77. }
  78. - (void)setAllowedEncoding:(NSStringEncoding)inAllowedEncoding
  79. {
  80. self.scanner.allowedEncoding = inAllowedEncoding;
  81. }
  82. #pragma mark -
  83. - (id)deserialize:(NSData *)inData error:(NSError **)outError
  84. {
  85. if (inData == NULL || [inData length] == 0)
  86. {
  87. if (outError)
  88. *outError = [NSError errorWithDomain:kJSONDeserializerErrorDomain code:kJSONScannerErrorCode_NothingToScan userInfo:NULL];
  89. return(NULL);
  90. }
  91. if ([self.scanner setData:inData error:outError] == NO)
  92. {
  93. return(NULL);
  94. }
  95. id theObject = NULL;
  96. if ([self.scanner scanJSONObject:&theObject error:outError] == YES)
  97. return(theObject);
  98. else
  99. return(NULL);
  100. }
  101. - (id)deserializeAsDictionary:(NSData *)inData error:(NSError **)outError
  102. {
  103. if (inData == NULL || [inData length] == 0)
  104. {
  105. if (outError)
  106. *outError = [NSError errorWithDomain:kJSONDeserializerErrorDomain code:kJSONScannerErrorCode_NothingToScan userInfo:NULL];
  107. return(NULL);
  108. }
  109. if ([self.scanner setData:inData error:outError] == NO)
  110. {
  111. return(NULL);
  112. }
  113. NSDictionary *theDictionary = NULL;
  114. if ([self.scanner scanJSONDictionary:&theDictionary error:outError] == YES)
  115. return(theDictionary);
  116. else
  117. return(NULL);
  118. }
  119. - (id)deserializeAsArray:(NSData *)inData error:(NSError **)outError
  120. {
  121. if (inData == NULL || [inData length] == 0)
  122. {
  123. if (outError)
  124. *outError = [NSError errorWithDomain:kJSONDeserializerErrorDomain code:kJSONScannerErrorCode_NothingToScan userInfo:NULL];
  125. return(NULL);
  126. }
  127. if ([self.scanner setData:inData error:outError] == NO)
  128. {
  129. return(NULL);
  130. }
  131. NSArray *theArray = NULL;
  132. if ([self.scanner scanJSONArray:&theArray error:outError] == YES)
  133. return(theArray);
  134. else
  135. return(NULL);
  136. }
  137. @end