SBJsonStreamWriterState.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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; \
  32. if (!state) state = [[self alloc] init]; \
  33. return state; \
  34. }
  35. @implementation SBJsonStreamWriterState
  36. + (id)sharedInstance { return nil; }
  37. - (BOOL)isInvalidState:(SBJsonStreamWriter*)writer { return NO; }
  38. - (void)appendSeparator:(SBJsonStreamWriter*)writer {}
  39. - (BOOL)expectingKey:(SBJsonStreamWriter*)writer { return NO; }
  40. - (void)transitionState:(SBJsonStreamWriter *)writer {}
  41. - (void)appendWhitespace:(SBJsonStreamWriter*)writer {
  42. [writer appendBytes:"\n" length:1];
  43. for (NSUInteger i = 0; i < writer.stateStack.count; i++)
  44. [writer appendBytes:" " length:2];
  45. }
  46. @end
  47. @implementation SBJsonStreamWriterStateObjectStart
  48. SINGLETON
  49. - (void)transitionState:(SBJsonStreamWriter *)writer {
  50. writer.state = [SBJsonStreamWriterStateObjectValue sharedInstance];
  51. }
  52. - (BOOL)expectingKey:(SBJsonStreamWriter *)writer {
  53. writer.error = @"JSON object key must be string";
  54. return YES;
  55. }
  56. @end
  57. @implementation SBJsonStreamWriterStateObjectKey
  58. SINGLETON
  59. - (void)appendSeparator:(SBJsonStreamWriter *)writer {
  60. [writer appendBytes:"," length:1];
  61. }
  62. @end
  63. @implementation SBJsonStreamWriterStateObjectValue
  64. SINGLETON
  65. - (void)appendSeparator:(SBJsonStreamWriter *)writer {
  66. [writer appendBytes:":" length:1];
  67. }
  68. - (void)transitionState:(SBJsonStreamWriter *)writer {
  69. writer.state = [SBJsonStreamWriterStateObjectKey sharedInstance];
  70. }
  71. - (void)appendWhitespace:(SBJsonStreamWriter *)writer {
  72. [writer appendBytes:" " length:1];
  73. }
  74. @end
  75. @implementation SBJsonStreamWriterStateArrayStart
  76. SINGLETON
  77. - (void)transitionState:(SBJsonStreamWriter *)writer {
  78. writer.state = [SBJsonStreamWriterStateArrayValue sharedInstance];
  79. }
  80. @end
  81. @implementation SBJsonStreamWriterStateArrayValue
  82. SINGLETON
  83. - (void)appendSeparator:(SBJsonStreamWriter *)writer {
  84. [writer appendBytes:"," length:1];
  85. }
  86. @end
  87. @implementation SBJsonStreamWriterStateStart
  88. SINGLETON
  89. - (void)transitionState:(SBJsonStreamWriter *)writer {
  90. writer.state = [SBJsonStreamWriterStateComplete sharedInstance];
  91. }
  92. - (void)appendSeparator:(SBJsonStreamWriter *)writer {
  93. }
  94. @end
  95. @implementation SBJsonStreamWriterStateComplete
  96. SINGLETON
  97. - (BOOL)isInvalidState:(SBJsonStreamWriter*)writer {
  98. writer.error = @"Stream is closed";
  99. return YES;
  100. }
  101. @end
  102. @implementation SBJsonStreamWriterStateError
  103. SINGLETON
  104. @end