Arrow 🏹 Parse JSON with style

Overview

Arrow

Arrow

Language: Swift 5 Platform: iOS 8+ SPM compatible Carthage compatible Cocoapods compatible Build Status codebeat badge License: MIT Release version

Reason - Example - Installation

identifier <-- json["id"]
name <-- json["name"]
stats <-- json["stats"]

Because parsing JSON in Swift is full of unecessary if lets, obvious casts and nil-checks
There must be a better way

Try it

Arrow is part of freshOS iOS toolset. Try it in an example App! Download Starter Project

How

By using a simple arrow operator that takes care of the boilerplate code for us.
Json mapping code becomes concise and maintainable ❀️

Why use Arrow

  • Infers types
  • Leaves your models clean
  • Handles custom & nested models
  • Dot and array syntax
  • Pure Swift, Simple & Lightweight

Example

Swift Model

struct Profile {
    var identifier = 0
    var name = ""
    var link:NSURL?
    var weekday:WeekDay = .Monday
    var stats = Stats()
    var phoneNumbers = [PhoneNumber]()
}

JSON File

{
    "id": 15678,
    "name": "John Doe",
    "link": "https://apple.com/steve",
    "weekdayInt" : 3,
    "stats": {
        "numberOfFriends": 163,
        "numberOfFans": 10987
    },
    "phoneNumbers": [{
                     "label": "house",
                     "number": "9809876545"
                     }, {
                     "label": "cell",
                     "number": "0908070656"
                     }, {
                     "label": "work",
                     "number": "0916570656"
    }]
}

Before (Chaos)

var profile = Profile()

// Int
if let id = json["id"] as? Int {
    profile.identifier = id
}  
// String
if let name = json["name"] as? String {
    profile.name = name
}
// NSURL
if let link = json["link"] as? String, url = NSURL(string:link)  {
    profile.link = link
}
// Enum
if let weekdayInt = json["weekdayInt"] as? Int, weekday = WeekDay(rawValue:weekdayInt) {
    profile.weekday = weekday
}
// Custom nested object
if let statsJson = json["stats"] as? AnyObject {
    if let numberOfFans = statsJson["numberOfFans"] as? Int {
        profile.stats.numberOfFans = numberOfFans
    }
    if let numberOfFriends = statsJson["numberOfFriends"] as? Int {
        profile.stats.numberOfFriends = numberOfFriends
    }
}
// Array of custom nested object
if let pns = json["phoneNumbers"] as? [AnyObject] {
    for pn in pns {
        phoneNumbers.append(PhoneNumber(json: pn))
    }
}

After πŸŽ‰ πŸŽ‰ πŸŽ‰

extension Profile:ArrowParsable {
    mutating func deserialize(_ json: JSON) {
        identifier <-- json["id"]
        link <-- json["link"]
        name <-- json["name"]
        weekday <-- json["weekdayInt"]
        stats <- json["stats"]
        phoneNumbers <-- json["phoneNumbers"]
    }
}

Usage

let profile = Profile()
profile.deserialize(json)

Installation

The Swift Package Manager (SPM) is now the official way to install Arrow. The other package managers are now deprecated as of 5.1.2 and won't be supported in future versions.

Swift Package Manager

Xcode > File > Swift Packages > Add Package Dependency... > Paste https://github.com/freshOS/Arrow

Carthage - Deprecated

github "freshOS/Arrow"

CocoaPods - Deprecated

target 'MyApp'
pod 'Arrow'
use_frameworks!

How Does That Work

Notice earlier we typed :

stats <-- json["stats"]

That's because we created and extension "Stats+Arrow.swift" enabling us to use the Arrow Operator

//  Stats+Arrow.swift

import Foundation

extension Stats:ArrowParsable {
    mutating func deserialize(json: JSON) {
        numberOfFriends <-- json["numberOfFriends"]
        numberOfFans <-- json["numberOfFans"]
    }
}

Flexible you said

  • DO I have to use the <-- for my sub models
  • Nope, you could write it like so if you wanted :
stats.numberOfFriends <-- json["stats.numberOfFriends"]
stats.numberOfFans <-- json["stats.numberOfFans"]

Date Parsing

Globally

// Configure Global Date Parsing with one of those
Arrow.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ")
Arrow.setUseTimeIntervalSinceReferenceDate(true)
Arrow.setDateFormatter(aDateFormatter)

// Then later dates can be parsed form custom date format or timestamps automatically πŸŽ‰
let json:JSON = JSON(["date": "2013-06-07T16:38:40+02:00", "timestamp": 392308720])
date1 <-- json["date"]
date2 <-- json["timestamp"]

On a per-key basis

createdAt <-- json["created_at"]?.dateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ")
createdAt <-- json["created_at"]?.dateFormatter(aCustomDateFormatter)

Just provide it on a case per case basis ! πŸŽ‰

Accessing JSON values

Nested values

value <-- json["nested.nested.nested.nestedValue"]

Object at index

value <-- json[12]

Combine both

value <-- json[1]?["someKey"]?[2]?["something.other"]

Looping on Array

if let collection = json.collection {
    for jsonEntry in collection {
        //Do something
    }
}

Swift Version

  • Swift 2 -> version 2.0.3
  • Swift 3 -> version 3.0.5
  • Swift 4 -> version 4.0.0
  • Swift 4.1 -> version 4.1.0
  • Swift 4.2 -> version 4.2.0
  • Swift 5.0 -> version 5.0.0
  • Swift 5.1 -> version 5.1.0
  • Swift 5.1.3 -> version 5.1.1
  • Swift 5.3 -> version 6.0.0

Acknowledgements

This wouldn't exist without YannickDot, Damien-nd and maxkonovalov

Backers

Like the project? Offer coffee or support us with a monthly donation and help us continue our activities :)

Sponsors

Become a sponsor and get your logo on our README on Github with a link to your site :)

Comments
  • Nested

    Nested

    This is not meant to be merged yet. This is a work in progress. Here we experiment using a strong JSON Type over AnyObject.

    This means we have a better control over what we can do on a JSON object and for instance add features on it.

    For example here a subscript has been added so that nested resources can be accessed like so :

    meaningOfLife <-- json["nested.nested.nested.nestedValue"]
    

    We can also access array transparently

    someObject <-- json[12]
    

    This means we can also do things like this :

    value <-- json[1]?["someKey"]?[2]?["something.other"]
    

    Another change is removing the <==fat arrow operator and using <-- everywhere instead

    @maxkonovalov I'd love your feedback on the idea of having a strong JSON type ;)

    opened by s4cha 9
  • <== not working for me

    <== not working for me

    Hi,

    I'm trying to parse my custom models but <== json["customModel"] not working for me!

    Example :

    struct oneModel: ArrowParsable {
    
    var customModel                = CustomMedel()
    
    init() {
    }
    init(json: JSON) {
        customModel <== json["customModel"]
    }
    
    }
    
    
    struct CustomModel: ArrowParsable {
    
    var id                  = Int()
    var title               = String()
    
    init() {
    
    }
    init(json: JSON) {
        id              <-- json["id"]
        title           <-- json["title"]
    }
    
    }
    

    But I can use json.valueForKeyPath without any problems!!!

    opened by Sina-KH 6
  • Array of enums

    Array of enums

    Having trouble of parsing an array of enums.

    var weekdays: [WeekDay]?

    Response:

    {
      "weekdays": [1, 3]
    }
    

    Parse:

    self.weekdays <-- json["weekdays"]
    

    Outcome is nil. :/

    opened by viktorgardart 5
  • Several improvements

    Several improvements

    • Minor bug fix ('collection' property wasn't available because it wasn't marked as 'public').
    • Extended functionality (see 'ArrowInitializable' protocol and '<--/' infix operator).
    • Improved code formatting to make it easy to read.
    • Few minor code cleanups.
    opened by maximkhatskevich 5
  • Doesn't compile with snapshot 4-12

    Doesn't compile with snapshot 4-12

    Hi,

    the newest version of Arrow (2.0.1) doesn't compile with snapshot 4-12.

    β†’ swift build
    Cloning https://github.com/s4cha/Arrow.git
    Resolved version: 2.0.1
    Compiling Swift Module 'Arrow' (2 sources)
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:34:33: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T>(inout left: T, right: JSON?) {
                        ~~~~~       ^
                                    inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:43:33: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T>(inout left: T?, right: JSON?) {
                        ~~~~~       ^
                                    inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:47:30: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    func parseType<T>(inout left:T?,right:JSON?) {
                      ~~~~~      ^
                                 inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:65:51: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T: RawRepresentable>(inout left: T, right: JSON?) {
                                          ~~~~~       ^
                                                      inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:73:51: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T: RawRepresentable>(inout left: T?, right: JSON?) {
                                          ~~~~~       ^
                                                      inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:83:46: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T:ArrowParsable>(inout left:T, right: JSON?) {
                                      ~~~~~      ^
                                                 inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:91:46: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T:ArrowParsable>(inout left:T?, right: JSON?) {
                                      ~~~~~      ^
                                                 inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:101:46: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T:ArrowParsable>(inout left:[T], right: JSON?) {
                                      ~~~~~      ^
                                                 inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:113:46: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T:ArrowParsable>(inout left:[T]?, right: JSON?) {
                                      ~~~~~      ^
                                                 inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:127:30: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- (inout left: NSDate, right: JSON?) {
                     ~~~~~       ^
                                 inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:135:30: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- (inout left: NSDate?, right: JSON?) {
                     ~~~~~       ^
                                 inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:139:27: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    func parseDate(inout left:NSDate?,right:JSON?) {
                   ~~~~~      ^
                              inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:163:30: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- (inout left: NSURL, right: JSON?) {
                     ~~~~~       ^
                                 inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:171:30: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- (inout left: NSURL?, right: JSON?) {
                     ~~~~~       ^
                                 inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:175:26: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    func parseURL(inout left:NSURL?, right:JSON?) {
                  ~~~~~      ^
                             inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:187:32: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    func parseArray<T>(inout left: [T]?, right: JSON?) {
                       ~~~~~       ^
                                   inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:196:33: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T>(inout left: [T], right: JSON?) {
                        ~~~~~       ^
                                    inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:204:33: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T>(inout left: [T]?, right: JSON?) {
                        ~~~~~       ^
                                    inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:66:17: error: 'RawValue' is not a member type of 'T'
        var temp: T.RawValue? = nil
                  ~ ^
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:74:17: error: 'RawValue' is not a member type of 'T'
        var temp: T.RawValue? = nil
                  ~ ^
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:85:17: error: type 'T' has no member 'init'
            var t = T.init()
                    ^ ~~~~
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:93:17: error: type 'T' has no member 'init'
            var t = T.init()
                    ^ ~~~~
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:150:27: error: value of type 'NSDateFormatter' has no member 'dateFromString'
                if let date = dateFormatter.dateFromString(s)  {
                              ^~~~~~~~~~~~~ ~~~~~~~~~~~~~~
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:34:33: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T>(inout left: T, right: JSON?) {
                        ~~~~~       ^
                                    inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:43:33: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T>(inout left: T?, right: JSON?) {
                        ~~~~~       ^
                                    inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:47:30: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    func parseType<T>(inout left:T?,right:JSON?) {
                      ~~~~~      ^
                                 inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:65:51: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T: RawRepresentable>(inout left: T, right: JSON?) {
                                          ~~~~~       ^
                                                      inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:73:51: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T: RawRepresentable>(inout left: T?, right: JSON?) {
                                          ~~~~~       ^
                                                      inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:83:46: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T:ArrowParsable>(inout left:T, right: JSON?) {
                                      ~~~~~      ^
                                                 inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:91:46: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T:ArrowParsable>(inout left:T?, right: JSON?) {
                                      ~~~~~      ^
                                                 inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:101:46: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T:ArrowParsable>(inout left:[T], right: JSON?) {
                                      ~~~~~      ^
                                                 inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:113:46: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T:ArrowParsable>(inout left:[T]?, right: JSON?) {
                                      ~~~~~      ^
                                                 inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:127:30: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- (inout left: NSDate, right: JSON?) {
                     ~~~~~       ^
                                 inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:135:30: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- (inout left: NSDate?, right: JSON?) {
                     ~~~~~       ^
                                 inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:139:27: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    func parseDate(inout left:NSDate?,right:JSON?) {
                   ~~~~~      ^
                              inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:163:30: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- (inout left: NSURL, right: JSON?) {
                     ~~~~~       ^
                                 inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:171:30: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- (inout left: NSURL?, right: JSON?) {
                     ~~~~~       ^
                                 inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:175:26: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    func parseURL(inout left:NSURL?, right:JSON?) {
                  ~~~~~      ^
                             inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:187:32: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    func parseArray<T>(inout left: [T]?, right: JSON?) {
                       ~~~~~       ^
                                   inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:196:33: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T>(inout left: [T], right: JSON?) {
                        ~~~~~       ^
                                    inout
    /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/Packages/Arrow-2.0.1/Source/Arrow.swift:204:33: error: 'inout' before a parameter name is not allowed, place it before the parameter type instead
    public func <-- <T>(inout left: [T]?, right: JSON?) {
                        ~~~~~       ^
                                    inout
    <unknown>:0: error: build had 1 command failures
    error: exit(1): /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-04-12-a.xctoolchain/usr/bin/swift-build-tool -f /Users/aevitas/Desktop/Projects/Swift/Server/league-downloader/.build/debug.yaml default
    

    Is the plan to support Swift3?

    opened by adrianbrink 5
  • Add support for strings parsing

    Add support for strings parsing

    Hey, don't know if this PR is appropriate for the original idea of the lib, but for me it is required to be able to parse numbers represented as strings in JSON to plain numeric types.

    Consider the following JSON:

    { "id": "42" }
    

    The current implementation fails to parse this case:

    int <-- json["id"] // does nothing
    

    I added support for parsing basic numeric types (Int, UInt, Bool, Double, Float, CGFloat and corresponding optionals) from string, so the above no longer fails:

    int <-- json["id"] // = 42
    double <-- json["id"] // = 42.0
    

    Also added the ability to parse NSDate from timestamp (number or string):

    let json = ["timestamp": 392308720]
    date <-- json["timestamp"]
    

    Please feel free to modify this as you see fit :)

    opened by maxkonovalov 5
  • How do I instantiate a model directly from a string containing json?

    How do I instantiate a model directly from a string containing json?

    Hi, sorry to ask such a dumb question, but I can't figure out how to do a simple decode from a string containing my json to my model object:

    struct doh: Codable {
      var homer = ""
    }
    extension doh : ArrowParsable {
      public mutating func deserialize(_ json: JSON) {
        homer <-- json["homer"]
      }
    }
    
    

    now to create one:

    let myJson = "{ \"homer\": \"simpson\"}"
    var aSimpson = doh()
    aSimpson.deserialize(JSON(myJson)!)
    

    this doesn't work! How do I create a doh object from the myJson string?

    Thanks.

    opened by ntkon 4
  • Add array of plain types support

    Add array of plain types support

    Hey, here's another PR πŸ˜€ It makes the following become possible:

    let a: JSON = ["1", "2", "3", "4"]
    var n: [Int]?
    n <-- a
    // n = [1, 2, 3, 4]
    

    Also added some fixes to values parsing here

    opened by maxkonovalov 4
  • Value of type 'JSON' has no member 'collection'

    Value of type 'JSON' has no member 'collection'

    Hi, I'm trying to iterate a JSON like so

    if let collection = json.collection {
            for jsonEntry in collection {
                // things
            }
        }
    

    and it says there is no member 'collection' in json. Checking the source code here shows that there actually is a 'collection'. I tried readding the libraries through Carthage as well but no dice.

    opened by theabhishek2511 2
  • Adds NSURL parsing

    Adds NSURL parsing

    @maxkonovalov since you're a regular contributor I figured I'd make Pull requests as well so that we can discuss things together.

    This transforms

    if let link = json["link"] as? String, url = NSURL(string:link)  {
        profile.link = link
    }
    

    into

    link <-- json["link"]
    

    What do you think ?

    opened by s4cha 2
  • Noinit

    Noinit

    In order to keep things well separated, The init(son:JSON) was supposed to be provided in a Swift extension file.

    • With Structs, it is supposed to work but yells a compiler segfault (weirdly when structs contain optional type)
    • With classes, the init(json:JSON) is required and has to be provided inside our class.

    This clearly violates our desire to separate the JSON parsing logic from the plain swift Object.

    The proposed solution is the following :

    Transform init(son:JSON) into func deserialize(json: JSON), the latter will be mutating for structs and plain for classes.

    Arrow still requires you to have a plain init() method but no longer the JSON version. (It needs to know how to build your models obviously).

    @maxkonovalov I have tested in my project and it seems to work fine, and solves the issue explained above. I would love your feedback on this since it is a breaking change, that will require bumping version number to 1.0.0

    Thanks a lot in advance,

    opened by s4cha 2
  • Doesn't compile on Linux OS

    Doesn't compile on Linux OS

    Using a Docker container to build my app with a reference to Arrow module, I get the following error:

    Dockerfile: FROM swift:latest as builder WORKDIR /app COPY ./Sources Sources/ COPY Package.swift Package.swift RUN swift build -c release CMD ["s"]h

    Error: /app/.build/checkouts/Arrow/Sources/Arrow/Arrow.swift:10:8: error: no such module 'CoreGraphics'

    opened by DeeGeeMan 1
  • netsted values problem

    netsted values problem

    netsted values: value <-- json["user.name"]

    How to deal with the following situation

    {
        "user": {
            "name": "Joannis"
        }
    }
    
    {
        "user.name": "Joannis"
    }
    
    opened by arden 0
  • Cannot cast json to other type

    Cannot cast json to other type

    Hi,

    I'm wondering me to how I can cast the json values!

    createdUser.id = Int32((json["id"]?.data as? Int32)!)

    does not work.

    id <-- Int32((json["id"]?.data as? Int32)!)

    does not work.

    id <--json["id"]

    does not work.

    Each time I get this error:

    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[User setId:]: unrecognized selector sent to instance 0x60400047d100'

    So do you have an idea about that? I'm surprised that nobody got the present error by the past :) My rest service returns id (so Int32 for me on swift) and some dates that I have to parse.

    opened by christophedebatz 3
  • Codable

    Codable

    TODO

    Look for all differences with codable and list them.

    Codable

    • 1 parsing fails all fails
    • Fine grained error handling
    • Need 1 custom parsing need to write all encodable method
    • Default parsing matching existing keys
    • No type inference (apart from dates)
    • Yields a new object
    • Quite verbose

    Arrow

    • Best effort parsing, ignoring fails
    • No error handling (seldom used when getting data from a webservice)
    • No default parsing
    • Type inference out of the box (sy string identifier to an int for example)
    • Can parse and fill an existing object
    • Custom date parsing on a case per case basis
    • Very concise syntax

    Having compared Arrow with Codable in a big iOS App, I still believe there's an advantage using Arrow.

    @maxkonovalov I would love to know your feelings on Codable vs Arrow :)

    opened by s4cha 3
Releases(6.0.0)
Owner
Fresh
Simple iOS tools to solve problems 99% of us have.
Fresh
JSONNeverDie - Auto reflection tool from JSON to Model, user friendly JSON encoder / decoder, aims to never die

JSONNeverDie is an auto reflection tool from JSON to Model, a user friendly JSON encoder / decoder, aims to never die. Also JSONNeverDie is a very important part of Pitaya.

John Lui 454 Oct 30, 2022
JSEN (JSON Swift Enum Notation) is a lightweight enum representation of a JSON, written in Swift.

JSEN /ˈdΚ’eΙͺsΙ™n/ JAY-sΙ™n JSEN (JSON Swift Enum Notation) is a lightweight enum representation of a JSON, written in Swift. A JSON, as defined in the EC

Roger Oba 8 Nov 22, 2022
JSON-Practice - JSON Practice With Swift

JSON Practice Vista creada con: Programmatic + AutoLayout Breve explicaciΓ³n de l

Vanesa Giselle Korbenfeld 0 Oct 29, 2021
Ss-json - High-performance json parsing in swift

json 0.1.1 swift-json is a pure-Swift JSON parsing library designed for high-per

kelvin 43 Dec 15, 2022
Swift-json - High-performance json parsing in swift

json 0.1.4 swift-json is a pure-Swift JSON parsing library designed for high-per

kelvin 43 Dec 15, 2022
Swift parser for JSON Feed β€” a new format similar to RSS and Atom but in JSON.

JSONFeed Swift parser for JSON Feed β€” a new format similar to RSS and Atom but in JSON. For more information about this new feed format visit: https:/

Toto Tvalavadze 31 Nov 22, 2021
AlamofireObjectMappe - An Alamofire extension which converts JSON response data into swift objects using ObjectMapper

AlamofireObjectMapper An extension to Alamofire which automatically converts JSON response data into swift objects using ObjectMapper. Usage Given a U

Tristan Himmelman 2.6k Dec 29, 2022
Functional JSON Parser - Linux Ready

Functional JSON Parser Feature Linux Ready Type-safe JSON parsing Functional value transformation Easy to parse nested value Dependency free No define

Ryo Aoyama 117 Sep 9, 2022
Argo is a library that lets you extract models from JSON or similar structures in a way that's concise, type-safe, and easy to extend

Argo is a library that lets you extract models from JSON or similar structures in a way that's concise, type-safe, and easy to extend. Using Argo

thoughtbot, inc. 3.5k Dec 20, 2022
Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable

Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable. Elevate should no longer be used for

Nike Inc. 611 Oct 23, 2022
Reflection based (Dictionary, CKRecord, NSManagedObject, Realm, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift

EVReflection General information At this moment the master branch is tested with Swift 4.2 and 5.0 beta If you want to continue using EVReflection in

Edwin Vermeer 964 Dec 14, 2022
Freddy - A reusable framework for parsing JSON in Swift.

Why Freddy? Parsing JSON elegantly and safely can be hard, but Freddy is here to help. Freddy is a reusable framework for parsing JSON in Swift. It ha

Big Nerd Ranch 1.1k Jan 1, 2023
[Deprecated] A shiny JSON parsing library in Swift :sparkles: Loved by many from 2015-2021

?? Deprecation Notice ?? Gloss has been deprecated in favor of Swift's Codable framework. The existing Gloss source is not going away, however updates

Harlan Kellaway 1.6k Nov 24, 2022
HandyJSON is a framework written in Swift which to make converting model objects to and from JSON easy on iOS.

HandyJSON To deal with crash on iOS 14 beta4 please try version 5.0.3-beta HandyJSON is a framework written in Swift which to make converting model ob

Alibaba 4.1k Dec 29, 2022
Himotoki (紐解き) is a type-safe JSON decoding library written purely in Swift.

Himotoki Himotoki (紐解き) is a type-safe JSON decoding library written purely in Swift. This library is highly inspired by the popular Swift JSON parsin

IKEDA Sho 799 Dec 6, 2022
JASON is a faster JSON deserializer written in Swift.

JASON is a faster JSON deserializer written in Swift. JASON is the best framework we found to manage JSON at Swapcard. This is by far the fastest and

Damien 1k Oct 15, 2022
JSONHelper - ✌ Convert anything into anything in one operation; JSON data into class instances, hex strings into UIColor/NSColor, y/n strings to booleans, arrays and dictionaries of these; anything you can make sense of!

JSONHelper Convert anything into anything in one operation; hex strings into UIColor/NSColor, JSON strings into class instances, y/n strings to boolea

Baris Sencan 788 Jul 19, 2022
ObjectMapper is a framework written in Swift that makes it easy for you to convert your model objects to and from JSON.

ObjectMapper is a framework written in Swift that makes it easy for you to convert your model objects (classes and structs) to and from J

Tristan Himmelman 9k Jan 2, 2023
PMJSON provides a pure-Swift strongly-typed JSON encoder/decoder

Now Archived and Forked PMJSON will not be maintained in this repository going forward. Please use, create issues on, and make PRs to the fork of PMJS

Postmates Inc. 364 Sep 28, 2022