Skip to content

Instantly share code, notes, and snippets.

@Appletone
Created October 13, 2016 02:06
Show Gist options
  • Save Appletone/b1b31754913acfcd8ba2c322d662cb6f to your computer and use it in GitHub Desktop.
Save Appletone/b1b31754913acfcd8ba2c322d662cb6f to your computer and use it in GitHub Desktop.
Magical enhanced SwiftyJSON with tree traversal
// Magical enhanced SwiftyJSON with tree traversal
/*
ex:
let a_b_c = JSONTree(root: demoJSON, keyPath: ["a", "b", "c"])
print(a_b_c.parentNode.parentNode["n"])
*/
struct JSONTree {
var root:JSON
var keyPath:[JSONSubscriptType]
var currentJSON:JSON {
return root[keyPath]
}
var parentNode:JSONTree {
var keyPathCopy = keyPath
keyPathCopy.popLast()
return JSONTree(root: root, keyPath: keyPathCopy)
}
subscript(path: [JSONSubscriptType]) -> JSON {
return currentJSON[path]
}
subscript(path: JSONSubscriptType...) -> JSON {
return currentJSON[path]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment