Skip to content
This repository has been archived by the owner on Sep 19, 2018. It is now read-only.

Reintroduce subscripting ifNull/ifNotFound methods (with deprecation warnings) #149

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions Sources/JSONSubscripting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,105 @@ extension JSON {
}

}

// MARK: - Deprecated methods

extension JSON {

@available(*, deprecated, message="Use 'decode(_:alongPath:type:)' with options '[.MissingKeyBecomesNil]'")
public func decode<Decoded: JSONDecodable>(path: JSONPathType..., ifNotFound: Swift.Bool, type: Decoded.Type = Decoded.self) throws -> Decoded? {
let options: SubscriptingOptions = ifNotFound ? [.MissingKeyBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Decoded.init)
}

@available(*, deprecated, message="Use 'decode(_:alongPath:type:)' with options '[.NullBecomesNil]'")
public func decode<Decoded: JSONDecodable>(path: JSONPathType..., ifNull: Swift.Bool, type: Decoded.Type = Decoded.self) throws -> Decoded? {
let options: SubscriptingOptions = ifNull ? [.NullBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Decoded.init)
}

@available(*, deprecated, message="Use 'double(_:alongPath:)' with options '[.MissingKeyBecomesNil]'")
public func double(path: JSONPathType..., ifNotFound: Swift.Bool) throws -> Swift.Double? {
let options: SubscriptingOptions = ifNotFound ? [.MissingKeyBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Swift.Double.init)
}

@available(*, deprecated, message="Use 'double(_:alongPath:)' with options '[.NullBecomesNil]'")
public func double(path: JSONPathType..., ifNull: Swift.Bool) throws -> Swift.Double? {
let options: SubscriptingOptions = ifNull ? [.NullBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Swift.Double.init)
}

@available(*, deprecated, message="Use 'int(_:alongPath:)' with options '[.MissingKeyBecomesNil]'")
public func int(path: JSONPathType..., ifNotFound: Swift.Bool) throws -> Swift.Int? {
let options: SubscriptingOptions = ifNotFound ? [.MissingKeyBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Swift.Int.init)
}

@available(*, deprecated, message="Use 'int(_:alongPath:)' with options '[.NullBecomesNil]'")
public func int(path: JSONPathType..., ifNull: Swift.Bool) throws -> Swift.Int? {
let options: SubscriptingOptions = ifNull ? [.NullBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Swift.Int.init)
}

@available(*, deprecated, message="Use 'string(_:alongPath:)' with options '[.MissingKeyBecomesNil]'")
public func string(path: JSONPathType..., ifNotFound: Swift.Bool) throws -> Swift.String? {
let options: SubscriptingOptions = ifNotFound ? [.MissingKeyBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Swift.String.init)
}

@available(*, deprecated, message="Use 'string(_:alongPath:)' with options '[.NullBecomesNil]'")
public func string(path: JSONPathType..., ifNull: Swift.Bool) throws -> Swift.String? {
let options: SubscriptingOptions = ifNull ? [.NullBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Swift.String.init)
}

@available(*, deprecated, message="Use 'bool(_:alongPath:)' with options '[.MissingKeyBecomesNil]'")
public func bool(path: JSONPathType..., ifNotFound: Swift.Bool) throws -> Swift.Bool? {
let options: SubscriptingOptions = ifNotFound ? [.MissingKeyBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Swift.Bool.init)
}

@available(*, deprecated, message="Use 'bool(_:alongPath:)' with options '[.NullBecomesNil]'")
public func bool(path: JSONPathType..., ifNull: Swift.Bool) throws -> Swift.Bool? {
let options: SubscriptingOptions = ifNull ? [.NullBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Swift.Bool.init)
}

@available(*, deprecated, message="Use 'array(_:alongPath:)' with options '[.MissingKeyBecomesNil]'")
public func array(path: JSONPathType..., ifNotFound: Swift.Bool) throws -> [JSON]? {
let options: SubscriptingOptions = ifNotFound ? [.MissingKeyBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: JSON.getArray)
}

@available(*, deprecated, message="Use 'array(_:alongPath:)' with options '[.NullBecomesNil]'")
public func array(path: JSONPathType..., ifNull: Swift.Bool) throws -> [JSON]? {
let options: SubscriptingOptions = ifNull ? [.NullBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: JSON.getArray)
}

@available(*, deprecated, message="Use 'arrayOf(_:alongPath:)' with options '[.MissingKeyBecomesNil]'")
public func arrayOf<Decoded: JSONDecodable>(path: JSONPathType..., ifNotFound: Swift.Bool) throws -> [Decoded]? {
let options: SubscriptingOptions = ifNotFound ? [.MissingKeyBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: JSON.getArrayOf)
}

@available(*, deprecated, message="Use 'arrayOf(_:alongPath:)' with options '[.NullBecomesNil]'")
public func arrayOf<Decoded: JSONDecodable>(path: JSONPathType..., ifNull: Swift.Bool) throws -> [Decoded]? {
let options: SubscriptingOptions = ifNull ? [.NullBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: JSON.getArrayOf)
}

@available(*, deprecated, message="Use 'dictionary(_:alongPath:)' with options '[.MissingKeyBecomesNil]'")
public func dictionary(path: JSONPathType..., ifNotFound: Swift.Bool) throws -> [Swift.String: JSON]? {
let options: SubscriptingOptions = ifNotFound ? [.MissingKeyBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: JSON.getDictionary)
}

@available(*, deprecated, message="Use 'dictionary(_:alongPath:)' with options '[.NullBecomesNil]'")
public func dictionary(path: JSONPathType..., ifNull: Swift.Bool) throws -> [Swift.String: JSON]? {
let options: SubscriptingOptions = ifNull ? [.NullBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: JSON.getDictionary)
}

}
2 changes: 1 addition & 1 deletion Tests/JSONSubscriptingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,4 @@ private func testUsage() {
_ = try? j.int(stringConst, 2, alongPath: .MissingKeyBecomesNil)
_ = try? j.int(stringConst, 3, alongPath: .NullBecomesNil)
_ = try? j.int(stringConst, 4, or: 42)
}
}