This repository has been archived by the owner on Jul 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTypeJSON.h
145 lines (116 loc) · 4.31 KB
/
TypeJSON.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#import <Foundation/Foundation.h>
extern NSString *TypeJSONErrorDomain;
/// Represents a JSON value, with an Objective-C interface that enables simple type-coersion and
/// tree-traversal.
@interface TypeJSON : NSObject
/// Parse UTF-8 JSON data as `JSON`.
///
/// @param object Valid JSON data
///
/// @return A JSON object based on the provided data or any errors encounter during parsing and initialization
+ (instancetype)fromData:(NSData *)object;
/// Creates a new JSON to represent the empty object, `{}`.
///
/// @return A new JSON instance
+ (instancetype)emptyObject;
#pragma mark Collection Accessors
/// Accesses the object located at the specified key, assuming the value to be an object.
///
/// If the value is not an object or if the key is not found, this method
/// will still succeed and return a `JSON` to represent the error.
///
/// If the value is already an error, the returned JSON will represent that same error.
///
/// @param index An index within the bounds of the array
///
/// @return A JSON either representing the indexed object or with an error
- (TypeJSON *)objectForKeyedSubscript:(NSString *)key;
/// Accesses the object located at the specified index, assuming the value to be an array.
///
/// If the value is not an array or if the index is out of bounds, this method
/// will still succeed and return a `JSON`.
///
/// If the value is already an error, the returned JSON will represent that same error.
///
/// @param index An index within the bounds of the array
///
/// @return A JSON either representing the indexed object or with an error
- (TypeJSON *)objectAtIndexedSubscript:(NSUInteger)index;
#pragma mark Value Accessors
/// Accesses the JSON value as a `string`.
///
/// @return Value as an `NSString`
- (NSString *)asString;
/// Accesses the JSON value as a `number`.
///
/// @return Value as an `NSDecimalNumber`
- (NSDecimalNumber *)asNumber;
/// Interprets the JSON value as either true or false.
///
/// Note that this is a convenience method; in JSON true and false are simply constant values,
/// which are not semantically related.
///
/// @return `YES` if the value is `true`, `NO` if the value is `false`, otherwise behavior is undefined
- (BOOL)asBool;
/// Accesses the JSON value as `null`, or nil if it is not nil.
///
/// Note the important differences between `nil` (Objective C language construct), `null` (JSON value) and `NSNull.null` (`Foundation` object) in this context.
///
/// @return NSNull.null or nil
- (NSNull *)asNull;
/// Accesses the JSON value as an `array`, with each value represented by a `JSON`, or nil if the value is not an array.
///
/// @return An array of _values_
- (NSArray *)asArray;
/// Access the JSON value as an `object`.
///
/// @return An NSDictionary to represent the value, or nil if it is not an `object`
- (NSDictionary *)asObject;
#pragma mark Type Checking
/// Checks whether value is `true`.
///
/// @return `YES` if the value is `true` or `NO` otherwise
- (BOOL)isTrue;
/// Checks whether value is `false`.
///
/// @return `YES` if the value is `false` or `NO` otherwise
- (BOOL)isFalse;
/// Checks whether value is `null`.
///
/// @return `YES` if the value is `null`, `NO` otherwise
- (BOOL)isNull;
/// Checks whether value is a `number`.
///
/// @return `YES` if the value is a `number`, `NO` otherwise
- (BOOL)isNumber;
/// Checks whether value is a `string`
///
/// @return `YES` if the value is a `number`, `NO` otherwise
- (BOOL)isString;
/// Checks whether value is an `array`
///
/// @return `YES` if the value is an `array`, `NO` otherwise
- (BOOL)isArray;
/// Checks whether value is an `object`
///
/// @return `YES` if the value is an `object`, `NO` otherwise
- (BOOL)isObject;
#pragma mark Error Handling
/// Checks whether the JSON has an error associated with it.
///
/// @return `YES` if there is an error, `NO` otherwise
- (BOOL)isError;
/// Returns the error associated with the JSON if one is present
///
/// @return An error or nil
- (NSError *)asError;
#pragma mark Serialization
/// Serializes the value as JSON.
///
/// @return UTF-8 Encoded JSON
- (NSData *)asJSON;
/// Serializes the value as JSON with optional pretty printing.
///
/// @return UTF-8 Encoded JSON, which is pretty printed when NSJSONWritingPrettyPrinted is passed
- (NSData *)asJSONWithOptions:(NSJSONWritingOptions)options;
@end