Developers often forget to register resources to resource map. If resource is not registered, deserializer will return nil for missing object (it is wanted behaviour). But, you can turn on debug mode while developing, so Izzy will throw an error if resource is not registered. Don’t enable debug mode for production code because it will cause crashes, it should be used only for debugging purpose - so that you can detect what resources are not registered.
// Article deserialization
do {
let document: Document<Article> = try izzy.deserializeResource(from: data)
let article = document.data
let id = article?.id
} catch let error {
print(error.localizedDescription)
}
Deserializing resource collection:
// Article deserialization
do {
let document: Document<[Article]> = try izzy.deserializeCollection(data)
let articles = document.data
} catch let error {
print(error.localizedDescription)
}
Resource with array of custom objects:
Input JSON:
/**
{
"data": {
"type": "subscription",
"id": "1",
"attributes": {
"title": "Very nice subscription!",
"prices": [{
"type:": "monthly",
"value": "1"
},
{
"type:": "onetime",
"value": "10"
}]
}
}
}
*/
@objcMembers class SubscriptionResource: Resource {
var title: String?
var prices: [Price]?
override class var type: String {
return "subscription"
}
override class var typesForKeys: [String : CustomObject.Type] {
return ["prices": Price.self]
}
}
struct Price: Codable {
var value: String?
var type: String?
}
License
IzzyParser is released under the MIT license. See LICENSE for details.
IzzyParser iOS
IzzyParser is a library for serializing and deserializing JSON API objects.
Installation
Swift Package Manager
Import IzzyParser
Cocoapods
CocoaPods is a dependency manager for Cocoa projects To integrate IzzyParser into your Xcode project using CocoaPods, specify it in your
Podfile
:Then, run the following command:
Usage
Defining resource
All resources MUST inherit
Resource
class.Basic resource:
Resource with
customKeys
for serialization andtypesForKeys
for deserialization:Object with custom deserializer:
Codable custom object:
Registering resources
All resources MUST be registered to
Izzy
instance.Developers often forget to register resources to resource map. If resource is not registered, deserializer will return nil for missing object (it is wanted behaviour). But, you can turn on debug mode while developing, so Izzy will throw an error if resource is not registered. Don’t enable debug mode for production code because it will cause crashes, it should be used only for debugging purpose - so that you can detect what resources are not registered.
Serializing
Serializing single resource:
Output:
Serializing single resource with custom property serialization:
Output:
Serializing resource collection:
Output:
Deserializing
Deserializing single resource:
Input JSON:
Deserializing resource collection:
Resource with array of custom objects:
Input JSON:
License
IzzyParser is released under the MIT license. See LICENSE for details.