diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..f4ff993 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,32 @@ +name: CI +on: + push: + branches: [master] + pull_request: {} +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true +jobs: + macos: + runs-on: macos-15 + name: macOS + timeout-minutes: 20 + steps: + - uses: actions/checkout@v4 + - name: Build + run: swift build + - name: Test + run: swift test + linux: + runs-on: ubuntu-latest + name: Linux (Swift ${{ matrix.swift }}) + strategy: + matrix: + swift: ["5.10", "6.1"] + container: swift:${{ matrix.swift }} + steps: + - uses: actions/checkout@v4 + - name: Build + run: swift build + - name: Test + run: swift test diff --git a/.hound.yml b/.hound.yml deleted file mode 100644 index c2b241e..0000000 --- a/.hound.yml +++ /dev/null @@ -1,2 +0,0 @@ -swift: - config_file: .swiftlint.yml \ No newline at end of file diff --git a/.swift-version b/.swift-version deleted file mode 100644 index 35a06be..0000000 --- a/.swift-version +++ /dev/null @@ -1 +0,0 @@ -3.0-GM-CANDIDATE \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 47ceb89..0000000 --- a/.travis.yml +++ /dev/null @@ -1,33 +0,0 @@ -os: - - osx -language: generic -osx_image: xcode8 - -env: - - PLATFORM=Mac - - PLATFORM=iOS NAME='iPhone 6s' - - PLATFORM=tvOS NAME='Apple TV 1080p' - - PLATFORM=watchOS - - PLATFORM=SPM - -before_install: - - if [ -n "$NAME" ]; then - export UUID=$(instruments -s | ruby -e "ARGF.each_line{ |ln| ln =~ /$NAME .* \[(.*)\]/; if \$1; puts(\$1); exit; end }"); - fi - -script: - - set -o pipefail; - case $PLATFORM in - Mac) - xcodebuild -scheme JSONUtilities -enableCodeCoverage YES test | xcpretty;; - iOS|tvOS) - open -a "simulator" --args -CurrentDeviceUDID "$UUID" && xcodebuild -scheme JSONUtilities -destination "id=$UUID" -enableCodeCoverage YES test | xcpretty;; - watchOS) - xcodebuild -scheme JSONUtilities -destination "name=Apple Watch - 38mm" | xcpretty;; - SPM) - swift build && swift test;; - esac - -after_success: - - sleep 5 - - bash <(curl -s https://codecov.io/bash) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64b70e3..c61ec39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Change Log +## Next Version + +- Updated to Swift tools version 5.9, with minimum platforms of macOS 10.15, iOS 13, tvOS 13 and watchOS 6 +- `URL` decoding rejects invalid URL strings again on macOS 14, iOS 17 and later, where `URL(string:)` started percent-encoding them +- `DecodingError` is now `Sendable` +- Replaced Travis CI with GitHub Actions on macOS and Linux + ## 4.0.0 - Decoded typed dictionaries now filter out invalid objects by default instead of failing the whole response. This aligns with the defaults of how arrays are decoded. diff --git a/Package.swift b/Package.swift index 73758e8..0204978 100644 --- a/Package.swift +++ b/Package.swift @@ -1,15 +1,20 @@ -// swift-tools-version:4.2 +// swift-tools-version:5.9 import PackageDescription let package = Package( name: "JSONUtilities", + platforms: [ + .macOS(.v10_15), + .iOS(.v13), + .tvOS(.v13), + .watchOS(.v6), + ], products: [ - .library(name: "JSONUtilities", targets: ["JSONUtilities"]) + .library(name: "JSONUtilities", targets: ["JSONUtilities"]), ], - dependencies: [], targets: [ .target(name: "JSONUtilities"), - .testTarget(name: "JSONUtilitiesTests") + .testTarget(name: "JSONUtilitiesTests", dependencies: ["JSONUtilities"]), ] ) diff --git a/README.md b/README.md index b7ff676..e5b7b60 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,10 @@ # JSONUtilities -[![Build Status](https://travis-ci.org/lucianomarisi/JSONUtilities.svg?branch=master)](https://travis-ci.org/lucianomarisi/JSONUtilities) +[![CI](https://github.com/yonaskolb/JSONUtilities/actions/workflows/ci.yml/badge.svg)](https://github.com/yonaskolb/JSONUtilities/actions/workflows/ci.yml) [![](https://img.shields.io/cocoapods/v/JSONUtilities.svg)](https://cocoapods.org/pods/JSONUtilities) [![](https://img.shields.io/cocoapods/p/JSONUtilities.svg)](https://cocoapods.org/pods/JSONUtilities) -[![codecov.io](http://codecov.io/github/lucianomarisi/JSONUtilities/coverage.svg?branch=master)](http://codecov.io/github/lucianomarisi/JSONUtilities?branch=master) ![Carthage Compatible](https://img.shields.io/badge/carthage-compatible-4BC51D.svg?style=flat) -![Swift Version](https://img.shields.io/badge/swift-3.0-brightgreen.svg) +![Swift Version](https://img.shields.io/badge/swift-5.9-brightgreen.svg) Easily load JSON objects and decode them into structs or classes. The `json(atKeyPath:)` function infers the type from the constant or variable definition to decode meaning no casting is needed. Both string keys and keypaths (keys separated by dots `.`) are supported when decoding JSON. @@ -27,7 +26,7 @@ Add the files inside the `Sources` folder into your Xcode project. **Swift Package Manager:** -Add the line `.Package(url: "https://github.com/lucianomarisi/JSONUtilities", majorVersion: 3)` to your `Package.swift`. +Add the line `.package(url: "https://github.com/yonaskolb/JSONUtilities.git", from: "4.2.0")` to the dependencies in your `Package.swift`. ## Types supported diff --git a/Sources/JSONUtilities/DecodingError.swift b/Sources/JSONUtilities/DecodingError.swift index 32edd83..44605a8 100644 --- a/Sources/JSONUtilities/DecodingError.swift +++ b/Sources/JSONUtilities/DecodingError.swift @@ -81,3 +81,6 @@ public struct DecodingError: Error, CustomStringConvertible, CustomDebugStringCo } } } + +// The dictionary, array and values of an error are plain JSON values, which are never mutated. +extension DecodingError: @unchecked Sendable {} diff --git a/Sources/JSONUtilities/Dictionary+JSONKey.swift b/Sources/JSONUtilities/Dictionary+JSONKey.swift index 98e41a7..16ab81f 100644 --- a/Sources/JSONUtilities/Dictionary+JSONKey.swift +++ b/Sources/JSONUtilities/Dictionary+JSONKey.swift @@ -38,7 +38,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath) -> T? { - return try? json(atKeyPath: keyPath) + return try? json(atKeyPath: keyPath) as T } // MARK: [JSONRawType] @@ -48,7 +48,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour = .remove) -> [T]? { - return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour) + return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour) as [T] } // MARK: [String: Any] @@ -68,7 +68,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour = .remove) -> [JSONDictionary]? { - return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour) + return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour) as [JSONDictionary] } // MARK: [String: JSONObjectConvertible] @@ -127,7 +127,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour = .remove) -> [T]? { - return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour) + return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour) as [T] } // MARK: RawRepresentable @@ -143,7 +143,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath) -> T? where T.RawValue: JSONRawType { - return try? json(atKeyPath: keyPath) + return try? json(atKeyPath: keyPath) as T } // MARK: [RawRepresentable] @@ -161,7 +161,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour = .remove) -> [T]? where T.RawValue: JSONRawType { - return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour) + return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour) as [T] } // MARK: [String: RawRepresentable] @@ -178,7 +178,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour = .remove) -> [K: T]? where T.RawValue: JSONRawType { - return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour) + return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour) as [K: T] } // MARK: JSONPrimitiveConvertible @@ -194,7 +194,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath) -> T? { - return try? json(atKeyPath: keyPath) + return try? json(atKeyPath: keyPath) as T } // MARK: [JSONPrimitiveConvertible] @@ -211,7 +211,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour = .remove) -> [T]? { - return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour) + return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour) as [T] } // MARK: [String: [RawRepresentable]] @@ -227,7 +227,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour = .remove) -> [String: [T]]? where T.RawValue: JSONRawType { - return try? json(atKeyPath: keyPath) + return try? json(atKeyPath: keyPath) as [String: [T]] } // MARK: [String: [JSONPrimitiveConvertible]] @@ -243,7 +243,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour = .remove) -> [String: [T]]? { - return try? json(atKeyPath: keyPath) + return try? json(atKeyPath: keyPath) as [String: [T]] } // MARK: [String: [JSONObjectConvertible]] @@ -259,7 +259,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour = .remove) -> [String: [T]]? { - return try? json(atKeyPath: keyPath) + return try? json(atKeyPath: keyPath) as [String: [T]] } // MARK: [String: [JSONRawType]] @@ -275,7 +275,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour = .remove) -> [String: [T]]? { - return try? json(atKeyPath: keyPath) + return try? json(atKeyPath: keyPath) as [String: [T]] } // MARK: [String: [String: RawRepresentable]] @@ -291,7 +291,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour = .remove) -> [String: [String: T]]? where T.RawValue: JSONRawType { - return try? json(atKeyPath: keyPath) + return try? json(atKeyPath: keyPath) as [String: [String: T]] } // MARK: [String: [String: JSONPrimitiveConvertible]] @@ -307,7 +307,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour = .remove) -> [String: [String: T]]? { - return try? json(atKeyPath: keyPath) + return try? json(atKeyPath: keyPath) as [String: [String: T]] } // MARK: [String: [String: JSONObjectConvertible]] @@ -323,7 +323,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour = .remove) -> [String: [String: T]]? { - return try? json(atKeyPath: keyPath) + return try? json(atKeyPath: keyPath) as [String: [String: T]] } // MARK: [String: [String: JSONRawType]] @@ -339,7 +339,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour = .remove) -> [String: [String: T]]? { - return try? json(atKeyPath: keyPath) + return try? json(atKeyPath: keyPath) as [String: [String: T]] } // MARK: [[RawRepresentable]] @@ -361,7 +361,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour = .remove) -> [[T]]? where T.RawValue: JSONRawType { - return try? json(atKeyPath: keyPath) + return try? json(atKeyPath: keyPath) as [[T]] } // MARK: [[JSONPrimitiveConvertible]] @@ -384,7 +384,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour = .remove) -> [[T]]? { - return try? json(atKeyPath: keyPath) + return try? json(atKeyPath: keyPath) as [[T]] } // MARK: [[JSONRawType]] @@ -403,7 +403,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour = .remove) -> [[T]]? { - return try? json(atKeyPath: keyPath) + return try? json(atKeyPath: keyPath) as [[T]] } // MARK: [[JSONObjectConvertible]] @@ -423,7 +423,7 @@ extension Dictionary where Key: JSONKey { } public func json(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour = .remove) -> [[T]]? { - return try? json(atKeyPath: keyPath) + return try? json(atKeyPath: keyPath) as [[T]] } } diff --git a/Sources/JSONUtilities/URL+JSONPrimitiveConvertible.swift b/Sources/JSONUtilities/URL+JSONPrimitiveConvertible.swift index 169fb35..665bd71 100644 --- a/Sources/JSONUtilities/URL+JSONPrimitiveConvertible.swift +++ b/Sources/JSONUtilities/URL+JSONPrimitiveConvertible.swift @@ -18,6 +18,17 @@ extension URL: JSONPrimitiveConvertible { /// /// - returns: Returns a URL if the input string was successfully converted to a URL public static func from(jsonValue: String) -> URL? { + // Since macOS 14, iOS 17 and Swift 6 on Linux, URL(string:) percent-encodes invalid characters instead of failing. + // Keep rejecting invalid URL strings, as on older systems. + #if canImport(Darwin) + if #available(macOS 14, iOS 17, tvOS 17, watchOS 10, *) { + return URL(string: jsonValue, encodingInvalidCharacters: false) + } return URL(string: jsonValue) + #elseif compiler(>=6.0) + return URL(string: jsonValue, encodingInvalidCharacters: false) + #else + return URL(string: jsonValue) + #endif } } diff --git a/Tests/JSONUtilitiesTests/Helpers/Dictionary+Equatable.swift b/Tests/JSONUtilitiesTests/Helpers/Dictionary+Equatable.swift index f5bab49..f8ae8b9 100644 --- a/Tests/JSONUtilitiesTests/Helpers/Dictionary+Equatable.swift +++ b/Tests/JSONUtilitiesTests/Helpers/Dictionary+Equatable.swift @@ -8,24 +8,30 @@ import Foundation +// Compares JSON values by their serialized form, so that the result doesn't depend on how each platform's +// Foundation bridges numbers and booleans (NSDictionary.isEqual differs between Darwin and Linux) +private func jsonEqual(_ lhs: Any, _ rhs: Any) -> Bool { + guard let lhsData = try? JSONSerialization.data(withJSONObject: lhs, options: .sortedKeys), + let rhsData = try? JSONSerialization.data(withJSONObject: rhs, options: .sortedKeys) else { + return false + } + return lhsData == rhsData +} + public func == (lhs: [String: Any], rhs: [String: Any]) -> Bool { - return NSDictionary(dictionary: lhs).isEqual(to: rhs) + return jsonEqual(lhs, rhs) } public func == (lhs: [String: Any]?, rhs: [String: Any]?) -> Bool { guard let lhs = lhs, let rhs = rhs else { return false } - return NSDictionary(dictionary: lhs).isEqual(to: rhs) + return jsonEqual(lhs, rhs) } public func == (lhs: [[String: Any]], rhs: [[String: Any]]) -> Bool { - let lhsArray = NSArray(array: lhs) - let rhsArray = NSArray(array: rhs) - return lhsArray.isEqual(to: rhsArray as [AnyObject]) + return jsonEqual(lhs, rhs) } public func == (lhs: [[String: Any]]?, rhs: [[String: Any]]?) -> Bool { guard let lhs = lhs, let rhs = rhs else { return false } - let lhsArray = NSArray(array: lhs) - let rhsArray = NSArray(array: rhs) - return lhsArray.isEqual(to: rhsArray as [AnyObject]) + return jsonEqual(lhs, rhs) } diff --git a/codecov.yml b/codecov.yml deleted file mode 100644 index 90ce587..0000000 --- a/codecov.yml +++ /dev/null @@ -1,9 +0,0 @@ -comment: - layout: header, changes, diff -coverage: - ignore: - - Sources/JSONUtilities/Deprecations.swift - - Tests/.* - - NonSwiftPackageManagerTests/.* - status: - patch: false