Skip to content

Commit e66dba5

Browse files
committed
Provide subscripts for JSON.Object to return the first key-value pair whose key matches a given key.
Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
1 parent 8b441f3 commit e66dba5

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Sources/JSONDecoding/JSON.Object (ext).swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
extension JSON.Object {
2+
@inlinable public subscript(key: JSON.Key) -> JSON.OptionalDecoder<JSON.Key> {
3+
JSON.OptionalDecoder(key: key, value: fields.first { $0.key == key }?.value)
4+
}
5+
6+
@inlinable public subscript(key: JSON.Key) -> JSON.FieldDecoder<JSON.Key>? {
7+
(fields.first { $0.key == key }?.value).map { JSON.FieldDecoder(key: key, value: $0) }
8+
}
9+
}
110
extension JSON.Object: RandomAccessCollection {
211
@inlinable public var startIndex: Int {
312
self.fields.startIndex

0 commit comments

Comments
 (0)