GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com β€” GitHub - saoudrizwan/DynamicJSON: Access JSON properties dynamically like JavaScript using Swift 4.2's new @dynamicMemberLookup feature

BranchesTags
Open more actions menu

Latest commit

 

History

46 Commits
46 Commits

Folders and files

NameName
Last commit message
Last commit date
DynamicJSON.xcodeproj
DynamicJSON.xcodeproj
 
 
DynamicJSON.xcworkspace
DynamicJSON.xcworkspace
 
 
Sources
Sources
 
 
Tests
Tests
 
 
.gitignore
.gitignore
 
 
DynamicJSON.podspec
DynamicJSON.podspec
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 

Repository files navigation

DynamicJSON


Platform: iOS, macOS, watchOS, tvOS Language: Swift 4.2 License: MIT

Installation β€’ Usage β€’ License

DynamicJSON is a dynamically typed JSON parser built upon the new @dynamicMemberLookup feature introduced by Chris Lattner in Swift 4.2. This allows us to access arbitrary object members which are resolved at runtime, allowing Swift to be as flexible as JavaScript when it comes to JSON.

Before

if let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
   let user = jsonObject["user"] as? [String: Any],
   let username = user["username"] as? String {
	// ...
}

After

let username = JSON(data).user.username.string

Installation

  • CocoaPods:
pod 'DynamicJSON', '~> 2.0.2'

(if you run into problems, pod repo update and try again)

  • Carthage:
github "saoudrizwan/DynamicJSON"
  • Or drag and drop DynamicJSON.swift into your project.

And import DynamicJSON in the files you'd like to use it.

Usage

1. Initialize 🐣

Throw Anything into a JSON object to get started

let json = JSON(Data())
           JSON(123)
           JSON(["key": "value"])
           JSON(["element", 1])
           JSON("Hello world")
           JSON(false)

...or cast a literal as JSON

let json = "Hello world" as JSON
           123 as JSON
           [1, 2, 3] as JSON

let user: JSON = [
	"username": "Saoud",
	"age": 21,
	"address": [
	    "zip": "12345",
	    "city": "San Diego"
	]
]

2. Drill in ⛏

Treat JSON objects like you're in JavaScript Land

let dictionary = json.dictionary
let array = json[0].cars.array
let string = json.users[1].username.string
let nsnumber = json.creditCard.pin.number
let double = json[3][1].height.double
let int = json[0].age.int
let bool = json.biography.isHuman.bool

Note how JSON doesn't actually have properties like cars or users, instead it uses dynamic member lookup to traverse through its associated JSON data to find the object you're looking for.

In case you have a key that's an actual property of JSON, like number or description for example, just use the string subscript accessor like so:

let number = json.account.contact["number"].number
let description = json.user.biography["description"].string

3. Have fun πŸ€ͺ

JSON conforms to Comparable

let json1 = JSON(jsonData1)
let json2 = JSON(jsonData2)

// Equality applies to all types (Dictionary, Array, String, NSNumber, Bool, NSNull)
let isEqual = json1 == json2

// Less/greater than only applies to NSNumbers (Double, Int) and Strings
let isLessThan = json1 < json2
let isLessThanOrEqual = json1 <= json2
let isGreaterThan = json1 > json2
let isGreaterThanOrEqual = json1 >= json2

Pretty print for debug purposes

print(json)

Convert to raw object

let anyObject = json.object

Convert to Data

let data = json.data() // optionally specify options...

License

DynamicJSON uses the MIT license. Please file an issue if you have any questions or if you'd like to share how you're using DynamicJSON.

Contribute

DynamicJSON is in its infancy, but provides the barebones of a revolutionary new way to work with JSON in Swift. Please feel free to send pull requests of any features you think would add to DynamicJSON and its philosophy.

Questions?

Contact me by email [email protected], or by twitter @sdrzn. Please create an issue if you come across a bug or would like a feature to be added.

Notable Mentions

  • Paul Hudson's wonderful literature over @dynamicMemberLookup
  • Chris Lattner's Swift Evolution Propsal SE-0195
  • SwiftyJSON for giving me a better idea of what the community wants out of a JSON parser
  • Helge Heß for helping remove the need to use optional chaining (before v2.0.0)

About

Access JSON properties dynamically like JavaScript using Swift 4.2's new @dynamicMemberLookup feature

twitter.com/sdrzn/status/1080340307802173440

Topics

iosios-swiftmacosswiftswift4swift4-2tvoswatchos

Resources

Readme
MIT license
Activity

Stars

708 stars

Watchers

21 watching

Forks

23 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402775 β€” GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800Γ—600. No cookies required.