# KakaJSON
[![pod](https://img.shields.io/cocoapods/v/KakaJSON.svg)](https://github.com/CocoaPods/CocoaPods) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) [![SwiftPM compatible](https://img.shields.io/badge/SwiftPM-Compatible-brightgreen.svg)](https://swift.org/package-manager/)
> Fast conversion between JSON and model in Swift.
- Convert model to JSON with one line of code.(一行代码Model转JSON)
- Convert JSON to Model with one line of code.(一行代码JSON转Model)
- Archive\Unarchive object with one line of code.(一行代码实现常见数据的归档\解档)
## 中文教程
- [KakaJSON手册](https://www.cnblogs.com/mjios/p/11352776.html)
## Integration
### CocoaPods
```ruby
pod 'KakaJSON', '~> 1.1.2'
```
### Carthage
```ruby
github "kakaopensource/KakaJSON" ~> 1.1.2
```
### Swift Package Manager
To use Swift Package Manager, you should update to Xcode 11.
* Open your project.
* Click File tab
* Select Swift Packages
* Add Package Dependency, enter [KakaJSON repo's URL](https://github.com/kakaopensource/KakaJSON.git)
Or you can login Xcode with your GitHub account. just search **KakaJSON**.
## Usages
- [Coding](#coding)
- [JSON To Model_01_Basic Usage](#json-to-model_01_basic-usage)
- [Simple Model](#simple-model)
- [Class Type](#class-type)
- [Inheritance](#inheritance)
- [let](#let)
- [NSNull](#nsnull)
- [JSONString](#jsonstring)
- [JSONData](#jsondata)
- [Nested Model 1](#nested-model-1)
- [Nested Model 2](#nested-model-2)
- [Nested Model 3](#nested-model-3)
- [Recursive](#recursive)
- [Generic](#generic)
- [Model Array](#model-array)
- [Model Array In Dictionary](#model-array-in-dictionary)
- [Convert](#convert)
- [Listen](#listen)
- [JSON To Model_02_Data Type](#json-to-model_02_data-type)
- [Int](#int)
- [Float](#float)
- [Double](#double)
- [CGFloat](#cgfloat)
- [Bool](#bool)
- [String](#string)
- [Decimal](#decimal)
- [NSDecimalNumber](#nsdecimalnumber)
- [NSNumber](#nsnumber)
- [Optional](#optional)
- [URL](#url)
- [Data](#data)
- [Date](#date)
- [Enum](#enum)
- [Enum In Array](#enum-in-array)
- [Enum In Dictionary](#enum-in-dictionary)
- [Enum Array In Dictionary](#enum-array-in-dictionary)
- [Array](#array)
- [Set](#set)
- [Dictionary](#dictionary)
- [JSON To Model_03_Key Mapping](#json-to-model_03_key-mapping)
- [Basic Usage](#basic-usage)
- [Camel -> Underline](#camel---underline)
- [Underline -> Camel](#underline---camel)
- [Inheritance](#inheritance-1)
- [Override 1](#override-1)
- [Override 2](#override-2)
- [Global Config](#global-config)
- [Local Config](#local-config)
- [Config Example 1](#config-example-1)
- [Config Example 2](#config-example-2)
- [Complex](#complex)
- [JSON To Model_04_Custom Value](#json-to-model_04_custom-value)
- [Date](#date-1)
- [Unspecific Type](#unspecific-type)
- [Example](#example)
- [Other Ways](#other-ways)
- [JSON To Model_05_Dynamic Model](#json-to-model_05_dynamic-model)
- [Model To JSON](#model-to-json)
- [JSON and JSONString](#json-and-jsonstring)
- [Optional](#optional-1)
- [Enum](#enum-1)
- [Nested Model](#nested-model)
- [Any](#any)
- [Model Array](#model-array-1)
- [Model Set](#model-set)
- [Key Mapping](#key-mapping-1)
- [Custom Value](#custom-value-1)
- [Listen](#listen)
## Coding
```swift
// file path (can be String or URL)
let file = "/Users/mj/Desktop/test.data"
/****************** String ******************/
let string1 = "123"
// wrtite String to file
write(string1, to: file)
// read String from file
let string2 = read(String.self, from: file)
XCTAssert(string2 == string1)
// read Int from file
XCTAssert(read(Int.self, from: file) == 123)
/****************** Date ******************/
let date1 = Date(timeIntervalSince1970: 1565922866)
// wrtite Date to file
write(date1, to: file)
// read Date from file
let date2 = read(Date.self, from: file)
XCTAssert(date2 == date1)
// read Int from file
XCTAssert(read(Int.self, from: file) == 1565922866)
/****************** Array ******************/
let array1 = ["Jack", "Rose"]
// wrtite [String] to file
write(array1, to: file)
// read [String] from file
let array2 = read([String].self, from: file)
XCTAssert(array2 == array1)
// Also support Set\Dictionary
/****************** Model ******************/
struct Book: Convertible {
var name: String = ""
var price: Double = 0.0
}
struct Car: Convertible {
var name: String = ""
var price: Double = 0.0
}
struct Dog: Convertible {
var name: String = ""
var age: Int = 0
}
struct Person: Convertible {
var name: String = "Jack"
var car: Car? = Car(name: "Bently", price: 106.666)
var books: [Book]? = [
Book(name: "Fast C++", price: 666.6),
Book(name: "Data Structure And Algorithm", price: 666.6),
]
var dogs: [String: Dog]? = [
"dog0": Dog(name: "Wang", age: 5),
"dog1": Dog(name: "ErHa", age: 3),
]
}
// wrtite Person to file
write(Person(), to: file)
// read Person from file
let person = read(Person.self, from: file)
XCTAssert(person?.name == "Jack")
XCTAssert(person?.car?.name == "Bently")
XCTAssert(person?.car?.price == 106.666)
XCTAssert(person?.books?.count == 2)
XCTAssert(person?.dogs?.count == 2)
/****************** Model Array ******************/
struct Car: Convertible {
var name: String = ""
var price: Double = 0.0
}
let models1 = [
Car(name: "BMW", price: 100.0),
Car(name: "Audi", price: 70.0)
]
// wrtite [Car] to file
write(models1, to: file)
// read [Car] from file
let models2 = read([Car].self, from: file)
XCTAssert(models2?.count == models1.count)
XCTAssert(models2?[0].name == "BMW")
XCTAssert(models2?[0].price == 100.0)
XCTAssert(models2?[1].name == "Audi")
XCTAssert(models2?[1].price == 70.0)
/****************** Model Set ******************/
struct Car: Convertible, Hashable {
var name: String = ""
var price: Double = 0.0
}
let models1: Set<Car> = [
Car(name: "BMW", price: 100.0),
Car(name: "Audi", price: 70.0)
]
// wrtite Set<Car> to file
write(models1, to: file)
// read Set<Car> from file
let models2 = read(Set<Car>.self, from: file)!
XCTAssert(models2.count == models1.count)
for car in models2 {
XCTAssert(["BMW", "Audi"].contains(car.name))
XCTAssert([100.0, 70.0].contains(car.price))
}
/****************** Model Dictionary ******************/
struct Car: Convertible {
var name: String = ""
var price: Double = 0.0
}
let models1 = [
"car0": Car(name: "BMW", price: 100.0),
"car1": Car(name: "Audi", price: 70.0)
]
// wrtite [String: Car] to file
write(models1, to: file)
// read [String: Car] from file
let models2 = read([String: Car].self, from: file)
XCTAssert(models2?.count == models1.count)
let car0 = models2?["car0"]
XCTAssert(car0?.name == "BMW")
XCTAssert(car0?.price == 100.0)
let car1 = models2?["car1"]
XCTAssert(car1?.name == "Audi")
XCTAssert(car1?.price == 70.0)
```
## JSON To Model_01_Basic Usage
### Simple Model
```swift
struct Cat: Convertible {
var name: String = ""
var weight: Double = 0.0
}
// json can also be NSDictionary, NSMutableDictionary
let json: [String: Any] = [
"name": "Miaomiao",
"weight": 6.66
]
let cat1 = json.kj.model(Cat.self)
XCTAssert(cat1.name == "Miaomiao")
XCTAssert(cat1.weight == 6.66)
// you can call global function `model`
let cat2 = model(from: json, Cat.self)
// support type variable
var type: Convertible.Type = Cat.self
let cat3 = json.kj.model(type: type) as? Cat
let cat4 = model(from: json, type: type) as? Cat
```
### Class Type
```swift
class Cat: Convertible {
var weight: Double = 0.0
var name: String = ""
// The protocol `Convertible` required an init constructor
// for initializing an instance completely.
required init() {}
}
let json = ...
let cat = json.kj.model(Cat.self)
// a class inh
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
iOS开发工具小集合,该框架集成了一个APP该有的开发框架。 安装: pod 'PooTools/Core', :git => 'https://github.com/crazypoo/PTools.git'
资源推荐
资源详情
资源评论
收起资源包目录
iOS开发工具小集合,该框架集成了一个APP该有的开发框架 (2000个子文件)
mz_zip.c 101KB
mz_zip_rw.c 61KB
mz_compat.c 27KB
mz_strm.c 16KB
mz_crypt_apple.c 13KB
mz_strm_buf.c 13KB
mz_strm_split.c 13KB
mz_strm_wzaes.c 11KB
flex_fishhook.c 11KB
mz_strm_pkcrypt.c 11KB
mz_strm_zlib.c 10KB
mz_os.c 9KB
mz_os_posix.c 9KB
mz_crypt.c 7KB
mz_strm_mem.c 7KB
mz_strm_os_posix.c 5KB
TransformationMatrix.cpp 37KB
bootstrap.css 118KB
bootstrap-theme.css 15KB
index.css 3KB
jquery.fileupload.css 655B
glyphicons-halflings-regular.eot 20KB
.gitignore 98B
NSAttributedString+YYText.h 58KB
DDLog.h 35KB
GCDWebServer.h 23KB
DDFileLogger.h 22KB
YYTextLayout.h 21KB
ZXNavigationBarController.h 19KB
ZXNavigationBarTableViewController.h 19KB
YYTextUtilities.h 18KB
YYTextView.h 17KB
YYTextAttribute.h 15KB
YYDiskCache.h 15KB
LookinAttrIdentifiers.h 15KB
IQKeyboardManager.h 14KB
YYLabel.h 14KB
MBProgressHUD.h 13KB
NSObject+YYModel.h 13KB
POPVector.h 13KB
NSObject+FLEX_Reflection.h 13KB
POPAnimationInternal.h 12KB
mz_zip_rw.h 12KB
TransformationMatrix.h 11KB
YYKVStorage.h 11KB
BRDatePickerView.h 11KB
mz_zip.h 11KB
UIScrollView+EmptyDataSet.h 11KB
mz_compat.h 10KB
PMacros.h 9KB
GCDWebServerPrivate.h 9KB
BRPickerStyle.h 9KB
IQUIView+IQKeyboardToolbar.h 9KB
POPPropertyAnimationInternal.h 9KB
DDDispatchQueueLogFormatter.h 9KB
SSZipArchive.h 8KB
YYClassInfo.h 8KB
LookinDisplayItem.h 8KB
POPAnimatableProperty.h 8KB
MASConstraint.h 8KB
mz.h 8KB
YYCache.h 7KB
BRStringPickerView.h 7KB
DDTTYLogger.h 7KB
GCDWebServerRequest.h 7KB
POPAnimation.h 7KB
YYMemoryCache.h 7KB
ActivityStreamAPI.h 7KB
GCDWebServerResponse.h 7KB
FLEXNetworkTransaction.h 7KB
FLEXProperty.h 7KB
FLEXTableViewSection.h 7KB
FLEXTableViewController.h 7KB
Firestore.h 6KB
LookinDefines.h 6KB
FLEXShortcutsSection.h 6KB
FLEXKeychain.h 6KB
GCDWebUploader.h 6KB
GCDWebServerConnection.h 6KB
MJRefreshComponent.h 6KB
MASUtilities.h 6KB
IQKeyboardManagerConstants.h 6KB
FLEXNetworkRecorder.h 6KB
mz_strm.h 6KB
MASConstraintMaker.h 6KB
DDLogMacros.h 6KB
NSObject+MJKeyValue.h 5KB
mz_os.h 5KB
AMKLaunchTimeProfiler.h 5KB
SVGAPlayerEdition.h 5KB
GCDWebServerHTTPStatusCodes.h 5KB
Lookin_PTChannel.h 5KB
Lookin_PTProtocol.h 5KB
FLEXRuntimeUtility.h 5KB
ZXNavigationBarDefine.h 5KB
View+MASAdditions.h 5KB
NSDate+BRPickerView.h 5KB
IQUIView+Hierarchy.h 5KB
View+MASShorthandAdditions.h 5KB
POPLayerExtras.h 5KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
十小大
- 粉丝: 1w+
- 资源: 1528
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 删除重复字符-Python与Java中实现字符串去重方法详解
- 面向初学者的 Java 教程(包含 500 个代码示例).zip
- 阿里云OSS Java版SDK.zip
- 阿里云api网关请求签名示例(java实现).zip
- 通过示例学习 Android 的 RxJava.zip
- 通过多线程编程在 Java 中发现并发模式和特性 线程、锁、原子等等 .zip
- 通过在终端中进行探索来学习 JavaScript .zip
- 通过不仅针对初学者而且针对 JavaScript 爱好者(无论他们的专业水平如何)设计的编码挑战,自然而自信地拥抱 JavaScript .zip
- 适用于 Kotlin 和 Java 的现代 JSON 库 .zip
- AppPay-安卓开发资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功