All Projects → alickbass → SwiftyJSONModel

alickbass / SwiftyJSONModel

Licence: MIT license
Better way to use SwiftyJSON with custom models

Programming Languages

swift
15916 projects
ruby
36898 projects - #4 most used programming language
objective c
16641 projects - #2 most used programming language

Projects that are alternatives of or similar to SwiftyJSONModel

Scalacss
Super type-safe CSS for Scala and Scala.JS.
Stars: ✭ 293 (+910.34%)
Mutual labels:  type-safe
Sinai
Type safe state management inspired by Vuex
Stars: ✭ 111 (+282.76%)
Mutual labels:  type-safe
Hydux
A light-weight type-safe Elm-like alternative for Redux ecosystem, inspired by hyperapp and Elmish
Stars: ✭ 216 (+644.83%)
Mutual labels:  type-safe
Gremlin Scala
Scala wrapper for Apache TinkerPop 3 Graph DSL
Stars: ✭ 462 (+1493.1%)
Mutual labels:  type-safe
Lila
♞ lichess.org: the forever free, adless and open source chess server ♞
Stars: ✭ 10,315 (+35468.97%)
Mutual labels:  type-safe
Fable.remoting
Type-safe communication layer (RPC-style) for F# featuring Fable and .NET Apps
Stars: ✭ 175 (+503.45%)
Mutual labels:  type-safe
chain
Extensible, Type Safe Error Handling in Haskell
Stars: ✭ 13 (-55.17%)
Mutual labels:  type-safe
Hypertext
Any-way-you-want-it, type-safe HTML in Swift.
Stars: ✭ 236 (+713.79%)
Mutual labels:  type-safe
Go Structured Query
Type safe SQL query builder and struct mapper for Go
Stars: ✭ 101 (+248.28%)
Mutual labels:  type-safe
C Macro Collections
Easy to use, header only, macro generated, generic and type-safe Data Structures in C
Stars: ✭ 192 (+562.07%)
Mutual labels:  type-safe
Caramel
🍬 a functional language for building type-safe, scalable, and maintainable applications
Stars: ✭ 756 (+2506.9%)
Mutual labels:  type-safe
Univeq
Safer universal equivalence (==) for Scala.
Stars: ✭ 55 (+89.66%)
Mutual labels:  type-safe
Composable Form
Build type-safe composable forms in Elm
Stars: ✭ 179 (+517.24%)
Mutual labels:  type-safe
Structopt
Parse command line arguments by defining a struct
Stars: ✭ 323 (+1013.79%)
Mutual labels:  type-safe
Nstack
Type-safe, composable microservices for data analytics
Stars: ✭ 219 (+655.17%)
Mutual labels:  type-safe
Feliz
A fresh retake of the React API in Fable and a collection of high-quality components to build React applications in F#, optimized for happiness
Stars: ✭ 273 (+841.38%)
Mutual labels:  type-safe
Noticeobservekit
NoticeObserveKit is type-safe NotificationCenter wrapper.
Stars: ✭ 147 (+406.9%)
Mutual labels:  type-safe
Hyper
Type-safe, statically checked composition of HTTP servers
Stars: ✭ 252 (+768.97%)
Mutual labels:  type-safe
Swaydb
Non-blocking persistent & in-memory key-value storage engine for JVM.
Stars: ✭ 221 (+662.07%)
Mutual labels:  type-safe
Swiftdb
A modern database abstraction layer, batteries included.
Stars: ✭ 183 (+531.03%)
Mutual labels:  type-safe

SwiftyJSONModel

A microframework that helps to use Swifty JSON with your custom models in a easy and type-safe way

Carthage compatible Build Status codecov

Motivation:

The full motivation and description is here

For the following JSON:

{
  "name": "John",
  "age": 25,
  "isMarried": false,
  "height": 170.0,
  "address": {
  	"city": "San-Fransisco",
  	"country": "USA"
  },
  "hobbies": ["bouldering", "guitar", "swift:)"]
}

to map to the following model:

struct Person {
    let name: String
    let age: Int
    let isMarried: Bool
    let height: Double
    let city: String
    let country: String
    let hobbies: [String]?
}

Instead of the following:

extension Person {
    init?(jsonDict: [String: Any]) {
        guard let name = jsonDict["name"] as? String,
            let age = jsonDict["age"] as? Int,
            let isMarried = jsonDict["isMarried"] as? Bool,
            let height = jsonDict["height"] as? Double,
            let address = jsonDict["address"] as? [String: Any],
            let city = address["city"] as? String,
            let country = address["country"] as? String
            else {
            return nil
        }
        
        self.name = name
        self.age = age
        self.isMarried = isMarried
        self.height = height
        self.city = city
        self.country = country
        hobbies = jsonDict["hobbies"] as? [String]
    }
}

We get the following:

import SwiftyJSONModel

extension Person: JSONModelType {
    enum PropertyKey: String {
        case name, age, isMarried, height, hobbies
        case address, country, city
    }
    
    init(object: JSONObject<PropertyKey>) throws {
        name = try object.value(for: .name)
        age = try object.value(for: .age)
        isMarried = try object.value(for: .isMarried)
        height = try object.value(for: .height)
        city = try object.value(for: .address, .city) // Accessing nested json
        country = try object.value(for: .address, .country) // Accessing nested json
        hobbies = try object.value(for: .hobbies)
    }
    
    var dictValue: [PropertyKey : JSONRepresentable?] {
        return [
            .name: name,
            .age: age,
            .isMarried: isMarried,
            .height: height,
            .city: city,
            .country: country,
            .hobbies: hobbies?.jsonRepresantable,
        ]
    }
}

What we improved:

  • Keys are now restricted to the PropertyKey enum and we will have a compile error if we try to use something different.
  • Autocomplition will help us navigate through available keys
  • The constructor now throws which means that the init will fail if some value or it's type was different from what we expected and the Error will tell us exactly which property in JSON was wrong
  • The type of the value for key is now inferred from the property we specify. That means we do not need to have all this boilerplate code with casting to String or Int. It will be done for us.

#Integration

CocoaPods (iOS 8+)

You can use CocoaPods to install SwiftyJSONModel by adding it to your Podfile:

platform :ios, '8.0'
use_frameworks!

target 'MyApp' do
pod 'SwiftyJSONModel'
end

Note that this requires CocoaPods version 36, and your iOS deployment target to be at least 8.0:

Carthage (iOS 8+)

You can use Carthage to install SwiftyJSONModel by adding it to your Cartfile:

github "alickbass/SwiftyJSONModel"
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].