JUNSON is JSON decode and encode Library for Swift3.0.
// decode json to person object.
let json = JUNSON(data: data)
let lukeSkywalker: Person = json["results"][0].decode()
// by Optional
let darthVader: Person = json["results"][1].asOptional.decode()
// by try-Catch
do {
let leiaOrgana: Person = json["results"][2].asTry.decode()
} catch {
}
JUNSON is simple library for encoding/decoding JSON for Swift.
- type-safe JSON decode.
- make any type decodable by implementing JUNSONDecodable
- make any type encodable by implementing JUNSONEncodable
- handling decode/encode error by try-catch, optional, and replacing with default value(like SwiftyJSON).
First of all, you have to create JUNSON object to access json easily. You can create JUNSON object with various type (like String,NSData,AnyObject).
String
// with String
let rawValue: String = "{\"hoge\":\"hoge\",\"foo\":1,\"bar\":0.12 }"
let junson = JUNSON(string: rawValue)
Data
let rawData: Data = rawValue.data(using: .utf8)!
let junson = JUNSON(data: rawValue)
when you retrieve value from JSON, you do not need specify value type.
class Person {
let name: String = ""
let age: Int = 0
}
let junson = JUNSON(string:string)
let person = Person()
person.name = junson.decode(key:"name")
person.name = junson["age"].decode()
Implementing JUNSONDecodable to your class, or struct,
decoding JSON is more easily.
class Person: JUNSONDecodable {
let name: String = ""
let age: Int = 0
// AnyJUNSON is protocol implemented by JUNSON,OptionalJUNSON and TryJUNSON
static func decode(junson: AnyJUNSON) -> Person? {
let defaultJunson = junson.asDefault
return Person(name: defaultJunson.decode(key: "name"),
age: defaultJunson.decode(key: "age"))
}
}
let junson = JUNSON(string:string)
let person = junson.decode()
To retrieve the value, JUNSON supports various approaches to handle parsing error.
when decode error occured, If you want to replace object by specified value(default value), use JUNSON *when you use JUNSON,please implement JUNSONDefaultValue to your class/struct,and define default value.
class Person: JUNSONDecodable,JUNSONDefaultValue {
static var defaultValue: Person {
return Person(name:"",age:20)
}
static func decode(junson: AnyJUNSON) -> Person? {
..
}
}
let junson = JUNSON(string:string)
let person: Person = junson.asDefault.decode()
- there is potential that object is not exist, use OptionalJUNSON(or JUNSON.asOptional)
let junson = OptionalJUNSON(string:string) // or JUNSON(string;string).asOptional
let person: Person? = junson.asDefault
- If you want to decode JSON more strictly, use TryJUNSON(or JUNSON.asTry)
let junson = TryJUNSON(string:string) // or JUNSON(string;string).asTry
do {
let person: Person = try json.decode()
} catch JUNSONError.hasNoValue(let key) {
// failed decoding value
}
By implementing JUNSONEncodable to your any class/structs, you can encode object to JSON more easily.
class Person: JUNSONEncodable {
let name: String
let age: Int
func encode() -> Any? {
return ["name",name,"age",age]
}
}
var persons = [Person]()
persons.append(Person(name:"Luke Skywalker",age:19))
persons.append(Person(name:"Darth Vader",age:42))
persons.append(Person(name:"Leia Organa",age:19))
let dict = [String:Any]()
dict["count"] = persons.count
dict["persons"] = persons
let data = JUNSON.encode(any:dict)
let string: String = String(data:data,encoding: .utf8)!
/*
{
"count": 3,
"persons": [
{
"age": 19,
"name": "Luke Skywalker"
},
{
"age": 42,
"name": "Darth Vader"
},
{
"age": 19,
"name": "Leia Organa"
}
]
}
*/
Swift 3.0 , Xcode8
JUNSON is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "JUNSON"
Add github SatoshiN21/JUNSON
to your cart file. Execute carthage update to install it.
SatoshiN21, satoshi.nagasaka21@gmail.com
JUNSON is available under the MIT license. See the LICENSE file for more info.