Skip to content
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
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions .hound.yml

This file was deleted.

1 change: 0 additions & 1 deletion .swift-version

This file was deleted.

33 changes: 0 additions & 33 deletions .travis.yml

This file was deleted.

7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
13 changes: 9 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -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"]),
]
)
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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

Expand Down
3 changes: 3 additions & 0 deletions Sources/JSONUtilities/DecodingError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
42 changes: 21 additions & 21 deletions Sources/JSONUtilities/Dictionary+JSONKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: JSONRawType>(atKeyPath keyPath: KeyPath) -> T? {
return try? json(atKeyPath: keyPath)
return try? json(atKeyPath: keyPath) as T
}

// MARK: [JSONRawType]
Expand All @@ -48,7 +48,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: JSONRawType>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) -> [T]? {
return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour)
return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour) as [T]
}

// MARK: [String: Any]
Expand All @@ -68,7 +68,7 @@ extension Dictionary where Key: JSONKey {
}

public func json(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<JSONDictionary> = .remove) -> [JSONDictionary]? {
return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour)
return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour) as [JSONDictionary]
}

// MARK: [String: JSONObjectConvertible]
Expand Down Expand Up @@ -127,7 +127,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: JSONObjectConvertible>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) -> [T]? {
return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour)
return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour) as [T]
}

// MARK: RawRepresentable
Expand All @@ -143,7 +143,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: RawRepresentable>(atKeyPath keyPath: KeyPath) -> T? where T.RawValue: JSONRawType {
return try? json(atKeyPath: keyPath)
return try? json(atKeyPath: keyPath) as T
}

// MARK: [RawRepresentable]
Expand All @@ -161,7 +161,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: RawRepresentable>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .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]
Expand All @@ -178,7 +178,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: RawRepresentable, K: JSONKey>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .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
Expand All @@ -194,7 +194,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: JSONPrimitiveConvertible>(atKeyPath keyPath: KeyPath) -> T? {
return try? json(atKeyPath: keyPath)
return try? json(atKeyPath: keyPath) as T
}

// MARK: [JSONPrimitiveConvertible]
Expand All @@ -211,7 +211,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: JSONPrimitiveConvertible>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) -> [T]? {
return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour)
return try? json(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour) as [T]
}

// MARK: [String: [RawRepresentable]]
Expand All @@ -227,7 +227,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: RawRepresentable>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) -> [String: [T]]? where T.RawValue: JSONRawType {
return try? json(atKeyPath: keyPath)
return try? json(atKeyPath: keyPath) as [String: [T]]
}

// MARK: [String: [JSONPrimitiveConvertible]]
Expand All @@ -243,7 +243,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: JSONPrimitiveConvertible>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) -> [String: [T]]? {
return try? json(atKeyPath: keyPath)
return try? json(atKeyPath: keyPath) as [String: [T]]
}

// MARK: [String: [JSONObjectConvertible]]
Expand All @@ -259,7 +259,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: JSONObjectConvertible>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) -> [String: [T]]? {
return try? json(atKeyPath: keyPath)
return try? json(atKeyPath: keyPath) as [String: [T]]
}

// MARK: [String: [JSONRawType]]
Expand All @@ -275,7 +275,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: JSONRawType>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) -> [String: [T]]? {
return try? json(atKeyPath: keyPath)
return try? json(atKeyPath: keyPath) as [String: [T]]
}

// MARK: [String: [String: RawRepresentable]]
Expand All @@ -291,7 +291,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: RawRepresentable>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .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]]
Expand All @@ -307,7 +307,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: JSONPrimitiveConvertible>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) -> [String: [String: T]]? {
return try? json(atKeyPath: keyPath)
return try? json(atKeyPath: keyPath) as [String: [String: T]]
}

// MARK: [String: [String: JSONObjectConvertible]]
Expand All @@ -323,7 +323,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: JSONObjectConvertible>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) -> [String: [String: T]]? {
return try? json(atKeyPath: keyPath)
return try? json(atKeyPath: keyPath) as [String: [String: T]]
}

// MARK: [String: [String: JSONRawType]]
Expand All @@ -339,7 +339,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: JSONRawType>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) -> [String: [String: T]]? {
return try? json(atKeyPath: keyPath)
return try? json(atKeyPath: keyPath) as [String: [String: T]]
}

// MARK: [[RawRepresentable]]
Expand All @@ -361,7 +361,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: RawRepresentable>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) -> [[T]]? where T.RawValue: JSONRawType {
return try? json(atKeyPath: keyPath)
return try? json(atKeyPath: keyPath) as [[T]]
}

// MARK: [[JSONPrimitiveConvertible]]
Expand All @@ -384,7 +384,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: JSONPrimitiveConvertible>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) -> [[T]]? {
return try? json(atKeyPath: keyPath)
return try? json(atKeyPath: keyPath) as [[T]]
}

// MARK: [[JSONRawType]]
Expand All @@ -403,7 +403,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: JSONRawType>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) -> [[T]]? {
return try? json(atKeyPath: keyPath)
return try? json(atKeyPath: keyPath) as [[T]]
}

// MARK: [[JSONObjectConvertible]]
Expand All @@ -423,7 +423,7 @@ extension Dictionary where Key: JSONKey {
}

public func json<T: JSONObjectConvertible>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) -> [[T]]? {
return try? json(atKeyPath: keyPath)
return try? json(atKeyPath: keyPath) as [[T]]
}
}

Expand Down
11 changes: 11 additions & 0 deletions Sources/JSONUtilities/URL+JSONPrimitiveConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
22 changes: 14 additions & 8 deletions Tests/JSONUtilitiesTests/Helpers/Dictionary+Equatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
9 changes: 0 additions & 9 deletions codecov.yml

This file was deleted.

Loading