Playlist Generator
1.0
|
00001 /* 00002 Copyright (C) 2009 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 "SBJsonWriter.h" 00031 #import "SBJsonStreamWriter.h" 00032 #import "SBJsonStreamWriterAccumulator.h" 00033 00034 00035 @interface SBJsonWriter () 00036 @property (copy) NSString *error; 00037 @end 00038 00039 @implementation SBJsonWriter 00040 00041 @synthesize sortKeys; 00042 @synthesize humanReadable; 00043 00044 @synthesize error; 00045 @synthesize maxDepth; 00046 00047 - (id)init { 00048 self = [super init]; 00049 if (self) { 00050 self.maxDepth = 32u; 00051 } 00052 return self; 00053 } 00054 00055 00056 - (NSString*)stringWithObject:(id)value { 00057 NSData *data = [self dataWithObject:value]; 00058 if (data) 00059 return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 00060 return nil; 00061 } 00062 00063 - (NSString*)stringWithObject:(id)value error:(NSError**)error_ { 00064 NSString *tmp = [self stringWithObject:value]; 00065 if (tmp) 00066 return tmp; 00067 00068 if (error_) { 00069 NSDictionary *ui = [NSDictionary dictionaryWithObjectsAndKeys:error, NSLocalizedDescriptionKey, nil]; 00070 *error_ = [NSError errorWithDomain:@"org.brautaset.SBJsonWriter.ErrorDomain" code:0 userInfo:ui]; 00071 } 00072 00073 return nil; 00074 } 00075 00076 - (NSData*)dataWithObject:(id)object { 00077 self.error = nil; 00078 00079 SBJsonStreamWriterAccumulator *accumulator = [[SBJsonStreamWriterAccumulator alloc] init]; 00080 00081 SBJsonStreamWriter *streamWriter = [[SBJsonStreamWriter alloc] init]; 00082 streamWriter.sortKeys = self.sortKeys; 00083 streamWriter.maxDepth = self.maxDepth; 00084 streamWriter.humanReadable = self.humanReadable; 00085 streamWriter.delegate = accumulator; 00086 00087 BOOL ok = NO; 00088 if ([object isKindOfClass:[NSDictionary class]]) 00089 ok = [streamWriter writeObject:object]; 00090 00091 else if ([object isKindOfClass:[NSArray class]]) 00092 ok = [streamWriter writeArray:object]; 00093 00094 else if ([object respondsToSelector:@selector(proxyForJson)]) 00095 return [self dataWithObject:[object proxyForJson]]; 00096 else { 00097 self.error = @"Not valid type for JSON"; 00098 return nil; 00099 } 00100 00101 if (ok) 00102 return accumulator.data; 00103 00104 self.error = streamWriter.error; 00105 return nil; 00106 } 00107 00108 00109 @end