JSONPath for Swift
Somtimes, you don't want to hard-code the mapping from JSON to your model. You may want a generic way to model data from different sources. You may want to be able to update your remote data structure without having to update your app binary.
SwiftPath allows you to keep your data source and your data mapping dynamic.
SwiftPath is available through CocoaPods and Carthage.
pod "SwiftPath"
github "g-mark/SwiftPath" "master"
SwiftPath is a Swift implementation of JSONPath, which allows you to extract a subset of data from a larger chunk of JSON.
You can add a wildcard to objects to convert the current object to an array of it's values:
// Given:
{
"books" : {
"one" : { "title": "one" },
"two" : { "title": "two" },
"three" : { "title": "three" }
}
}
// When using this path:
$.books.*
// Then you'll get this output:
[ { "title": "one" }, { "title": "two" }, { "title": "three" } ]
This is a difference from the JSONPath 'spec'. With SwiftPath, you can re-map property names to match your model in code. When collecting a subset of properties, you can do things like this (renamning the value
property to id
):
// Given:
[
{"name": "one", "value": 1, "extra": true },
{"name": "one", "value": 2, "extra": true },
{"name": "one", "value": 3, "extra": true }
]
// When using this path:
$['name', 'value'=>'id']
// Then you'll get this output:
[
{"name": "one", "id": 1 },
{"name": "one", "id": 2 },
{"name": "one", "id": 3 }
]
To publish a new version of SwiftPath:
SwiftPath.podspec
production
with version: e.g., release/#.#.#
develop
into release/version branchproduction
from release/version branch#.#.#
, merge into production
, and delete the release/version branchpod trunk push SwiftLint.podspec
link |
Stars: 52 |
Last commit: 4 weeks ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics