JSONCopderIO is a implementation of JSONDecoder and JSONEncoder. Is implemented natively in Swift and support the Codable Protocol. I write this Libary for the SwiftIO Board from MadMachineIO. Big thanks for this borad!
Here you can find a good tutorial that explains how to install a library in MadMachien IDE.
quicktype is a amazing tool to generate Codable Struct/Class for your JSON File. Check it out!
import JSONCoderIO
var json = """
{
"Name" : "Kevin",
"Age" : 16,
"Nickname" : "Kivi",
"Single" : true
}
"""
// We need the right Codable Structs https://app.quicktype.io
struct Person: Codable {
let name: String
let age: Int
let nickname: String
let single: Bool
enum CodingKeys: String, CodingKey {
case name = "Name"
case age = "Age"
case nickname = "Nickname"
case single = "Single"
}
}
// Decode String
let decoder = try! JSONDecoderIO(json)
let person = try! Person(from: decoder)
print(person)
//Encode the Struct
let encoder = JSONEncoderIO()
try! person.encode(to: encoder)
let jsonString = try? encoder.getJson()
print(jsonString)
An overview of what has been tested with this library. If you have found errors or formats that I need to test. Let me know.
Libary | Type | Decodable | Encodable |
---|---|---|---|
Swift Standard Library |
Array |
true | true |
Swift Standard Library |
Bool |
true | true |
Swift Standard Library |
Dictionary |
true | true |
Swift Standard Library |
Double |
true | true |
Swift Standard Library |
Float |
true | true |
Swift Standard Library |
Int |
true | true |
Swift Standard Library |
Int8 |
true | true |
Swift Standard Library |
Int16 |
true | true |
Swift Standard Library |
Int32 |
true | true |
Swift Standard Library |
Int64 |
true | true |
Swift Standard Library |
Optional |
true | true |
Swift Standard Library |
Set |
true | true |
Swift Standard Library |
String |
true | true |
Test Missing: