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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next Version

- Added `KeyAccessTracking` to report keys that were never read while decoding, with their paths
- 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`
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,25 @@ It's also possible to have an array of `JSONPrimitiveConvertible` values, for ex
let urlsDictionary = ["urls": ["www.google.com", "www.yahoo.com"]]
let urls: [URL] = try! urlsDictionary.json(atKeyPath: "urls") // [www.google.com, www.yahoo.com]
```

## Unused key tracking

`KeyAccessTracking` can report keys that were present in your JSON but never read while decoding it, which is useful for warning about typos or unsupported keys. It's opt-in and does nothing unless you decode inside `KeyAccessTracking.record`.

```swift
let json: JSONDictionary = ["name": "App", "child": ["name": "Child", "nmae": "typo"], "extra": 1]

let (parent, unusedKeys) = try KeyAccessTracking.record(root: json) {
try Parent(jsonDictionary: json)
}
unusedKeys.map { $0.description } // ["child.nmae", "extra"]
```

Every `JSONObjectConvertible` decoded with `json(atKeyPath:)` gets its own scope, and unused keys are reported with their path, including array indexes like `children[1].key`. Keys are marked as read when they are accessed with `json(atKeyPath:)`, whether or not they decode successfully.

- `KeyAccessTracking.markRead("key")` marks keys you read some other way, like `jsonDictionary["key"]`.
- `KeyAccessTracking.markAllRead()` marks every key of the current object as read, for objects whose keys are data.
- `KeyAccessTracking.scope(dictionary, at: ["sources", .index(1)]) { ... }` gives an object you decode yourself its own scope, at a path relative to the current one.
- Each `UnusedKey` has a `path` of `JSONPathComponent`s (`.key` or `.index`) and the unused `key`. Its `description` is the full path, like `sources[1].buildphase`.
- The current scope is a task local, so separate recordings never mix and child tasks continue their parent's recording. Threads that don't inherit task locals, like `DispatchQueue.concurrentPerform`, can continue it by capturing `KeyAccessTracking.currentContext` and running their decoding inside `KeyAccessTracking.withContext(context) { ... }`.

104 changes: 71 additions & 33 deletions Sources/JSONUtilities/Dictionary+JSONKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,26 @@ extension Dictionary where Key: JSONKey {
// MARK: JSONObjectConvertible

public func json<T: JSONObjectConvertible>(atKeyPath keyPath: KeyPath) throws -> T {
return try T(jsonDictionary: JSONDictionaryForKey(atKeyPath: keyPath))
let jsonDictionary = try JSONDictionaryForKey(atKeyPath: keyPath)
return try KeyAccessTracking.scope(jsonDictionary, at: keyPath.pathComponents) {
try T(jsonDictionary: jsonDictionary)
}
}

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

// MARK: [JSONObjectConvertible]

public func json<T: JSONObjectConvertible>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) throws -> [T] {
var index = -1
return try decodeArray(atKeyPath: keyPath, invalidItemBehaviour: invalidItemBehaviour) { keyPath, jsonArray, value in
index += 1
let jsonDictionary: JSONDictionary = try getValue(atKeyPath: keyPath, array: jsonArray, value: value)
return try T(jsonDictionary: jsonDictionary)
return try KeyAccessTracking.scope(jsonDictionary, at: keyPath.pathComponents + [.index(index)]) {
try T(jsonDictionary: jsonDictionary)
}
}
}

Expand Down Expand Up @@ -219,9 +226,12 @@ extension Dictionary where Key: JSONKey {
public func json<T: RawRepresentable>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) throws -> [String: [T]] where T.RawValue: JSONRawType {
let jsonDictionary: JSONDictionary = try json(atKeyPath: keyPath)
var dictionary: [String: [T]] = [:]
for key in jsonDictionary.keys {
let array: [T] = try jsonDictionary.json(atKeyPath: .key(key), invalidItemBehaviour: invalidItemBehaviour)
dictionary[key] = array
// the keys of this dictionary are data, so give it a scope with all keys read
try KeyAccessTracking.scope(jsonDictionary, at: keyPath.pathComponents, allKeysRead: true) { () throws -> Void in
for key in jsonDictionary.keys {
let array: [T] = try jsonDictionary.json(atKeyPath: .key(key), invalidItemBehaviour: invalidItemBehaviour)
dictionary[key] = array
}
}
return dictionary
}
Expand All @@ -235,9 +245,12 @@ extension Dictionary where Key: JSONKey {
public func json<T: JSONPrimitiveConvertible>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) throws -> [String: [T]] {
let jsonDictionary: JSONDictionary = try json(atKeyPath: keyPath)
var dictionary: [String: [T]] = [:]
for key in jsonDictionary.keys {
let array: [T] = try jsonDictionary.json(atKeyPath: .key(key), invalidItemBehaviour: invalidItemBehaviour)
dictionary[key] = array
// the keys of this dictionary are data, so give it a scope with all keys read
try KeyAccessTracking.scope(jsonDictionary, at: keyPath.pathComponents, allKeysRead: true) { () throws -> Void in
for key in jsonDictionary.keys {
let array: [T] = try jsonDictionary.json(atKeyPath: .key(key), invalidItemBehaviour: invalidItemBehaviour)
dictionary[key] = array
}
}
return dictionary
}
Expand All @@ -251,9 +264,12 @@ extension Dictionary where Key: JSONKey {
public func json<T: JSONObjectConvertible>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) throws -> [String: [T]] {
let jsonDictionary: JSONDictionary = try json(atKeyPath: keyPath)
var dictionary: [String: [T]] = [:]
for key in jsonDictionary.keys {
let array: [T] = try jsonDictionary.json(atKeyPath: .key(key), invalidItemBehaviour: invalidItemBehaviour)
dictionary[key] = array
// the keys of this dictionary are data, so give it a scope with all keys read
try KeyAccessTracking.scope(jsonDictionary, at: keyPath.pathComponents, allKeysRead: true) { () throws -> Void in
for key in jsonDictionary.keys {
let array: [T] = try jsonDictionary.json(atKeyPath: .key(key), invalidItemBehaviour: invalidItemBehaviour)
dictionary[key] = array
}
}
return dictionary
}
Expand All @@ -267,9 +283,12 @@ extension Dictionary where Key: JSONKey {
public func json<T: JSONRawType>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) throws -> [String: [T]] {
let jsonDictionary: JSONDictionary = try json(atKeyPath: keyPath)
var dictionary: [String: [T]] = [:]
for key in jsonDictionary.keys {
let array: [T] = try jsonDictionary.json(atKeyPath: .key(key), invalidItemBehaviour: invalidItemBehaviour)
dictionary[key] = array
// the keys of this dictionary are data, so give it a scope with all keys read
try KeyAccessTracking.scope(jsonDictionary, at: keyPath.pathComponents, allKeysRead: true) { () throws -> Void in
for key in jsonDictionary.keys {
let array: [T] = try jsonDictionary.json(atKeyPath: .key(key), invalidItemBehaviour: invalidItemBehaviour)
dictionary[key] = array
}
}
return dictionary
}
Expand All @@ -283,9 +302,12 @@ extension Dictionary where Key: JSONKey {
public func json<T: RawRepresentable>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) throws -> [String: [String: T]] where T.RawValue: JSONRawType {
let jsonDictionary: JSONDictionary = try json(atKeyPath: keyPath)
var dictionary: [String: [String: T]] = [:]
for key in jsonDictionary.keys {
let subDictionary: [String: T] = try jsonDictionary.json(atKeyPath: .key(key), invalidItemBehaviour: invalidItemBehaviour)
dictionary[key] = subDictionary
// the keys of this dictionary are data, so give it a scope with all keys read
try KeyAccessTracking.scope(jsonDictionary, at: keyPath.pathComponents, allKeysRead: true) { () throws -> Void in
for key in jsonDictionary.keys {
let subDictionary: [String: T] = try jsonDictionary.json(atKeyPath: .key(key), invalidItemBehaviour: invalidItemBehaviour)
dictionary[key] = subDictionary
}
}
return dictionary
}
Expand All @@ -299,9 +321,12 @@ extension Dictionary where Key: JSONKey {
public func json<T: JSONPrimitiveConvertible>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) throws -> [String: [String: T]] {
let jsonDictionary: JSONDictionary = try json(atKeyPath: keyPath)
var dictionary: [String: [String: T]] = [:]
for key in jsonDictionary.keys {
let subDictionary: [String: T] = try jsonDictionary.json(atKeyPath: .key(key), invalidItemBehaviour: invalidItemBehaviour)
dictionary[key] = subDictionary
// the keys of this dictionary are data, so give it a scope with all keys read
try KeyAccessTracking.scope(jsonDictionary, at: keyPath.pathComponents, allKeysRead: true) { () throws -> Void in
for key in jsonDictionary.keys {
let subDictionary: [String: T] = try jsonDictionary.json(atKeyPath: .key(key), invalidItemBehaviour: invalidItemBehaviour)
dictionary[key] = subDictionary
}
}
return dictionary
}
Expand All @@ -315,9 +340,12 @@ extension Dictionary where Key: JSONKey {
public func json<T: JSONObjectConvertible>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) throws -> [String: [String: T]] {
let jsonDictionary: JSONDictionary = try json(atKeyPath: keyPath)
var dictionary: [String: [String: T]] = [:]
for key in jsonDictionary.keys {
let subDictionary: [String: T] = try jsonDictionary.json(atKeyPath: .key(key), invalidItemBehaviour: invalidItemBehaviour)
dictionary[key] = subDictionary
// the keys of this dictionary are data, so give it a scope with all keys read
try KeyAccessTracking.scope(jsonDictionary, at: keyPath.pathComponents, allKeysRead: true) { () throws -> Void in
for key in jsonDictionary.keys {
let subDictionary: [String: T] = try jsonDictionary.json(atKeyPath: .key(key), invalidItemBehaviour: invalidItemBehaviour)
dictionary[key] = subDictionary
}
}
return dictionary
}
Expand All @@ -331,9 +359,12 @@ extension Dictionary where Key: JSONKey {
public func json<T: JSONRawType>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) throws -> [String: [String: T]] {
let jsonDictionary: JSONDictionary = try json(atKeyPath: keyPath)
var dictionary: [String: [String: T]] = [:]
for key in jsonDictionary.keys {
let subDictionary: [String: T] = try jsonDictionary.json(atKeyPath: .key(key), invalidItemBehaviour: invalidItemBehaviour)
dictionary[key] = subDictionary
// the keys of this dictionary are data, so give it a scope with all keys read
try KeyAccessTracking.scope(jsonDictionary, at: keyPath.pathComponents, allKeysRead: true) { () throws -> Void in
for key in jsonDictionary.keys {
let subDictionary: [String: T] = try jsonDictionary.json(atKeyPath: .key(key), invalidItemBehaviour: invalidItemBehaviour)
dictionary[key] = subDictionary
}
}
return dictionary
}
Expand Down Expand Up @@ -410,11 +441,15 @@ extension Dictionary where Key: JSONKey {

public func json<T: JSONObjectConvertible>(atKeyPath keyPath: KeyPath, invalidItemBehaviour: InvalidItemBehaviour<T> = .remove) throws -> [[T]] {

var outerIndex = -1
return try decodeArray(atKeyPath: keyPath) { (keyPath, jsonArray, value) -> [T] in
outerIndex += 1
if let array = value as? JSONArray {
return try array.map {
let jsonDictionary: JSONDictionary = try getValue(atKeyPath: keyPath, array: array, value: $0)
return try T(jsonDictionary: jsonDictionary)
return try array.enumerated().map { innerIndex, item in
let jsonDictionary: JSONDictionary = try getValue(atKeyPath: keyPath, array: array, value: item)
return try KeyAccessTracking.scope(jsonDictionary, at: keyPath.pathComponents + [.index(outerIndex), .index(innerIndex)]) {
try T(jsonDictionary: jsonDictionary)
}
}
} else {
throw DecodingError(dictionary: self, keyPath: keyPath, expectedType: JSONArray.self, value: value, array: jsonArray, reason: .incorrectType)
Expand Down Expand Up @@ -464,9 +499,12 @@ extension Dictionary where Key: JSONKey {
let jsonDictionary: JSONDictionary = try json(atKeyPath: keyPath)

var dictionary: [K: T] = [:]
for (key, _) in jsonDictionary {
if let jsonKey = K(rawValue: key), let item = try invalidItemBehaviour.decodeItem(decode: { try decode(jsonDictionary, KeyPath.key(key)) }) {
dictionary[jsonKey] = item
// the keys of this dictionary are data, so give it a scope with all keys read
try KeyAccessTracking.scope(jsonDictionary, at: keyPath.pathComponents, allKeysRead: true) { () throws -> Void in
for (key, _) in jsonDictionary {
if let jsonKey = K(rawValue: key), let item = try invalidItemBehaviour.decodeItem(decode: { try decode(jsonDictionary, KeyPath.key(key)) }) {
dictionary[jsonKey] = item
}
}
}

Expand Down
1 change: 1 addition & 0 deletions Sources/JSONUtilities/Dictionary+KeyPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ extension Dictionary {
guard let dictionary = self as? JSONDictionary else {
return nil
}
KeyAccessTracking.didRead(keyPath.firstKey)
return keyPath.getValue(dictionary: dictionary)
}
}
Loading
Loading