Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork28
freshOS/Arrow
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
identifier<--json["id"]name<--json["name"]stats<--json["stats"]
Because parsing JSON in Swift is full ofunecessary if lets, obvious casts and nil-checks
There must be a better way
Arrow is part offreshOS iOS toolset. Try it in an example App!Download Starter Project
By using a simple arrow operator that takes care of the boilerplate code for us.
Json mapping code becomesconcise andmaintainable ❤️
- Infers types
- Leaves your models clean
- Handles custom & nested models
- Dot and array syntax
- Pure Swift, Simple & Lightweight
structProfile{varidentifier=0varname=""varlink:NSURL?varweekday:WeekDay=.Mondayvarstats=Stats()varphoneNumbers=[PhoneNumber]()}
{"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" }]}varprofile=Profile()// Intiflet id=json["id"]as?Int{ profile.identifier= id} // Stringiflet name=json["name"]as?String{ profile.name= name}// NSURLiflet link=json["link"]as?String, url=NSURL(string:link){ profile.link= link}// Enumif let weekdayInt=json["weekdayInt"]as?Int, weekday=WeekDay(rawValue:weekdayInt){ profile.weekday= weekday}// Custom nested objectif let statsJson=json["stats"]as?AnyObject{iflet numberOfFans=statsJson["numberOfFans"]as?Int{ profile.stats.numberOfFans= numberOfFans}iflet numberOfFriends=statsJson["numberOfFriends"]as?Int{ profile.stats.numberOfFriends= numberOfFriends}}// Array of custom nested objectif let pns=json["phoneNumbers"]as?[AnyObject]{forpnin pns{ phoneNumbers.append(PhoneNumber(json: pn))}}
extensionProfile:ArrowParsable{mutatingfunc deserialize(_ json:JSON){ identifier<--json["id"] link<--json["link"] name<--json["name"] weekday<--json["weekdayInt"] stats<-json["stats"] phoneNumbers<--json["phoneNumbers"]}}
letprofile=Profile()profile.deserialize(json)
The Swift Package Manager (SPM) is now the official way to installArrow. The other package managers are now deprecated as of5.1.2 and won't be supported in future versions.
Xcode >File >Swift Packages >Add Package Dependency... >Pastehttps://github.com/freshOS/Arrow
github "freshOS/Arrow"target 'MyApp'pod 'Arrow'use_frameworks!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.swiftimport FoundationextensionStats:ArrowParsable{mutatingfunc deserialize(json:JSON){ numberOfFriends<--json["numberOfFriends"] numberOfFans<--json["numberOfFans"]}}
- 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"]
// Configure Global Date Parsing with one of thoseArrow.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 🎉letjson:JSON=JSON(["date":"2013-06-07T16:38:40+02:00","timestamp":392308720])date1<--json["date"]date2<--json["timestamp"]
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 ! 🎉
value<--json["nested.nested.nested.nestedValue"]
value<--json[12]
value<--json[1]?["someKey"]?[2]?["something.other"]
iflet collection= json.collection{forjsonEntryin collection{ //Do something}}
- Swift 2 -> version2.0.3
- Swift 3 -> version3.0.5
- Swift 4 -> version4.0.0
- Swift 4.1 -> version4.1.0
- Swift 4.2 -> version4.2.0
- Swift 5.0 -> version5.0.0
- Swift 5.1 -> version5.1.0
- Swift 5.1.3 -> version5.1.1
- Swift 5.3 -> version6.0.0
This wouldn't exist withoutYannickDot,Damien-nd andmaxkonovalov
Like the project? Offer coffee or support us with a monthly donation and help us continue our activities :)
Become a sponsor and get your logo on our README on Github with a link to your site :)
About
🏹 Parse JSON with style
Topics
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors8
Uh oh!
There was an error while loading.Please reload this page.
