# 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
十小大
- 粉丝: 1w+
- 资源: 1529
最新资源
- MATLAB程序:多个无人船 协同围捕控制算法 3船围捕控制,围捕运动船只 可以仿真多个船之间的距离以及距离目标船的距离,特别适合学习、参考
- 树苗自动播种机sw22可编辑全套技术资料100%好用.zip
- FX3U的定位和气缸的FB块,没有密码,可以随便直接调用修改 写法新颖,思路清晰,是敩习FB块很好的范 例
- Java教务信息管理系统(eclipse+MySQL)源码+文档说明
- 6个电池均衡,buckboost电路,精度高,均衡速度快
- 四代磁芯机sw13可编辑全套技术资料100%好用.zip
- 上下料滚筒线sw18可编辑全套技术资料100%好用.zip
- MATLAB环境下一种改进的变分模态分解方法 算法可迁移至金融时间序列,地震 微震信号,机械振动信号,声发射信号,电压 电流信号,语音信号,声信号,生理信号(ECG,EEG,EMG)等信号
- 异步电机矢量控制 foc 转子磁场定向控制 是三套异步foc,可以对应学习,有相应的Word文档说明 控制效果很好
- 塑料链板输送机sw16可编辑全套技术资料100%好用.zip
- 塑胶卡簧装配机sw11可编辑全套技术资料100%好用.zip
- 全方位讲解三菱Q系列QD173H、QD170运动控制器, 是事频,共25个小时的事频讲解,非常详细 需要特殊播放器播放,一机一码,必须电脑本地播放,看清楚再拿哦 Q系列运动控制器是比较高级的内容,专
- Java学生信息管理系统(eclipse+MySQL)源码+文档说明+报告
- 混合动力汽车基于规则的能量管理策略,结果如图,包含CRUISE整车模型,控制策略
- 四柱印刷机sw18可编辑全套技术资料100%好用.zip
- java学生信息管理系统代码+数据库+实验报告
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈