SBJson.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. Copyright (C) 2009-2011 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. /**
  25. @page json2objc JSON to Objective-C
  26. JSON is mapped to Objective-C types in the following way:
  27. @li null -> NSNull
  28. @li string -> NSString
  29. @li array -> NSMutableArray
  30. @li object -> NSMutableDictionary
  31. @li true -> NSNumber's -numberWithBool:YES
  32. @li false -> NSNumber's -numberWithBool:NO
  33. @li integer up to 19 digits -> NSNumber's -numberWithLongLong:
  34. @li all other numbers -> NSDecimalNumber
  35. Since Objective-C doesn't have a dedicated class for boolean values,
  36. these turns into NSNumber instances. However, since these are
  37. initialised with the -initWithBool: method they round-trip back to JSON
  38. properly. In other words, they won't silently suddenly become 0 or 1;
  39. they'll be represented as 'true' and 'false' again.
  40. As an optimisation integers up to 19 digits in length (the max length
  41. for signed long long integers) turn into NSNumber instances, while
  42. complex ones turn into NSDecimalNumber instances. We can thus avoid any
  43. loss of precision as JSON allows ridiculously large numbers.
  44. @page objc2json Objective-C to JSON
  45. Objective-C types are mapped to JSON types in the following way:
  46. @li NSNull -> null
  47. @li NSString -> string
  48. @li NSArray -> array
  49. @li NSDictionary -> object
  50. @li NSNumber's -initWithBool:YES -> true
  51. @li NSNumber's -initWithBool:NO -> false
  52. @li NSNumber -> number
  53. @note In JSON the keys of an object must be strings. NSDictionary
  54. keys need not be, but attempting to convert an NSDictionary with
  55. non-string keys into JSON will throw an exception.
  56. NSNumber instances created with the -numberWithBool: method are
  57. converted into the JSON boolean "true" and "false" values, and vice
  58. versa. Any other NSNumber instances are converted to a JSON number the
  59. way you would expect.
  60. */
  61. #import "SBJsonParser.h"
  62. #import "SBJsonWriter.h"
  63. #import "SBJsonStreamParser.h"
  64. #import "SBJsonStreamParserAdapter.h"
  65. #import "SBJsonStreamWriter.h"
  66. #import "NSObject+SBJson.h"