2 Copyright (C) 2009 Stig Brautaset. All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright notice, this
8 list of conditions and the following disclaimer.
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.
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.
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #import <Foundation/Foundation.h>
31 #import "SBJsonBase.h"
34 @brief Options for the writer class.
36 This exists so the SBJSON facade can implement the options in the writer without having to re-declare them.
38 @protocol SBJsonWriter
41 @brief Whether we are generating human-readable (multiline) JSON.
43 Set whether or not to generate human-readable JSON. The default is NO, which produces
44 JSON without any whitespace. (Except inside strings.) If set to YES, generates human-readable
45 JSON with linebreaks after each array value and dictionary key/value pair, indented two
46 spaces per nesting level.
48 @property BOOL humanReadable;
51 @brief Whether or not to sort the dictionary keys in the output.
53 If this is set to YES, the dictionary keys in the JSON output will be in sorted order.
54 (This is useful if you need to compare two structures, for example.) The default is NO.
56 @property BOOL sortKeys;
59 @brief Return JSON representation (or fragment) for the given object.
61 Returns a string containing JSON representation of the passed in value, or nil on error.
62 If nil is returned and @p error is not NULL, @p *error can be interrogated to find the cause of the error.
64 @param value any instance that can be represented as a JSON fragment
67 - (NSString*)stringWithObject:(id)value;
73 @brief The JSON writer class.
75 Objective-C types are mapped to JSON types in the following way:
78 @li NSString -> String
80 @li NSDictionary -> Object
81 @li NSNumber (-initWithBool:) -> Boolean
82 @li NSNumber -> Number
84 In JSON the keys of an object must be strings. NSDictionary keys need
85 not be, but attempting to convert an NSDictionary with non-string keys
86 into JSON will throw an exception.
88 NSNumber instances created with the +initWithBool: method are
89 converted into the JSON boolean "true" and "false" values, and vice
90 versa. Any other NSNumber instances are converted to a JSON number the
94 @interface SBJsonWriter : SBJsonBase <SBJsonWriter> {
97 BOOL sortKeys, humanReadable;
102 // don't use - exists for backwards compatibility. Will be removed in 2.3.
103 @interface SBJsonWriter (Private)
104 - (NSString*)stringWithFragment:(id)value;
108 @brief Allows generation of JSON for otherwise unsupported classes.
110 If you have a custom class that you want to create a JSON representation for you can implement
111 this method in your class. It should return a representation of your object defined
112 in terms of objects that can be translated into JSON. For example, a Person
113 object might implement it like this:
116 - (id)jsonProxyObject {
117 return [NSDictionary dictionaryWithObjectsAndKeys:
126 @interface NSObject (SBProxyForJson)