-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathJSONAPIResource.h
More file actions
164 lines (139 loc) · 6.58 KB
/
Copy pathJSONAPIResource.h
File metadata and controls
164 lines (139 loc) · 6.58 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// JSONAPIResource.h
// JSONAPI
//
// Created by Jonathan Karl Armstrong, 2015.
//
#import <Foundation/Foundation.h>
@class JSONAPI;
@class JSONAPIPropertyDescriptor;
@class JSONAPIResourceDescriptor;
/**
* Protocol of an object that is available for JSON API serialization.
*
* When developing model classes for use with JSON-API, it is suggested that classes
* be derived from <JSONAPIResourceBase>, but that is not required. An existing model
* class can be adapted for JSON-API by implementing this protocol.
*/
@protocol JSONAPIResource <NSObject>
#pragma mark - Class Methods
/**
Get the JSON API resource metadata description. This will be different for each resource
model class. It must be defined by the subclass.
The definition should look something like:
<pre><code>
#import <JSONAPI/JSONAPIPropertyDescriptor.h>
#import <JSONAPI/JSONAPIResourceDescriptor.h>
@implementation PeopleResource
static JSONAPIResourceDescriptor *__descriptor = nil;
+ (JSONAPIResourceDescriptor*)descriptor {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__descriptor = [[JSONAPIResourceDescriptor alloc] initWithClass:[self class] forLinkedType:@"people"];
[__descriptor setIdProperty:@"ID"];
[__descriptor addProperty:@"telephone"];
[__descriptor addProperty:@"birthday" withDescription:[[JSONAPIPropertyDescriptor alloc] initWithJsonName:@"birthday" withFormat:[NSDateFormatter RFC3339DateFormatter]]];
[__descriptor addProperty:@"firstName" withDescription:[[JSONAPIPropertyDescriptor alloc] initWithJsonName:@"first"]];
[__descriptor addProperty:@"lastName" withDescription:[[JSONAPIPropertyDescriptor alloc] initWithJsonName:@"last"]];
});
return __descriptor;
}
</code></pre>
In this example, `PeopleResource` is a class that inherits from <JSONAPIResourceBase>
(defines property 'ID'), and defines properties `NSString *telephone`, `NSDate *birthday`,
`NSString *firstName` and `NSString *lastName`.
* The `telephone` property needs no special transform rules.
* The `birthday` property must be transformed into a string for JSON.
* The API you are targeting uses the labels `first` and `last` for the last two properties,
which you prefer to relabel internally.
@return Resource description for the target model class.
*/
+ (JSONAPIResourceDescriptor*)descriptor;
#pragma mark - Properties
/**
* Get the URL that corresponds to this resource. May be nil. Should be set if returned from a
* server. A GET request on the JSON-API endpoint should return the same resource.
*
* There should be no <JSONAPIPropertyDescriptor> for this property. It is set from the 'links'
* property in the JSON body, and the JSON value is always a string URL.
*
* In general, this should be implemented by a @property selfLink in the realized class. The
* @property declaration will automatically synthesize the get/set members declared in this
* protocol. The property storage is an implementation detail, which is why the protocol does
* not use a @property declaration.
*
* @return The URL that corresponds to this resource.
*/
- (NSString *)selfLink;
/**
* Set the URL that corresponds to this resource. This attribute is set from the 'links'
* property in the JSON body, and the JSON value is always a string URL. A GET request on the
* JSON-API endpoint should return the same resource.
*
* In general, this should be implemented by a @property selfLink in the realized class. The
* @property declaration will automatically synthesize the get/set members declared in this
* protocol. The property storage is an implementation detail, which is why the protocol does
* not use a @property declaration.
*
* @param path The URL that corresponds to this resource.
*/
- (void)setSelfLink:(NSString*)path;
/**
* Get the API record identifier for a resource instance. Required for resources that come
* from persistance storage (i.e. the server), but may be nil for new records that have not
* been saved. Every saved resource record should be uniquely identifiable by the combination
* of type and ID.
*
* This is typically a database sequence number associated withe the resource record,
* but that is not required. The JSON API requires ID to be serialized as a string.
*
* In general, this should be implemented by a @property ID in the realized class. The
* @property declaration will automatically synthesize the get/set members declared in this
* protocol. The property storage is an implementation detail, which is why the protocol does
* not use a @property declaration.
*
* @return The record identifier for a resource instance.
*/
- (id)iD;
/**
* Set the API record identifier for a resource instance. Required for resources that come
* from persistance storage (i.e. the server), but may be nil for new records that have not
* been saved. Every saved resource record should be uniquely identifiable by the combination
* of type and ID.
*
* This is typically a database sequence number associated withe the resource record,
* but that is not required. The JSON API requires ID to be serialized as a string.
*
* In general, this should be implemented by a @property ID in the realized class. The
* @property declaration will automatically synthesize the get/set members declared in this
* protocol. The property storage is an implementation detail, which is why the protocol does
* not use a @property declaration.
*
* @param identifier The record identifier for a resource instance.
*/
- (void)setID:(id)identifier;
/**
* Get the meta for a resource instance. Optional for resources that come
* from persistance storage (i.e. the server).
*
* In general, this should be implemented by a @property ID in the realized class. The
* @property declaration will automatically synthesize the get/set members declared in this
* protocol. The property storage is an implementation detail, which is why the protocol does
* not use a @property declaration.
*
* @return The meta for a resource instance.
*/
- (NSDictionary*)meta;
/**
* Set the meta for a resource instance. Optional for resources that come
* from persistance storage (i.e. the server).
*
* In general, this should be implemented by a @property ID in the realized class. The
* @property declaration will automatically synthesize the get/set members declared in this
* protocol. The property storage is an implementation detail, which is why the protocol does
* not use a @property declaration.
*
* @param meta The meta for a resource instance.
*/
- (void)setMeta:(NSDictionary*)meta;
@end