car in 場站查車鎖車 ios
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

5 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. # JSONModel - Magical Data Modeling Framework for JSON
  2. JSONModel allows rapid creation of smart data models. You can use it in your
  3. iOS, macOS, watchOS and tvOS apps. Automatic introspection of your model classes
  4. and JSON input drastically reduces the amount of code you have to write.
  5. See [CHANGELOG.md](CHANGELOG.md) for details on changes.
  6. ## Installation
  7. ### CocoaPods
  8. ```ruby
  9. pod 'JSONModel'
  10. ```
  11. ### Carthage
  12. ```ruby
  13. github "jsonmodel/jsonmodel"
  14. ```
  15. ### Manual
  16. 0. download the JSONModel repository
  17. 0. copy the JSONModel sub-folder into your Xcode project
  18. 0. link your app to SystemConfiguration.framework
  19. ## Basic Usage
  20. Consider you have JSON like this:
  21. ```json
  22. { "id": 10, "country": "Germany", "dialCode": 49, "isInEurope": true }
  23. ```
  24. - create a JSONModel subclass for your data model
  25. - declare properties in your header file with the name of the JSON keys:
  26. ```objc
  27. @interface CountryModel : JSONModel
  28. @property (nonatomic) NSInteger id;
  29. @property (nonatomic) NSString *country;
  30. @property (nonatomic) NSString *dialCode;
  31. @property (nonatomic) BOOL isInEurope;
  32. @end
  33. ```
  34. There's no need to do anything in the implementation (`.m`) file.
  35. - initialize your model with data:
  36. ```objc
  37. NSError *error;
  38. CountryModel *country = [[CountryModel alloc] initWithString:myJson error:&error];
  39. ```
  40. If the validation of the JSON passes. you have all the corresponding properties
  41. in your model populated from the JSON. JSONModel will also try to convert as
  42. much data to the types you expect. In the example above it will:
  43. - convert `id` from string (in the JSON) to an `int` for your class
  44. - copy the `country` value
  45. - convert `dialCode` from a number (in the JSON) to an `NSString` value
  46. - copy the `isInEurope` value
  47. All you have to do is define the properties and their expected types.
  48. ## Examples
  49. ### Automatic name based mapping
  50. ```json
  51. {
  52. "id": 123,
  53. "name": "Product name",
  54. "price": 12.95
  55. }
  56. ```
  57. ```objc
  58. @interface ProductModel : JSONModel
  59. @property (nonatomic) NSInteger id;
  60. @property (nonatomic) NSString *name;
  61. @property (nonatomic) float price;
  62. @end
  63. ```
  64. ### Model cascading (models including other models)
  65. ```json
  66. {
  67. "orderId": 104,
  68. "totalPrice": 13.45,
  69. "product": {
  70. "id": 123,
  71. "name": "Product name",
  72. "price": 12.95
  73. }
  74. }
  75. ```
  76. ```objc
  77. @interface ProductModel : JSONModel
  78. @property (nonatomic) NSInteger id;
  79. @property (nonatomic) NSString *name;
  80. @property (nonatomic) float price;
  81. @end
  82. @interface OrderModel : JSONModel
  83. @property (nonatomic) NSInteger orderId;
  84. @property (nonatomic) float totalPrice;
  85. @property (nonatomic) ProductModel *product;
  86. @end
  87. ```
  88. ### Model collections
  89. ```json
  90. {
  91. "orderId": 104,
  92. "totalPrice": 103.45,
  93. "products": [
  94. {
  95. "id": 123,
  96. "name": "Product #1",
  97. "price": 12.95
  98. },
  99. {
  100. "id": 137,
  101. "name": "Product #2",
  102. "price": 82.95
  103. }
  104. ]
  105. }
  106. ```
  107. ```objc
  108. @protocol ProductModel;
  109. @interface ProductModel : JSONModel
  110. @property (nonatomic) NSInteger id;
  111. @property (nonatomic) NSString *name;
  112. @property (nonatomic) float price;
  113. @end
  114. @interface OrderModel : JSONModel
  115. @property (nonatomic) NSInteger orderId;
  116. @property (nonatomic) float totalPrice;
  117. @property (nonatomic) NSArray <ProductModel> *products;
  118. @end
  119. ```
  120. Note: the angle brackets after `NSArray` contain a protocol. This is not the
  121. same as the Objective-C generics system. They are not mutually exclusive, but
  122. for JSONModel to work, the protocol must be in place.
  123. Also property can have generics info for compiler
  124. ```objc
  125. @interface OrderModel : JSONModel
  126. @property (nonatomic) NSInteger orderId;
  127. @property (nonatomic) float totalPrice;
  128. @property (nonatomic) NSArray<ProductModel *> <ProductModel> *products;
  129. @end
  130. ```
  131. ### Nested key mapping
  132. ```json
  133. {
  134. "orderId": 104,
  135. "orderDetails": {
  136. "name": "Product #1",
  137. "price": {
  138. "usd": 12.95
  139. }
  140. }
  141. }
  142. ```
  143. ```objc
  144. @interface OrderModel : JSONModel
  145. @property (nonatomic) NSInteger id;
  146. @property (nonatomic) NSString *productName;
  147. @property (nonatomic) float price;
  148. @end
  149. @implementation OrderModel
  150. + (JSONKeyMapper *)keyMapper
  151. {
  152. return [[JSONKeyMapper alloc] initWithModelToJSONDictionary:@{
  153. @"id": @"orderId",
  154. @"productName": @"orderDetails.name",
  155. @"price": @"orderDetails.price.usd"
  156. }];
  157. }
  158. @end
  159. ```
  160. ### Map automatically to snake_case
  161. ```json
  162. {
  163. "order_id": 104,
  164. "order_product": "Product #1",
  165. "order_price": 12.95
  166. }
  167. ```
  168. ```objc
  169. @interface OrderModel : JSONModel
  170. @property (nonatomic) NSInteger orderId;
  171. @property (nonatomic) NSString *orderProduct;
  172. @property (nonatomic) float orderPrice;
  173. @end
  174. @implementation OrderModel
  175. + (JSONKeyMapper *)keyMapper
  176. {
  177. return [JSONKeyMapper mapperForSnakeCase];
  178. }
  179. @end
  180. ```
  181. ### Optional properties (i.e. can be missing or null)
  182. ```json
  183. {
  184. "id": 123,
  185. "name": null,
  186. "price": 12.95
  187. }
  188. ```
  189. ```objc
  190. @interface ProductModel : JSONModel
  191. @property (nonatomic) NSInteger id;
  192. @property (nonatomic) NSString <Optional> *name;
  193. @property (nonatomic) float price;
  194. @property (nonatomic) NSNumber <Optional> *uuid;
  195. @end
  196. ```
  197. ### Ignored properties (i.e. JSONModel completely ignores them)
  198. ```json
  199. {
  200. "id": 123,
  201. "name": null
  202. }
  203. ```
  204. ```objc
  205. @interface ProductModel : JSONModel
  206. @property (nonatomic) NSInteger id;
  207. @property (nonatomic) NSString <Ignore> *customProperty;
  208. @end
  209. ```
  210. ### Making scalar types optional
  211. ```json
  212. {
  213. "id": null
  214. }
  215. ```
  216. ```objc
  217. @interface ProductModel : JSONModel
  218. @property (nonatomic) NSInteger id;
  219. @end
  220. @implementation ProductModel
  221. + (BOOL)propertyIsOptional:(NSString *)propertyName
  222. {
  223. if ([propertyName isEqualToString:@"id"])
  224. return YES;
  225. return NO;
  226. }
  227. @end
  228. ```
  229. ### Export model to `NSDictionary` or JSON
  230. ```objc
  231. ProductModel *pm = [ProductModel new];
  232. pm.name = @"Some Name";
  233. // convert to dictionary
  234. NSDictionary *dict = [pm toDictionary];
  235. // convert to json
  236. NSString *string = [pm toJSONString];
  237. ```
  238. ### Custom data transformers
  239. ```objc
  240. @interface JSONValueTransformer (CustomTransformer)
  241. @end
  242. @implementation JSONValueTransformer (CustomTransformer)
  243. - (NSDate *)NSDateFromNSString:(NSString *)string
  244. {
  245. NSDateFormatter *formatter = [NSDateFormatter new];
  246. formatter.dateFormat = APIDateFormat;
  247. return [formatter dateFromString:string];
  248. }
  249. - (NSString *)JSONObjectFromNSDate:(NSDate *)date
  250. {
  251. NSDateFormatter *formatter = [NSDateFormatter new];
  252. formatter.dateFormat = APIDateFormat;
  253. return [formatter stringFromDate:date];
  254. }
  255. @end
  256. ```
  257. ### Custom getters/setters
  258. ```objc
  259. @interface ProductModel : JSONModel
  260. @property (nonatomic) NSInteger id;
  261. @property (nonatomic) NSString *name;
  262. @property (nonatomic) float price;
  263. @property (nonatomic) NSLocale *locale;
  264. @end
  265. @implementation ProductModel
  266. - (void)setLocaleWithNSString:(NSString *)string
  267. {
  268. self.locale = [NSLocale localeWithLocaleIdentifier:string];
  269. }
  270. - (void)setLocaleWithNSDictionary:(NSDictionary *)dictionary
  271. {
  272. self.locale = [NSLocale localeWithLocaleIdentifier:dictionary[@"identifier"]];
  273. }
  274. - (NSString *)JSONObjectForLocale
  275. {
  276. return self.locale.localeIdentifier;
  277. }
  278. @end
  279. ```
  280. ### Custom JSON validation
  281. ```objc
  282. @interface ProductModel : JSONModel
  283. @property (nonatomic) NSInteger id;
  284. @property (nonatomic) NSString *name;
  285. @property (nonatomic) float price;
  286. @property (nonatomic) NSLocale *locale;
  287. @property (nonatomic) NSNumber <Ignore> *minNameLength;
  288. @end
  289. @implementation ProductModel
  290. - (BOOL)validate:(NSError **)error
  291. {
  292. if (![super validate:error])
  293. return NO;
  294. if (self.name.length < self.minNameLength.integerValue)
  295. {
  296. *error = [NSError errorWithDomain:@"me.mycompany.com" code:1 userInfo:nil];
  297. return NO;
  298. }
  299. return YES;
  300. }
  301. @end
  302. ```
  303. ## License
  304. MIT licensed - see [LICENSE](LICENSE) file.
  305. ## Contributing
  306. We love pull requests! See [CONTRIBUTING.md](CONTRIBUTING.md) for full details.