-
Notifications
You must be signed in to change notification settings - Fork 1k
/
JSONKeyMapper.h
102 lines (87 loc) · 4.86 KB
/
JSONKeyMapper.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
//
// JSONKeyMapper.h
//
// @version 1.4.1
// @author Marin Todorov (http://www.underplot.com) and contributors
//
// Copyright (c) 2012-2015 Marin Todorov, Underplot ltd.
// This code is distributed under the terms and conditions of the MIT license.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import <Foundation/Foundation.h>
typedef NSString *(^JSONModelKeyMapBlock)(NSString *keyName);
/**
* **You won't need to create or store instances of this class yourself.** If you want your model
* to have different property names than the JSON feed keys, look below on how to
* make your model use a key mapper.
*
* For example if you consume JSON from twitter
* you get back underscore_case style key names. For example:
*
* <pre>"profile_sidebar_border_color": "0094C2",
* "profile_background_tile": false,</pre>
*
* To comply with Obj-C accepted camelCase property naming for your classes,
* you need to provide mapping between JSON keys and ObjC property names.
*
* In your model overwrite the + (JSONKeyMapper *)keyMapper method and provide a JSONKeyMapper
* instance to convert the key names for your model.
*
* If you need custom mapping it's as easy as:
* <pre>
* + (JSONKeyMapper *)keyMapper {
* return [[JSONKeyMapper alloc] initWithDictionary:@{@"crazy_JSON_name":@"myCamelCaseName"}];
* }
* </pre>
* In case you want to handle underscore_case, **use the predefined key mapper**, like so:
* <pre>
* + (JSONKeyMapper *)keyMapper {
* return [JSONKeyMapper mapperFromUnderscoreCaseToCamelCase];
* }
* </pre>
*/
@interface JSONKeyMapper : NSObject
// deprecated
@property (readonly, nonatomic) JSONModelKeyMapBlock JSONToModelKeyBlock DEPRECATED_ATTRIBUTE;
- (NSString *)convertValue:(NSString *)value isImportingToModel:(BOOL)importing DEPRECATED_MSG_ATTRIBUTE("use convertValue:");
- (instancetype)initWithDictionary:(NSDictionary *)map DEPRECATED_MSG_ATTRIBUTE("use initWithModelToJSONDictionary:");
- (instancetype)initWithJSONToModelBlock:(JSONModelKeyMapBlock)toModel modelToJSONBlock:(JSONModelKeyMapBlock)toJSON DEPRECATED_MSG_ATTRIBUTE("use initWithModelToJSONBlock:");
+ (instancetype)mapper:(JSONKeyMapper *)baseKeyMapper withExceptions:(NSDictionary *)exceptions DEPRECATED_MSG_ATTRIBUTE("use baseMapper:withModelToJSONExceptions:");
/** @name Name converters */
/** Block, which takes in a property name and converts it to the corresponding JSON key name */
@property (readonly, nonatomic) JSONModelKeyMapBlock modelToJSONKeyBlock;
/** Combined converter method
* @param value the source name
* @return JSONKeyMapper instance
*/
- (NSString *)convertValue:(NSString *)value;
/** @name Creating a key mapper */
/**
* Creates a JSONKeyMapper instance, based on the block you provide this initializer.
* The parameter takes in a JSONModelKeyMapBlock block:
* <pre>NSString *(^JSONModelKeyMapBlock)(NSString *keyName)</pre>
* The block takes in a string and returns the transformed (if at all) string.
* @param toJSON transforms your model property name to a JSON key
*/
- (instancetype)initWithModelToJSONBlock:(JSONModelKeyMapBlock)toJSON;
/**
* Creates a JSONKeyMapper instance, based on the mapping you provide.
* Use your JSONModel property names as keys, and the JSON key names as values.
* @param toJSON map dictionary, in the format: <pre>@{@"myCamelCaseName":@"crazy_JSON_name"}</pre>
* @return JSONKeyMapper instance
*/
- (instancetype)initWithModelToJSONDictionary:(NSDictionary *)toJSON;
/**
* Creates a JSONKeyMapper, which converts underscore_case to camelCase and vice versa.
*/
+ (instancetype)mapperFromUnderscoreCaseToCamelCase;
+ (instancetype)mapperFromUpperCaseToLowerCase;
/**
* Creates a JSONKeyMapper based on a built-in JSONKeyMapper, with specific exceptions.
* Use your JSONModel property names as keys, and the JSON key names as values.
*/
+ (instancetype)baseMapper:(JSONKeyMapper *)baseKeyMapper withModelToJSONExceptions:(NSDictionary *)toJSON;
@end