All Projects → undabot → izzyparser-ios

undabot / izzyparser-ios

Licence: MIT license
IzzyParser is an iOS library for serializing and deserializing JSON:API objects

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to izzyparser-ios

Netclient Ios
Versatile HTTP Networking in Swift
Stars: ✭ 117 (+515.79%)
Mutual labels:  swift-package-manager, request, response
Alamofire
Elegant HTTP Networking in Swift
Stars: ✭ 36,896 (+194089.47%)
Mutual labels:  swift-package-manager, request, response
Guzzlette
🌀 Guzzle integration into Nette Framework (@nette)
Stars: ✭ 19 (+0%)
Mutual labels:  request, response
Alagarr
🦍 Alagarr is a request-response helper library that removes the boilerplate from your Node.js (AWS Lambda) serverless functions and helps make your code portable.
Stars: ✭ 58 (+205.26%)
Mutual labels:  request, response
Holen
Declarative fetch for React
Stars: ✭ 152 (+700%)
Mutual labels:  request, response
Swiftyjson
The better way to deal with JSON data in Swift.
Stars: ✭ 21,042 (+110647.37%)
Mutual labels:  request, response
Fast Cgi Client
A PHP fast CGI client for sending requests (a)synchronously to PHP-FPM
Stars: ✭ 478 (+2415.79%)
Mutual labels:  request, response
Kitura Net
Kitura networking
Stars: ✭ 98 (+415.79%)
Mutual labels:  request, response
psr7-http-message
💫 PSR #7 [HTTP Message Interface] to Nette Framework (@nette)
Stars: ✭ 17 (-10.53%)
Mutual labels:  request, response
reqres
Powerful classes for http requests and responses
Stars: ✭ 36 (+89.47%)
Mutual labels:  request, response
Wiremock.net
WireMock.Net is a flexible library for stubbing and mocking web HTTP responses using request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Stars: ✭ 408 (+2047.37%)
Mutual labels:  request, response
WaterPipe
URL routing framework, requests/responses handler, and HTTP client for PHP
Stars: ✭ 24 (+26.32%)
Mutual labels:  request, response
Netfox
A lightweight, one line setup, iOS / OSX network debugging library! 🦊
Stars: ✭ 3,188 (+16678.95%)
Mutual labels:  request, response
Ngx Restangular
Restangular for Angular 2 and higher versions
Stars: ✭ 787 (+4042.11%)
Mutual labels:  request, response
Cors
🔮Supported(Laravel/Lumen/PSR-15/Swoft/Slim/ThinkPHP) - PHP CORS (Cross-origin resource sharing) middleware.
Stars: ✭ 266 (+1300%)
Mutual labels:  request, response
Aura.http
HTTP Request and Response tools
Stars: ✭ 69 (+263.16%)
Mutual labels:  request, response
httpmate
Non-invasive, flexible and ultra-extendable http framework that offers you 3 modes of handling http requests - UseCase driven, low-level http and event-driven request handling, as well as a mix of those modes
Stars: ✭ 15 (-21.05%)
Mutual labels:  request, response
mock-req-res
Extensible mock req / res objects for use in unit tests of Express controller and middleware functions.
Stars: ✭ 39 (+105.26%)
Mutual labels:  request, response
Examples
Examples of Mock Service Worker usage with various frameworks and libraries.
Stars: ✭ 163 (+757.89%)
Mutual labels:  request, response
chronosjs
JS Channels (Events / Commands / Reqest-Response / Courier) Mechanism
Stars: ✭ 35 (+84.21%)
Mutual labels:  request, response

IzzyParser iOS

IzzyParser is a library for serializing and deserializing JSON API objects.

Test codecov Platform Pod version SPM Top language License

Installation

Swift Package Manager

  1. Using Xcode 11 or higher go to File > Swift Packages > Add Package Dependency
  2. Paste the project URL: https://github.com/undabot/izzyparser-ios.git
  3. Click on next, select the project target and click finish
  4. 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:

target '<Your Target Name>' do
    pod 'IzzyParser'
end

Then, run the following command:

$ pod install

Usage

Defining resource

All resources MUST inherit Resource class.

Basic resource:

class Author: Resource {
    
    override class var type: String {
        return "people"
    }
    
    required init(id: String) {
        super.init(id: id)
    }
}

Resource with customKeys for serialization and typesForKeys for deserialization:

class Article: Resource {
    @objc var title: String = ""
    @objc var author: Author?
    @objc var comment: Comment?
    
    override class var type: String {
        return "articles"
    }
    
    // Custom coding keys
    override public class var customKeys: [String: String] {
        return ["komentar": "comment"]
    }
    
    override public class var typesForKeys: [String: CustomObject.Type] {
        return ["komentar": Comment.self]
    }
    
    init(id: String, title: String, author: Author, comment: Comment? = nil) {
        self.title = title
        self.author = author
        self.comment = comment
        super.init(id: id)
    }
    
    required init(id: String) {
        super.init(id: id)
    }
}

Object with custom deserializer:

class Comment: NSObject, CustomObject {
    @objc var id: String
    @objc var content: String
    required init(objectJson: [String: Any]) {
        self.id = objectJson["id"] as? String ?? ""
        self.content = objectJson["content"] as? String ?? ""
    }
}

Codable custom object:

class CodableComment: NSObject, CodableCustomObject {
    var id: String?
    var content: String?
}

Registering resources

All resources MUST be registered to Izzy instance.

// Creating resource map
let resourceMap: [Resource.Type] = [
    Article.self,
    Author.self
]

// Registering resources
let izzy = Izzy()
izzy.registerResources(resources: resourceMap)

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.

// Registering resources
let izzy = Izzy()
izzy.isDebugModeOn = true
izzy.registerResources(resources: resourceMap)

Serializing

Serializing single resource:

// Article serialization
let author = Author(id: "authorID")
let article = Article(id: "articleID", title: "Article title", author: author)

let serializedArticle = izzy.serialize(resource: article)

Output:

// Serialized article JSON:
/**
	{
	  "data" : {
	    "attributes" : {
	      "title" : "Article title"
	    },
	    "id" : "articleID",
	    "type" : "articles",
	    "relationships" : {
	      "author" : {
	        "data" : {
	          "id" : "authorID",
	          "type" : "people"
	        }
	      }
	    }
	  }
	}
*/

Serializing single resource with custom property serialization:

// Article serialization with custom author serialization
let customRelationshipDict: [String: Any] = [
    "name": "John",
    "surname": "Smith",
    "age": NSNull()
]

serializedArticle = izzy.serializeCustom(resource: article, relationshipKey: "author", relationshipValue: customRelationshipDict)

Output:

// Serialized article JSON:
/**
	{
	  "data" : {
	    "attributes" : {
	      "title" : "Article title"
	    },
	    "id" : "articleID",
	    "type" : "articles",
	    "relationships" : {
	      "author" : {
	        "name" : "John",
	        "surname" : "Smith",
	        "age" : null
	      }
	    }
	  }
	}
*/

Serializing resource collection:

let author = Author(id: "authorID")
let article = Article(id: "articleID", title: "Article title", author: author)

let serializedResourceCollection = izzy.serialize(resourceCollection: [article, article])

Output:

// Serialized resource collection:
 /**
 	{
	  "data" : [
	    {
	      "attributes" : {
	        "title" : "Article title"
	      },
	      "id" : "articleID",
	      "type" : "articles",
	      "relationships" : {
	        "author" : {
	          "data" : {
	            "id" : "authorID",
	            "type" : "people"
	          }
	        }
	      }
	    },
	    {
	      "attributes" : {
	        "title" : "Article title"
	      },
	      "id" : "articleID",
	      "type" : "articles",
	      "relationships" : {
	        "author" : {
	          "data" : {
	            "id" : "authorID",
	            "type" : "people"
	          }
	        }
	      }
	    }
	  ]
	}
 */

Deserializing

Deserializing single resource:

Input JSON:

// JSON data:
/**
	{
    "data": {
        "type": "articles",
        "id": "1",
        "attributes": {
            "title": "Rails is Omakase",
            "komentar": {
                "id": "11",
                "content": "Custom content"
            }

        },
        "relationships": {
            "author": {
                "links": {
                    "self": "/articles/1/relationships/author",
                    "related": "/articles/1/author"
                },
                "data": { "type": "people", "id": "9" }
            }
        }
    }
}
*/
// 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].