Playlist Generator
1.0
|
00001 /* 00002 Copyright (C) 2009,2010 Stig Brautaset. All rights reserved. 00003 00004 Redistribution and use in source and binary forms, with or without 00005 modification, are permitted provided that the following conditions are met: 00006 00007 * Redistributions of source code must retain the above copyright notice, this 00008 list of conditions and the following disclaimer. 00009 00010 * Redistributions in binary form must reproduce the above copyright notice, 00011 this list of conditions and the following disclaimer in the documentation 00012 and/or other materials provided with the distribution. 00013 00014 * Neither the name of the author nor the names of its contributors may be used 00015 to endorse or promote products derived from this software without specific 00016 prior written permission. 00017 00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00019 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00020 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00021 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 00022 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00023 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00024 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00025 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00026 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00027 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 #import "SBJsonParser.h" 00031 #import "SBJsonStreamParser.h" 00032 #import "SBJsonStreamParserAdapter.h" 00033 #import "SBJsonStreamParserAccumulator.h" 00034 00035 @implementation SBJsonParser 00036 00037 @synthesize maxDepth; 00038 @synthesize error; 00039 00040 - (id)init { 00041 self = [super init]; 00042 if (self) 00043 self.maxDepth = 32u; 00044 return self; 00045 } 00046 00047 00048 #pragma mark Methods 00049 00050 - (id)objectWithData:(NSData *)data { 00051 00052 if (!data) { 00053 self.error = @"Input was 'nil'"; 00054 return nil; 00055 } 00056 00057 SBJsonStreamParserAccumulator *accumulator = [[SBJsonStreamParserAccumulator alloc] init]; 00058 00059 SBJsonStreamParserAdapter *adapter = [[SBJsonStreamParserAdapter alloc] init]; 00060 adapter.delegate = accumulator; 00061 00062 SBJsonStreamParser *parser = [[SBJsonStreamParser alloc] init]; 00063 parser.maxDepth = self.maxDepth; 00064 parser.delegate = adapter; 00065 00066 switch ([parser parse:data]) { 00067 case SBJsonStreamParserComplete: 00068 return accumulator.value; 00069 break; 00070 00071 case SBJsonStreamParserWaitingForData: 00072 self.error = @"Unexpected end of input"; 00073 break; 00074 00075 case SBJsonStreamParserError: 00076 self.error = parser.error; 00077 break; 00078 } 00079 00080 return nil; 00081 } 00082 00083 - (id)objectWithString:(NSString *)repr { 00084 return [self objectWithData:[repr dataUsingEncoding:NSUTF8StringEncoding]]; 00085 } 00086 00087 - (id)objectWithString:(NSString*)repr error:(NSError**)error_ { 00088 id tmp = [self objectWithString:repr]; 00089 if (tmp) 00090 return tmp; 00091 00092 if (error_) { 00093 NSDictionary *ui = [NSDictionary dictionaryWithObjectsAndKeys:error, NSLocalizedDescriptionKey, nil]; 00094 *error_ = [NSError errorWithDomain:@"org.brautaset.SBJsonParser.ErrorDomain" code:0 userInfo:ui]; 00095 } 00096 00097 return nil; 00098 } 00099 00100 @end