From 2b8544b99994c18fe65d635ce4efe72e10c7ee00 Mon Sep 17 00:00:00 2001 From: Lukas Lipka Date: Mon, 10 Apr 2017 22:51:44 +0200 Subject: [PATCH 1/3] Decoding an array should propagate inner error. --- Sources/Decoding.swift | 2 +- Tests/JSONTests/DeserializationTests.swift | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Sources/Decoding.swift b/Sources/Decoding.swift index 0a226ec..258fce0 100644 --- a/Sources/Decoding.swift +++ b/Sources/Decoding.swift @@ -45,7 +45,7 @@ public func decode(_ dictionary: JSONDictionary, key: Str /// - throws: JSONDeserializationError public func decode(_ dictionary: JSONDictionary, key: String) throws -> [T] { let values: [JSONDictionary] = try decode(dictionary, key: key) - return values.flatMap { try? decode($0) } + return try values.map { try decode($0) } } diff --git a/Tests/JSONTests/DeserializationTests.swift b/Tests/JSONTests/DeserializationTests.swift index ce92b03..c952da8 100644 --- a/Tests/JSONTests/DeserializationTests.swift +++ b/Tests/JSONTests/DeserializationTests.swift @@ -60,4 +60,26 @@ final class DeserializationTests: XCTestCase { XCTAssertEqual(blog, try! decode(dictionary)) } + + func testNestedErrorDeserialization() { + let dictionary: JSONDictionary = [ + "title": "My Blog", + "posts": [ + [ + "title": "Next Post", + "author": [ + "name": "Sam Soffes" + ] + ], + [ + "title": 1, + "author": [ + "name": "Sam Soffes" + ] + ] + ] + ] + + XCTAssertThrowsError(try decode(dictionary) as Blog) + } } From 9df601a86bad461de3196abc67896880e137ffc6 Mon Sep 17 00:00:00 2001 From: Lukas Lipka Date: Sun, 1 Jul 2018 20:01:58 +0200 Subject: [PATCH 2/3] Fix Swift 4 warning. --- Sources/JSON/Decoding.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/JSON/Decoding.swift b/Sources/JSON/Decoding.swift index 332994e..10b9c8d 100644 --- a/Sources/JSON/Decoding.swift +++ b/Sources/JSON/Decoding.swift @@ -34,6 +34,6 @@ extension Dictionary where Key : StringProtocol { /// - throws: JSONDeserializationError public func decode(key: Key) throws -> [T] { let values: [JSON] = try decode(key: key) - return try values.flatMap { try T.init(json: $0) } + return try values.compactMap { try T.init(json: $0) } } } From a4070b13aad166bf2bf85ecbfcc730253e633672 Mon Sep 17 00:00:00 2001 From: Lukas Lipka Date: Sun, 1 Jul 2018 20:02:45 +0200 Subject: [PATCH 3/3] Add podspec. --- JSON.podspec | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 JSON.podspec diff --git a/JSON.podspec b/JSON.podspec new file mode 100644 index 0000000..8329901 --- /dev/null +++ b/JSON.podspec @@ -0,0 +1,16 @@ +Pod::Spec.new do |s| + s.name = "JSON" + s.version = "0.1.3" + s.summary = "Micro framework for easily parsing JSON." + s.description = <<-DESC + Micro framework for easily parsing JSON in Swift with rich error messages in less than 100 lines of code. + DESC + s.homepage = "https://github.com/soffes/JSON" + s.license = { :type => "MIT", :file => "LICENSE" } + s.author = { "Sam Soffes" => "sam@soff.es" } + s.ios.deployment_target = "10.0" + s.source = { :git => ".git", :tag => s.version.to_s } + s.source_files = "Sources/**/*" + s.frameworks = "Foundation" + s.swift_version = "4.1" +end