From e66dba5a8a881f5129de5f71983f073be8025c8c Mon Sep 17 00:00:00 2001 From: Ross Goldberg <484615+rgoldberg@users.noreply.github.com> Date: Mon, 20 Oct 2025 19:21:33 -0400 Subject: [PATCH] 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> --- Sources/JSONDecoding/JSON.Object (ext).swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sources/JSONDecoding/JSON.Object (ext).swift b/Sources/JSONDecoding/JSON.Object (ext).swift index 70d39d3..5ae87d2 100644 --- a/Sources/JSONDecoding/JSON.Object (ext).swift +++ b/Sources/JSONDecoding/JSON.Object (ext).swift @@ -1,3 +1,12 @@ +extension JSON.Object { + @inlinable public subscript(key: JSON.Key) -> JSON.OptionalDecoder { + JSON.OptionalDecoder(key: key, value: fields.first { $0.key == key }?.value) + } + + @inlinable public subscript(key: JSON.Key) -> JSON.FieldDecoder? { + (fields.first { $0.key == key }?.value).map { JSON.FieldDecoder(key: key, value: $0) } + } +} extension JSON.Object: RandomAccessCollection { @inlinable public var startIndex: Int { self.fields.startIndex