SBJsonWriter.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <Foundation/Foundation.h>
  25. /**
  26. @brief The JSON writer class.
  27. This uses SBJsonStreamWriter internally.
  28. @see @ref json2objc
  29. */
  30. @interface SBJsonWriter : NSObject
  31. /**
  32. @brief The maximum recursing depth.
  33. Defaults to 32. If the input is nested deeper than this the input will be deemed to be
  34. malicious and the parser returns nil, signalling an error. ("Nested too deep".) You can
  35. turn off this security feature by setting the maxDepth value to 0.
  36. */
  37. @property NSUInteger maxDepth;
  38. /**
  39. @brief Return an error trace, or nil if there was no errors.
  40. Note that this method returns the trace of the last method that failed.
  41. You need to check the return value of the call you're making to figure out
  42. if the call actually failed, before you know call this method.
  43. */
  44. @property (readonly, copy) NSString *error;
  45. /**
  46. @brief Whether we are generating human-readable (multiline) JSON.
  47. Set whether or not to generate human-readable JSON. The default is NO, which produces
  48. JSON without any whitespace. (Except inside strings.) If set to YES, generates human-readable
  49. JSON with linebreaks after each array value and dictionary key/value pair, indented two
  50. spaces per nesting level.
  51. */
  52. @property BOOL humanReadable;
  53. /**
  54. @brief Whether or not to sort the dictionary keys in the output.
  55. If this is set to YES, the dictionary keys in the JSON output will be in sorted order.
  56. (This is useful if you need to compare two structures, for example.) The default is NO.
  57. */
  58. @property BOOL sortKeys;
  59. /**
  60. @brief An optional comparator to be used if sortKeys is YES.
  61. If this is nil, sorting will be done via @selector(compare:).
  62. */
  63. @property (copy) NSComparator sortKeysComparator;
  64. /**
  65. @brief Return JSON representation for the given object.
  66. Returns a string containing JSON representation of the passed in value, or nil on error.
  67. If nil is returned and @p error is not NULL, @p *error can be interrogated to find the cause of the error.
  68. @param value any instance that can be represented as JSON text.
  69. */
  70. - (NSString*)stringWithObject:(id)value;
  71. /**
  72. @brief Return JSON representation for the given object.
  73. Returns an NSData object containing JSON represented as UTF8 text, or nil on error.
  74. @param value any instance that can be represented as JSON text.
  75. */
  76. - (NSData*)dataWithObject:(id)value;
  77. /**
  78. @brief Return JSON representation (or fragment) for the given object.
  79. Returns a string containing JSON representation of the passed in value, or nil on error.
  80. If nil is returned and @p error is not NULL, @p *error can be interrogated to find the cause of the error.
  81. @param value any instance that can be represented as a JSON fragment
  82. @param error pointer to object to be populated with NSError on failure
  83. */- (NSString*)stringWithObject:(id)value
  84. error:(NSError**)error;
  85. @end