SBJsonStreamWriterState.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. Copyright (c) 2010, Stig Brautaset.
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. Neither the name of the the author nor the names of its contributors
  13. may be used to endorse or promote products derived from this software
  14. without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  16. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  17. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  18. PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #import "SBJsonStreamWriterState.h"
  28. #import "SBJsonStreamWriter.h"
  29. #define SINGLETON \
  30. + (id)sharedInstance { \
  31. static id state = nil; \
  32. if (!state) { \
  33. @synchronized(self) { \
  34. if (!state) state = [[self alloc] init]; \
  35. } \
  36. } \
  37. return state; \
  38. }
  39. @implementation SBJsonStreamWriterState
  40. + (id)sharedInstance { return nil; }
  41. - (BOOL)isInvalidState:(SBJsonStreamWriter*)writer { return NO; }
  42. - (void)appendSeparator:(SBJsonStreamWriter*)writer {}
  43. - (BOOL)expectingKey:(SBJsonStreamWriter*)writer { return NO; }
  44. - (void)transitionState:(SBJsonStreamWriter *)writer {}
  45. - (void)appendWhitespace:(SBJsonStreamWriter*)writer {
  46. [writer appendBytes:"\n" length:1];
  47. for (NSUInteger i = 0; i < writer.stateStack.count; i++)
  48. [writer appendBytes:" " length:2];
  49. }
  50. @end
  51. @implementation SBJsonStreamWriterStateObjectStart
  52. SINGLETON
  53. - (void)transitionState:(SBJsonStreamWriter *)writer {
  54. writer.state = [SBJsonStreamWriterStateObjectValue sharedInstance];
  55. }
  56. - (BOOL)expectingKey:(SBJsonStreamWriter *)writer {
  57. writer.error = @"JSON object key must be string";
  58. return YES;
  59. }
  60. @end
  61. @implementation SBJsonStreamWriterStateObjectKey
  62. SINGLETON
  63. - (void)appendSeparator:(SBJsonStreamWriter *)writer {
  64. [writer appendBytes:"," length:1];
  65. }
  66. @end
  67. @implementation SBJsonStreamWriterStateObjectValue
  68. SINGLETON
  69. - (void)appendSeparator:(SBJsonStreamWriter *)writer {
  70. [writer appendBytes:":" length:1];
  71. }
  72. - (void)transitionState:(SBJsonStreamWriter *)writer {
  73. writer.state = [SBJsonStreamWriterStateObjectKey sharedInstance];
  74. }
  75. - (void)appendWhitespace:(SBJsonStreamWriter *)writer {
  76. [writer appendBytes:" " length:1];
  77. }
  78. @end
  79. @implementation SBJsonStreamWriterStateArrayStart
  80. SINGLETON
  81. - (void)transitionState:(SBJsonStreamWriter *)writer {
  82. writer.state = [SBJsonStreamWriterStateArrayValue sharedInstance];
  83. }
  84. @end
  85. @implementation SBJsonStreamWriterStateArrayValue
  86. SINGLETON
  87. - (void)appendSeparator:(SBJsonStreamWriter *)writer {
  88. [writer appendBytes:"," length:1];
  89. }
  90. @end
  91. @implementation SBJsonStreamWriterStateStart
  92. SINGLETON
  93. - (void)transitionState:(SBJsonStreamWriter *)writer {
  94. writer.state = [SBJsonStreamWriterStateComplete sharedInstance];
  95. }
  96. - (void)appendSeparator:(SBJsonStreamWriter *)writer {
  97. }
  98. @end
  99. @implementation SBJsonStreamWriterStateComplete
  100. SINGLETON
  101. - (BOOL)isInvalidState:(SBJsonStreamWriter*)writer {
  102. writer.error = @"Stream is closed";
  103. return YES;
  104. }
  105. @end
  106. @implementation SBJsonStreamWriterStateError
  107. SINGLETON
  108. @end