Added public object functionality.
[pithos-ios] / Classes / SBJsonWriter.h
1 /*
2  Copyright (C) 2009 Stig Brautaset. All rights reserved.
3  
4  Redistribution and use in source and binary forms, with or without
5  modification, 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  
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.
28  */
29
30 #import <Foundation/Foundation.h>
31 #import "SBJsonBase.h"
32
33 /**
34  @brief Options for the writer class.
35  
36  This exists so the SBJSON facade can implement the options in the writer without having to re-declare them.
37  */
38 @protocol SBJsonWriter
39
40 /**
41  @brief Whether we are generating human-readable (multiline) JSON.
42  
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.
47  */
48 @property BOOL humanReadable;
49
50 /**
51  @brief Whether or not to sort the dictionary keys in the output.
52  
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.
55  */
56 @property BOOL sortKeys;
57
58 /**
59  @brief Return JSON representation (or fragment) for the given object.
60  
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.
63  
64  @param value any instance that can be represented as a JSON fragment
65  
66  */
67 - (NSString*)stringWithObject:(id)value;
68
69 @end
70
71
72 /**
73  @brief The JSON writer class.
74  
75  Objective-C types are mapped to JSON types in the following way:
76  
77  @li NSNull -> Null
78  @li NSString -> String
79  @li NSArray -> Array
80  @li NSDictionary -> Object
81  @li NSNumber (-initWithBool:) -> Boolean
82  @li NSNumber -> Number
83  
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.
87  
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
91  way you would expect.
92  
93  */
94 @interface SBJsonWriter : SBJsonBase <SBJsonWriter> {
95
96 @private
97     BOOL sortKeys, humanReadable;
98 }
99
100 @end
101
102 // don't use - exists for backwards compatibility. Will be removed in 2.3.
103 @interface SBJsonWriter (Private)
104 - (NSString*)stringWithFragment:(id)value;
105 @end
106
107 /**
108  @brief Allows generation of JSON for otherwise unsupported classes.
109  
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:
114  
115  @code
116  - (id)jsonProxyObject {
117     return [NSDictionary dictionaryWithObjectsAndKeys:
118         name, @"name",
119         phone, @"phone",
120         email, @"email",
121         nil];
122  }
123  @endcode
124  
125  */
126 @interface NSObject (SBProxyForJson)
127 - (id)proxyForJson;
128 @end
129