Playlist Generator  1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Defines
SBJsonStreamWriterState.m
Go to the documentation of this file.
00001 /*
00002  Copyright (c) 2010, Stig Brautaset.
00003  All rights reserved.
00004 
00005  Redistribution and use in source and binary forms, with or without
00006  modification, are permitted provided that the following conditions are
00007  met:
00008 
00009    Redistributions of source code must retain the above copyright
00010    notice, this list of conditions and the following disclaimer.
00011 
00012    Redistributions in binary form must reproduce the above copyright
00013    notice, this list of conditions and the following disclaimer in the
00014    documentation and/or other materials provided with the distribution.
00015 
00016    Neither the name of the the author nor the names of its contributors
00017    may be used to endorse or promote products derived from this software
00018    without specific prior written permission.
00019 
00020  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00021  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00022  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00024  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00026  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00027  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00028  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00030  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 #import "SBJsonStreamWriterState.h"
00034 #import "SBJsonStreamWriter.h"
00035 
00036 #define SINGLETON \
00037 + (id)sharedInstance { \
00038     static id state; \
00039     if (!state) state = [[self alloc] init]; \
00040     return state; \
00041 }
00042 
00043 
00044 @implementation SBJsonStreamWriterState
00045 + (id)sharedInstance { return nil; }
00046 - (BOOL)isInvalidState:(SBJsonStreamWriter*)writer { return NO; }
00047 - (void)appendSeparator:(SBJsonStreamWriter*)writer {}
00048 - (BOOL)expectingKey:(SBJsonStreamWriter*)writer { return NO; }
00049 - (void)transitionState:(SBJsonStreamWriter *)writer {}
00050 - (void)appendWhitespace:(SBJsonStreamWriter*)writer {
00051         [writer appendBytes:"\n" length:1];
00052         for (NSUInteger i = 0; i < writer.stateStack.count; i++)
00053             [writer appendBytes:"  " length:2];
00054 }
00055 @end
00056 
00057 @implementation SBJsonStreamWriterStateObjectStart
00058 
00059 SINGLETON
00060 
00061 - (void)transitionState:(SBJsonStreamWriter *)writer {
00062         writer.state = [SBJsonStreamWriterStateObjectValue sharedInstance];
00063 }
00064 - (BOOL)expectingKey:(SBJsonStreamWriter *)writer {
00065         writer.error = @"JSON object key must be string";
00066         return YES;
00067 }
00068 @end
00069 
00070 @implementation SBJsonStreamWriterStateObjectKey
00071 
00072 SINGLETON
00073 
00074 - (void)appendSeparator:(SBJsonStreamWriter *)writer {
00075         [writer appendBytes:"," length:1];
00076 }
00077 @end
00078 
00079 @implementation SBJsonStreamWriterStateObjectValue
00080 
00081 SINGLETON
00082 
00083 - (void)appendSeparator:(SBJsonStreamWriter *)writer {
00084         [writer appendBytes:":" length:1];
00085 }
00086 - (void)transitionState:(SBJsonStreamWriter *)writer {
00087     writer.state = [SBJsonStreamWriterStateObjectKey sharedInstance];
00088 }
00089 - (void)appendWhitespace:(SBJsonStreamWriter *)writer {
00090         [writer appendBytes:" " length:1];
00091 }
00092 @end
00093 
00094 @implementation SBJsonStreamWriterStateArrayStart
00095 
00096 SINGLETON
00097 
00098 - (void)transitionState:(SBJsonStreamWriter *)writer {
00099     writer.state = [SBJsonStreamWriterStateArrayValue sharedInstance];
00100 }
00101 @end
00102 
00103 @implementation SBJsonStreamWriterStateArrayValue
00104 
00105 SINGLETON
00106 
00107 - (void)appendSeparator:(SBJsonStreamWriter *)writer {
00108         [writer appendBytes:"," length:1];
00109 }
00110 @end
00111 
00112 @implementation SBJsonStreamWriterStateStart
00113 
00114 SINGLETON
00115 
00116 
00117 - (void)transitionState:(SBJsonStreamWriter *)writer {
00118     writer.state = [SBJsonStreamWriterStateComplete sharedInstance];
00119 }
00120 - (void)appendSeparator:(SBJsonStreamWriter *)writer {
00121 }
00122 @end
00123 
00124 @implementation SBJsonStreamWriterStateComplete
00125 
00126 SINGLETON
00127 
00128 - (BOOL)isInvalidState:(SBJsonStreamWriter*)writer {
00129         writer.error = @"Stream is closed";
00130         return YES;
00131 }
00132 @end
00133 
00134 @implementation SBJsonStreamWriterStateError
00135 
00136 SINGLETON
00137 
00138 @end
00139