PageRenderTime 61ms CodeModel.GetById 18ms app.highlight 40ms RepoModel.GetById 1ms app.codeStats 1ms

/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
 30#import "CJSONDeserializer.h"
 31
 32#import "CJSONScanner.h"
 33#import "CDataScanner.h"
 34
 35NSString *const kJSONDeserializerErrorDomain  = @"CJSONDeserializerErrorDomain";
 36
 37@interface CJSONDeserializer ()
 38@end
 39
 40@implementation CJSONDeserializer
 41
 42@synthesize scanner;
 43@synthesize options;
 44
 45+ (id)deserializer
 46    {
 47    return([[[self alloc] init] autorelease]);
 48    }
 49
 50- (id)init
 51    {
 52    if ((self = [super init]) != NULL)
 53        {
 54        }
 55    return(self);
 56    }
 57
 58- (void)dealloc
 59    {
 60    [scanner release];
 61    scanner = NULL;
 62    //
 63    [super dealloc];
 64    }
 65
 66#pragma mark -
 67
 68- (CJSONScanner *)scanner
 69    {
 70    if (scanner == NULL)
 71        {
 72        scanner = [[CJSONScanner alloc] init];
 73        }
 74    return(scanner);
 75    }
 76
 77- (id)nullObject
 78    {
 79    return(self.scanner.nullObject);
 80    }
 81
 82- (void)setNullObject:(id)inNullObject
 83    {
 84    self.scanner.nullObject = inNullObject;
 85    }
 86
 87#pragma mark -
 88
 89- (NSStringEncoding)allowedEncoding
 90    {
 91    return(self.scanner.allowedEncoding);
 92    }
 93
 94- (void)setAllowedEncoding:(NSStringEncoding)inAllowedEncoding
 95    {
 96    self.scanner.allowedEncoding = inAllowedEncoding;
 97    }
 98
 99#pragma mark -
100
101- (id)deserialize:(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
108        return(NULL);
109        }
110    if ([self.scanner setData:inData error:outError] == NO)
111        {
112        return(NULL);
113        }
114    id theObject = NULL;
115    if ([self.scanner scanJSONObject:&theObject error:outError] == YES)
116        return(theObject);
117    else
118        return(NULL);
119    }
120
121- (id)deserializeAsDictionary:(NSData *)inData error:(NSError **)outError
122    {
123    if (inData == NULL || [inData length] == 0)
124        {
125        if (outError)
126            *outError = [NSError errorWithDomain:kJSONDeserializerErrorDomain code:kJSONScannerErrorCode_NothingToScan userInfo:NULL];
127
128        return(NULL);
129        }
130    if ([self.scanner setData:inData error:outError] == NO)
131        {
132        return(NULL);
133        }
134    NSDictionary *theDictionary = NULL;
135    if ([self.scanner scanJSONDictionary:&theDictionary error:outError] == YES)
136        return(theDictionary);
137    else
138        return(NULL);
139    }
140
141- (id)deserializeAsArray:(NSData *)inData error:(NSError **)outError
142    {
143    if (inData == NULL || [inData length] == 0)
144        {
145        if (outError)
146            *outError = [NSError errorWithDomain:kJSONDeserializerErrorDomain code:kJSONScannerErrorCode_NothingToScan userInfo:NULL];
147
148        return(NULL);
149        }
150    if ([self.scanner setData:inData error:outError] == NO)
151        {
152        return(NULL);
153        }
154    NSArray *theArray = NULL;
155    if ([self.scanner scanJSONArray:&theArray error:outError] == YES)
156        return(theArray);
157    else
158        return(NULL);
159    }
160
161@end