KSJSONCodecObjC.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // KSJSONCodecObjC.h
  3. //
  4. // Created by Karl Stenerud on 2012-01-08.
  5. //
  6. // Copyright (c) 2012 Karl Stenerud. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall remain in place
  16. // in this source code.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. #import <Foundation/Foundation.h>
  27. /** Optional behavior when encoding JSON data */
  28. typedef enum
  29. {
  30. KSJSONEncodeOptionNone = 0,
  31. /** Indent 4 spaces per object/array level */
  32. KSJSONEncodeOptionPretty = 1,
  33. /** Sort object contents by key name */
  34. KSJSONEncodeOptionSorted = 2,
  35. } KSJSONEncodeOption;
  36. /** Optional behavior when decoding JSON data */
  37. typedef enum
  38. {
  39. KSJSONDecodeOptionNone = 0,
  40. /** Normally, null elements get stored as [NSNull null].
  41. * If this option is set, do not store anything when a null element is
  42. * encountered inside an array.
  43. */
  44. KSJSONDecodeOptionIgnoreNullInArray = 1,
  45. /** Normally, null elements get stored as [NSNull null].
  46. * If this option is set, do not store anything when a null element is
  47. * encountered inside an object.
  48. */
  49. KSJSONDecodeOptionIgnoreNullInObject = 2,
  50. /** Convenience enum to ignore nulls in arrays and objects. */
  51. KSJSONDecodeOptionIgnoreAllNulls = 3,
  52. /** If an error is encountered, return the partially decoded object. */
  53. KSJSONDecodeOptionKeepPartialObject = 4,
  54. } KSJSONDecodeOption;
  55. /**
  56. * Encodes and decodes UTF-8 JSON data.
  57. */
  58. @interface KSJSONCodec : NSObject
  59. /** Encode an object to JSON data.
  60. *
  61. * @param object The array or dictionary to encode.
  62. *
  63. * @param options Options for how to encode the data.
  64. *
  65. * @param error Place to store any error that occurs (nil = ignore). Will be
  66. * set to nil on success.
  67. *
  68. * @return The encoded UTF-8 JSON data or nil if an error occurred.
  69. */
  70. + (NSData*) encode:(id) object
  71. options:(KSJSONEncodeOption) options
  72. error:(NSError**) error;
  73. /** Decode JSON data to an object.
  74. *
  75. * @param JSONData The UTF-8 data to decode.
  76. *
  77. * @param options Options for how to decode the data.
  78. *
  79. * @param error Place to store any error that occurs (nil = ignore). Will be
  80. * set to nil on success.
  81. *
  82. * @return The decoded object or, if the KSJSONDecodeOptionKeepPartialFile
  83. * option is not set, nil when an error occurs.
  84. */
  85. + (id) decode:(NSData*) JSONData
  86. options:(KSJSONDecodeOption) options
  87. error:(NSError**) error;
  88. @end