PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/core/externals/update-engine/externals/gdata-objectivec-client/Source/JSON/SBJSON.h

http://macfuse.googlecode.com/
C++ Header | 137 lines | 38 code | 15 blank | 84 comment | 0 complexity | a4ffbfe93af8ff81690edcc6d1823260 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0
  1. /*
  2. Copyright (C) 2008 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. extern NSString * SBJSONErrorDomain;
  26. enum {
  27. EUNSUPPORTED = 1,
  28. EPARSENUM,
  29. EPARSE,
  30. EFRAGMENT,
  31. ECTRL,
  32. EUNICODE,
  33. EDEPTH,
  34. EESCAPE,
  35. ETRAILCOMMA,
  36. ETRAILGARBAGE,
  37. EEOF,
  38. EINPUT
  39. };
  40. /**
  41. @brief A strict JSON parser and generator
  42. This is the parser and generator underlying the categories added to
  43. NSString and various other objects.
  44. Objective-C types are mapped to JSON types and back in the following way:
  45. @li NSNull -> Null -> NSNull
  46. @li NSString -> String -> NSMutableString
  47. @li NSArray -> Array -> NSMutableArray
  48. @li NSDictionary -> Object -> NSMutableDictionary
  49. @li NSNumber (-initWithBool:) -> Boolean -> NSNumber -initWithBool:
  50. @li NSNumber -> Number -> NSDecimalNumber
  51. In JSON the keys of an object must be strings. NSDictionary keys need
  52. not be, but attempting to convert an NSDictionary with non-string keys
  53. into JSON will throw an exception.
  54. NSNumber instances created with the +numberWithBool: method are
  55. converted into the JSON boolean "true" and "false" values, and vice
  56. versa. Any other NSNumber instances are converted to a JSON number the
  57. way you would expect. JSON numbers turn into NSDecimalNumber instances,
  58. as we can thus avoid any loss of precision.
  59. Strictly speaking correctly formed JSON text must have <strong>exactly
  60. one top-level container</strong>. (Either an Array or an Object.) Scalars,
  61. i.e. nulls, numbers, booleans and strings, are not valid JSON on their own.
  62. It can be quite convenient to pretend that such fragments are valid
  63. JSON however, and this class lets you do so.
  64. This class does its best to be as strict as possible, both in what it
  65. accepts and what it generates. (Other than the above mentioned support
  66. for JSON fragments.) For example, it does not support trailing commas
  67. in arrays or objects. Nor does it support embedded comments, or
  68. anything else not in the JSON specification.
  69. */
  70. @interface SBJSON : NSObject {
  71. BOOL humanReadable;
  72. BOOL sortKeys;
  73. NSUInteger maxDepth;
  74. @private
  75. // Used temporarily during scanning/generation
  76. NSUInteger depth;
  77. const char *c;
  78. }
  79. /// Whether we are generating human-readable (multiline) JSON
  80. /**
  81. Set whether or not to generate human-readable JSON. The default is NO, which produces
  82. JSON without any whitespace. (Except inside strings.) If set to YES, generates human-readable
  83. JSON with linebreaks after each array value and dictionary key/value pair, indented two
  84. spaces per nesting level.
  85. */
  86. @property BOOL humanReadable;
  87. /// Whether or not to sort the dictionary keys in the output
  88. /** The default is to not sort the keys. */
  89. @property BOOL sortKeys;
  90. /// The maximum depth the parser will go to
  91. /** Defaults to 512. */
  92. @property NSUInteger maxDepth;
  93. /// Return JSON representation of an array or dictionary
  94. - (NSString*)stringWithObject:(id)value error:(NSError**)error;
  95. /// Return JSON representation of any legal JSON value
  96. - (NSString*)stringWithFragment:(id)value error:(NSError**)error;
  97. /// Return the object represented by the given string
  98. - (id)objectWithString:(NSString*)jsonrep error:(NSError**)error;
  99. /// Return the fragment represented by the given string
  100. - (id)fragmentWithString:(NSString*)jsonrep error:(NSError**)error;
  101. /// Return JSON representation (or fragment) for the given object
  102. - (NSString*)stringWithObject:(id)value
  103. allowScalar:(BOOL)x
  104. error:(NSError**)error;
  105. /// Parse the string and return the represented object (or scalar)
  106. - (id)objectWithString:(id)value
  107. allowScalar:(BOOL)x
  108. error:(NSError**)error;
  109. @end