Created JSON.remove(_:) method

This commit is contained in:
Caleb Kleveter 2019-03-27 06:51:24 -05:00
parent 1b7e94e677
commit bb7b4a0ff7
No known key found for this signature in database
GPG Key ID: B38DBD5CF2C98D69
3 changed files with 57 additions and 0 deletions

View File

@ -402,6 +402,52 @@ public enum JSON: Equatable, CustomStringConvertible {
}
}
/// Removes a key/value pair from an object at a given path.
///
/// The `JSON` type converts `nil` to it's `.null` case, so if you try to remove a value like this:
///
/// json["foo", "bar"] = nil
///
/// You just set the object's property to `null`:
///
/// {
/// "foo": {
/// "bar": null
/// }
/// }
///
/// To actually remove a property from an object, you use `.remove(_:)` with the path to the property to remove:
///
/// json.remove(["foo", "bar"])
///
/// Will result in this json structure:
///
/// {
/// "foo": {}
/// }
///
/// - Parameter path: The key path to the json property to remove.
///
/// - Complexity: _O(n)_, where _n_ is the number of elements in the path to remove.
/// Keep in mind that this method is recursive, so each succesive eleemnt in the path will
/// add another call to the stack.
public mutating func remove<Path>(_ path: Path) where Path: Collection, Path.Element == String {
guard path.count > 0 else { return }
if let key = path.first {
guard var object = self.object else { return }
if path.count == 1 {
object[key] = nil
self.object = object
} else {
if var json = object[key] {
json.remove(path.dropFirst())
self[key] = json
}
}
}
}
/// See `CustomStringConvertible.description`.
///
/// This textal representation is compressed, so you might need to prettify it to read it.

View File

@ -166,6 +166,16 @@ class JSONTests: XCTestCase {
}
}
func testRemove() throws {
var json: JSON = ["foo": ["bar": 42]]
json.foo.bar = nil
XCTAssertEqual(json, .object(["foo": .object(["bar": .null])]))
json.remove(["foo", "bar"])
XCTAssertEqual(json, .object(["foo": .object([:])]))
}
func testDescription() {
XCTAssertEqual(JSON.null.description, "null")
XCTAssertEqual(JSON.bool(true).description, "true")

View File

@ -36,6 +36,7 @@ extension JSONTests {
("testDynamicAccessSetSpeed", testDynamicAccessSetSpeed),
("testGet", testGet),
("testInitializers", testInitializers),
("testRemove", testRemove),
("testSet", testSet),
("testSubscript", testSubscript),
]