DBJsonWriter.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #import "DBJsonBase.h"
  26. /**
  27. @brief Options for the writer class.
  28. This exists so the DBJSON facade can implement the options in the writer without having to re-declare them.
  29. */
  30. @protocol DBJsonWriter
  31. /**
  32. @brief Whether we are generating human-readable (multiline) JSON.
  33. Set whether or not to generate human-readable JSON. The default is NO, which produces
  34. JSON without any whitespace. (Except inside strings.) If set to YES, generates human-readable
  35. JSON with linebreaks after each array value and dictionary key/value pair, indented two
  36. spaces per nesting level.
  37. */
  38. @property BOOL humanReadable;
  39. /**
  40. @brief Whether or not to sort the dictionary keys in the output.
  41. If this is set to YES, the dictionary keys in the JSON output will be in sorted order.
  42. (This is useful if you need to compare two structures, for example.) The default is NO.
  43. */
  44. @property BOOL sortKeys;
  45. /**
  46. @brief Return JSON representation (or fragment) for the given object.
  47. Returns a string containing JSON representation of the passed in value, or nil on error.
  48. If nil is returned and @p error is not NULL, @p *error can be interrogated to find the cause of the error.
  49. @param value any instance that can be represented as a JSON fragment
  50. */
  51. - (NSString*)stringWithObject:(id)value;
  52. @end
  53. /**
  54. @brief The JSON writer class.
  55. Objective-C types are mapped to JSON types in the following way:
  56. @li NSNull -> Null
  57. @li NSString -> String
  58. @li NSArray -> Array
  59. @li NSDictionary -> Object
  60. @li NSNumber (-initWithBool:) -> Boolean
  61. @li NSNumber -> Number
  62. In JSON the keys of an object must be strings. NSDictionary keys need
  63. not be, but attempting to convert an NSDictionary with non-string keys
  64. into JSON will throw an exception.
  65. NSNumber instances created with the +initWithBool: method are
  66. converted into the JSON boolean "true" and "false" values, and vice
  67. versa. Any other NSNumber instances are converted to a JSON number the
  68. way you would expect.
  69. */
  70. @interface DBJsonWriter : DBJsonBase <DBJsonWriter> {
  71. @private
  72. BOOL sortKeys, humanReadable;
  73. }
  74. @end
  75. // don't use - exists for backwards compatibility. Will be removed in 2.3.
  76. @interface DBJsonWriter (Private)
  77. - (NSString*)stringWithFragment:(id)value;
  78. @end
  79. /**
  80. @brief Allows generation of JSON for otherwise unsupported classes.
  81. If you have a custom class that you want to create a JSON representation for you can implement
  82. this method in your class. It should return a representation of your object defined
  83. in terms of objects that can be translated into JSON. For example, a Person
  84. object might implement it like this:
  85. @code
  86. - (id)jsonProxyObject {
  87. return [NSDictionary dictionaryWithObjectsAndKeys:
  88. name, @"name",
  89. phone, @"phone",
  90. email, @"email",
  91. nil];
  92. }
  93. @endcode
  94. */
  95. @interface NSObject (SBProxyForJson)
  96. - (id)proxyForJson;
  97. @end