diff --git a/JSON.xcodeproj/JSON_Info.plist b/JSON.xcodeproj/JSON_Info.plist index 67b6034..823cc1e 100644 --- a/JSON.xcodeproj/JSON_Info.plist +++ b/JSON.xcodeproj/JSON_Info.plist @@ -15,11 +15,11 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.16.0 + 0.16.1 CFBundleSignature ???? CFBundleVersion - 0.16.0 + 0.16.1 NSPrincipalClass diff --git a/Sources/JSON/JSONAccessors.swift b/Sources/JSON/JSONAccessors.swift index 1a37b63..fa41a59 100644 --- a/Sources/JSON/JSONAccessors.swift +++ b/Sources/JSON/JSONAccessors.swift @@ -23,9 +23,14 @@ extension JSON { } } - public func get() throws -> [T] { - guard case .array(let array) = self else { throw JSON.Error.badValue(self) } - return try array.map(T.init(json:)) + public func get(`default`: [T]? = nil) throws -> [T] { + do { + guard case .array(let array) = self else { throw JSON.Error.badValue(self) } + return try array.map(T.init(json:)) + } catch { + guard let `default` = `default` else { throw error } + return `default` + } } public func get(`default`: T? = nil) throws -> T @@ -51,11 +56,16 @@ extension JSON { } } - public func get() throws -> [T] + public func get(`default`: [T]? = nil) throws -> [T] where T.RawValue: JSONInitializable { - guard case .array(let array) = self else { throw JSON.Error.badValue(self) } - return try array.map(T.init(json:)) + do { + guard case .array(let array) = self else { throw JSON.Error.badValue(self) } + return try array.map(T.init(json:)) + } catch { + guard let `default` = `default` else { throw error } + return `default` + } } public func get(`default`: T? = nil) throws -> T { @@ -80,9 +90,14 @@ extension JSON { } /// Returns the content matching the type of its destination - public func get() throws -> [T] { - guard case .array(let array) = self else { throw JSON.Error.badValue(self) } - return try array.map(T.init(json:)) + public func get(`default`: [T]? = nil) throws -> [T] { + do { + guard case .array(let array) = self else { throw JSON.Error.badValue(self) } + return try array.map(T.init(json:)) + } catch { + guard let `default` = `default` else { throw error } + return `default` + } } } @@ -120,9 +135,14 @@ extension JSON { } } - public func get(_ field: String) throws -> [T] { - guard let array = self[field].array else { throw JSON.Error.badField(field) } - return try array.map(T.init(json:)) + public func get(_ field: String, `default`: [T]? = nil) throws -> [T] { + do { + guard let array = self[field].array else { throw JSON.Error.badField(field) } + return try array.map(T.init(json:)) + } catch { + guard let `default` = `default` else { throw error } + return `default` + } } /// Returns the content matching the type of its destination @@ -140,11 +160,16 @@ extension JSON { } /// Returns the content matching the type of its destination - public func get(_ field: String) throws -> [T] + public func get(_ field: String, `default`: [T]? = nil) throws -> [T] where T.RawValue: JSONInitializable { - guard let array = self[field].array else { throw JSON.Error.badField(field) } - return try array.map(T.init(json:)) + do { + guard let array = self[field].array else { throw JSON.Error.badField(field) } + return try array.map(T.init(json:)) + } catch { + guard let `default` = `default` else { throw error } + return `default` + } } public func get(_ field: String, `default`: T? = nil) throws -> T? { @@ -170,9 +195,14 @@ extension JSON { } /// Returns the content matching the type of its destination - public func get(_ field: String) throws -> [T] { - guard let array = self[field].array else { throw JSON.Error.badField(field) } - return try array.map(T.init(json:)) + public func get(_ field: String, `default`: [T]? = nil) throws -> [T] { + do { + guard let array = self[field].array else { throw JSON.Error.badField(field) } + return try array.map(T.init(json:)) + } catch { + guard let `default` = `default` else { throw error } + return `default` + } } }