SBJsonWriter.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright (C) 2009 Stig Brautaset. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice, this
  6. list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of the author nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without specific
  12. prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  17. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  21. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  22. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #import "SBJsonWriter.h"
  25. #import "SBJsonStreamWriter.h"
  26. #import "SBJsonStreamWriterAccumulator.h"
  27. @interface SBJsonWriter ()
  28. @property (copy) NSString *error;
  29. @end
  30. @implementation SBJsonWriter
  31. @synthesize sortKeys;
  32. @synthesize humanReadable;
  33. @synthesize error;
  34. @synthesize maxDepth;
  35. @synthesize sortKeysComparator;
  36. - (id)init {
  37. self = [super init];
  38. if (self) {
  39. self.maxDepth = 32u;
  40. }
  41. return self;
  42. }
  43. - (NSString*)stringWithObject:(id)value {
  44. NSData *data = [self dataWithObject:value];
  45. if (data)
  46. return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  47. return nil;
  48. }
  49. - (NSString*)stringWithObject:(id)value error:(NSError**)error_ {
  50. NSString *tmp = [self stringWithObject:value];
  51. if (tmp)
  52. return tmp;
  53. if (error_) {
  54. NSDictionary *ui = [NSDictionary dictionaryWithObjectsAndKeys:error, NSLocalizedDescriptionKey, nil];
  55. *error_ = [NSError errorWithDomain:@"org.brautaset.SBJsonWriter.ErrorDomain" code:0 userInfo:ui];
  56. }
  57. return nil;
  58. }
  59. - (NSData*)dataWithObject:(id)object {
  60. self.error = nil;
  61. SBJsonStreamWriterAccumulator *accumulator = [[SBJsonStreamWriterAccumulator alloc] init];
  62. SBJsonStreamWriter *streamWriter = [[SBJsonStreamWriter alloc] init];
  63. streamWriter.sortKeys = self.sortKeys;
  64. streamWriter.maxDepth = self.maxDepth;
  65. streamWriter.sortKeysComparator = self.sortKeysComparator;
  66. streamWriter.humanReadable = self.humanReadable;
  67. streamWriter.delegate = accumulator;
  68. BOOL ok = NO;
  69. if ([object isKindOfClass:[NSDictionary class]])
  70. ok = [streamWriter writeObject:object];
  71. else if ([object isKindOfClass:[NSArray class]])
  72. ok = [streamWriter writeArray:object];
  73. else if ([object respondsToSelector:@selector(proxyForJson)])
  74. return [self dataWithObject:[object proxyForJson]];
  75. else {
  76. self.error = @"Not valid type for JSON";
  77. return nil;
  78. }
  79. if (ok)
  80. return accumulator.data;
  81. self.error = streamWriter.error;
  82. return nil;
  83. }
  84. @end