PageRenderTime 26ms CodeModel.GetById 17ms app.highlight 6ms RepoModel.GetById 2ms 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
  1/*
  2Copyright (C) 2008 Stig Brautaset. All rights reserved.
  3
  4Redistribution and use in source and binary forms, with or without
  5modification, are permitted provided that the following conditions are met:
  6
  7  Redistributions of source code must retain the above copyright notice, this
  8  list of conditions and the following disclaimer.
  9
 10  Redistributions in binary form must reproduce the above copyright notice,
 11  this list of conditions and the following disclaimer in the documentation
 12  and/or other materials provided with the distribution.
 13
 14  Neither the name of the author nor the names of its contributors may be used
 15  to endorse or promote products derived from this software without specific
 16  prior written permission.
 17
 18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 20IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 21DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 22FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 23DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 25CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 26OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 27OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28*/
 29
 30#import <Foundation/Foundation.h>
 31
 32extern NSString * SBJSONErrorDomain;
 33
 34enum {
 35    EUNSUPPORTED = 1,
 36    EPARSENUM,
 37    EPARSE,
 38    EFRAGMENT,
 39    ECTRL,
 40    EUNICODE,
 41    EDEPTH,
 42    EESCAPE,
 43    ETRAILCOMMA,
 44    ETRAILGARBAGE,
 45    EEOF,
 46    EINPUT
 47};
 48
 49/**
 50@brief A strict JSON parser and generator
 51
 52This is the parser and generator underlying the categories added to
 53NSString and various other objects.
 54
 55Objective-C types are mapped to JSON types and back in the following way:
 56
 57@li NSNull -> Null -> NSNull
 58@li NSString -> String -> NSMutableString
 59@li NSArray -> Array -> NSMutableArray
 60@li NSDictionary -> Object -> NSMutableDictionary
 61@li NSNumber (-initWithBool:) -> Boolean -> NSNumber -initWithBool:
 62@li NSNumber -> Number -> NSDecimalNumber
 63
 64In JSON the keys of an object must be strings. NSDictionary keys need
 65not be, but attempting to convert an NSDictionary with non-string keys
 66into JSON will throw an exception.
 67
 68NSNumber instances created with the +numberWithBool: method are
 69converted into the JSON boolean "true" and "false" values, and vice
 70versa. Any other NSNumber instances are converted to a JSON number the
 71way you would expect. JSON numbers turn into NSDecimalNumber instances,
 72as we can thus avoid any loss of precision.
 73
 74Strictly speaking correctly formed JSON text must have <strong>exactly
 75one top-level container</strong>. (Either an Array or an Object.) Scalars,
 76i.e. nulls, numbers, booleans and strings, are not valid JSON on their own.
 77It can be quite convenient to pretend that such fragments are valid
 78JSON however, and this class lets you do so.
 79
 80This class does its best to be as strict as possible, both in what it
 81accepts and what it generates. (Other than the above mentioned support
 82for JSON fragments.) For example, it does not support trailing commas
 83in arrays or objects. Nor does it support embedded comments, or
 84anything else not in the JSON specification.
 85 
 86*/
 87@interface SBJSON : NSObject {
 88    BOOL humanReadable;
 89    BOOL sortKeys;
 90    NSUInteger maxDepth;
 91
 92@private
 93    // Used temporarily during scanning/generation
 94    NSUInteger depth;
 95    const char *c;
 96}
 97
 98/// Whether we are generating human-readable (multiline) JSON
 99/**
100 Set whether or not to generate human-readable JSON. The default is NO, which produces
101 JSON without any whitespace. (Except inside strings.) If set to YES, generates human-readable
102 JSON with linebreaks after each array value and dictionary key/value pair, indented two
103 spaces per nesting level.
104 */
105@property BOOL humanReadable;
106
107/// Whether or not to sort the dictionary keys in the output
108/** The default is to not sort the keys. */
109@property BOOL sortKeys;
110
111/// The maximum depth the parser will go to
112/** Defaults to 512. */
113@property NSUInteger maxDepth;
114
115/// Return JSON representation of an array  or dictionary
116- (NSString*)stringWithObject:(id)value error:(NSError**)error;
117
118/// Return JSON representation of any legal JSON value
119- (NSString*)stringWithFragment:(id)value error:(NSError**)error;
120
121/// Return the object represented by the given string
122- (id)objectWithString:(NSString*)jsonrep error:(NSError**)error;
123
124/// Return the fragment represented by the given string
125- (id)fragmentWithString:(NSString*)jsonrep error:(NSError**)error;
126
127/// Return JSON representation (or fragment) for the given object
128- (NSString*)stringWithObject:(id)value
129                  allowScalar:(BOOL)x
130    					error:(NSError**)error;
131
132/// Parse the string and return the represented object (or scalar)
133- (id)objectWithString:(id)value
134           allowScalar:(BOOL)x
135    			 error:(NSError**)error;
136
137@end