在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:kakaopensource/KakaJSON开源软件地址:https://github.com/kakaopensource/KakaJSON开源编程语言:Swift 99.5%开源软件介绍:KakaJSON
中文教程IntegrationCocoaPodspod 'KakaJSON', '~> 1.1.2' Carthagegithub "kakaopensource/KakaJSON" ~> 1.1.2 Swift Package ManagerTo use Swift Package Manager, you should update to Xcode 11.
Or you can login Xcode with your GitHub account. just search KakaJSON. Usages
Coding// 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 UsageSimple Modelstruct 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 Typeclass 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 inherit from NSObject
class Person: NSObject, Convertible {
var name: String = ""
var age: Int = 0
// must add `override` because NSObject has `init`
required override init() {}
}
let person = json.kj.model(Person.self)
struct Dog: Convertible {
var weight: Double = 0.0
var name: String = ""
// This struct don't need to implement init because compiler generates init for it
}
struct Pig: Convertible {
var weight: Double
var name: String
// This struct need to implement init to initialize all the stored properties.
init() {
name = ""
weight = 0.0
}
} Inheritanceclass Person: Convertible {
var name: String = ""
var age: Int = 0
required init() {}
}
class Student: Person {
var score: Int = 0
var no: String = ""
}
let json: [String: Any] = [
"name": "jack",
"age": 18,
"score": 98,
"no": "9527"
]
let student = json.kj.model(Student.self) letstruct Cat: Convertible {
// let of integer type is very restricted in release mode
// please user `private(set) var` instead of `let`
private(set) var weight: Double = 0.0
let name: String = ""
}
let json = ...
let cat = json.kj.model(Cat.self) NSNullstruct Cat: Convertible {
var weight: Double = 0.0
var name: String = "xx"
var data: NSNull?
}
let json: [String: Any] = [
"name": NSNull(),
"weight": 6.6,
"data": NSNull()
]
let cat = json.kj.model(Cat.self)
// convert failed, keep default value
XCTAssert(cat.name == "xx")
XCTAssert(cat.weight == 6.6)
XCTAssert(cat.data == NSNull()) JSONString// jsonString can alse be NSString, NSMutableString
let jsonString = """
{
"name": "Miaomiao",
"weight": 6.66
}
"""
let cat1 = jsonString.kj.model(Cat.self)
let cat2 = model(from: jsonString, Cat.self)
var type: Convertible.Type = Cat.self
let cat3 = jsonString.kj.model(type: type) as? Cat
let cat4 = model(from: jsonString, type: type) as? Cat JSONData// jsonData can alse be NSData, NSMutableData
let jsonData = """
{
"name": "Miaomiao",
"weight": 6.66
}
""".data(using: .utf8)!
let cat1 = jsonData.kj.model(Cat.self)
let cat2 = model(from: jsonData, Cat.self)
var type: Convertible.Type = Cat.self
let cat3 = jsonData.kj.model(type: type) as? Cat
let cat4 = model(from: jsonData, type: type) as? Cat Nested Model 1// let all the models comform to Convertible
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 = ""
var car: Car?
var books: [Book]?
var dogs: [String: Dog]?
}
let json: [String: Any] = [
"name": "Jack",
"car": ["name": "BMW7", "price": 105.5],
"books": [
["name": "Fast C++", "price": 666.6],
["name": "Data Structure And Algorithm", "price": 1666.6]
],
"dogs": [
"dog0": ["name": "Larry", "age": 5],
"dog1": ["name": "ErHa", "age": 2]
]
]
let person = json.kj.model(Person.self)
XCTAssert(person.car?.name == "BMW7")
XCTAssert(person.books?[1].name == "Data Structure And Algorithm")
XCTAssert(person.dogs?["dog0"]?.name == "Larry") Nested Model 2全部评论
专题导读
上一篇:FairwindsOps/rok8s-scripts: Opinionated scripts for managing application deploym ...发布时间:2022-07-09下一篇:Mirantis/kqueen: Kubernetes queen - cluster manager发布时间:2022-07-09热门推荐
热门话题
阅读排行榜
|
请发表评论