Created
October 13, 2016 02:06
-
-
Save Appletone/b1b31754913acfcd8ba2c322d662cb6f to your computer and use it in GitHub Desktop.
Magical enhanced SwiftyJSON with tree traversal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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