DynamicJSON
Access JSON properties dynamically like JavaScript using Swift 4.2's new @dynamicMemberLookup feature.
DynamicJSON is a dynamically typed parser for JSON 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
After
Installation
- 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 Any
thing into a JSON
object to get started
...or cast a literal as JSON
2. Drill in ⛏
Treat JSON
objects like you're in JavaScript Land
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:
3. Have fun ?
JSON
conforms to Comparable
Pretty print for debug purposes
Convert to raw object
Convert to Data