diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 520f025..43a2638 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -12,8 +12,8 @@ on: jobs: Linux: runs-on: [ubuntu-latest] - container: swift:5.10 + container: swiftlang/swift:nightly-6.4.x-noble steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v4 - name: Linux run: swift build -v diff --git a/Package.swift b/Package.swift index 3051f2f..e559d64 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version: 5.10 +// swift-tools-version: 6.4 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription @@ -46,11 +46,11 @@ let targetDependencies: [Target.Dependency] = [ let package = Package( name: "ReerJSON", platforms: [ - .macOS(.v10_13), - .iOS(.v12), - .tvOS(.v12), - .watchOS(.v6), - .macCatalyst(.v13), + .macOS(.v12), + .iOS(.v15), + .tvOS(.v15), + .watchOS(.v9), + .macCatalyst(.v15), .visionOS(.v1) ], products: [ diff --git a/README.md b/README.md index 3baf584..5adfdcb 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,10 @@ Tested with ReerJSON 1.0.1, ZippyJSON 1.2.15, IkigaJSON 2.3.2 # Installation +ReerJSON requires Swift 6.4 or newer while keeping the package's existing +Apple platform deployment targets: macOS 12, iOS 15, tvOS 15, watchOS 9, +Mac Catalyst 15, and visionOS 1. + ## Swift Package Manager Add dependency in `Package.swift` or project `Package Dependencies` @@ -96,6 +100,23 @@ if let name = value["users"]?[0]?["name"]?.string { } ``` +For hot DOM traversal, use `JSONDocument` and borrow values from the document +instead of creating escaping `JSONValue` wrappers for every child: + +```swift +let document = try JSONDocument(string: json) + +let userCount = try document.withRootValue { root in + root.withObject { object in + object.withValue(forKey: "users") { users in + users.withArray { array in + array.count + } ?? 0 + } ?? 0 + } ?? 0 +} +``` + ## In-Place Parsing For maximum performance with large JSON files, @@ -112,7 +133,7 @@ avoiding memory allocation for string storage. The `inout` parameter makes it clear that the data is consumed by this operation. > [!NOTE] -> For most use cases, the standard `YYJSONValue(data:)` initializer is sufficient. +> For most use cases, the standard `JSONValue(data:)` initializer is sufficient. > Use in-place parsing only when performance is critical > and you can accept the ownership semantics. @@ -169,8 +190,8 @@ for try await event in dataChunks.decode(Event.self, mode: .jsonLines) { } ``` -`AsyncSequence` adapters are available on macOS 10.15, iOS 13, tvOS 13, -and watchOS 6 or newer. +`AsyncSequence` adapters are available on macOS 12, iOS 15, tvOS 15, watchOS 9, +Mac Catalyst 15, and visionOS 1 when building with Swift 6.4 or newer. ## JSONSerialization Alternative diff --git a/Sources/ReerJSON/JSONDecoderImpl.swift b/Sources/ReerJSON/JSONDecoderImpl.swift index 0235414..ab93b25 100644 --- a/Sources/ReerJSON/JSONDecoderImpl.swift +++ b/Sources/ReerJSON/JSONDecoderImpl.swift @@ -212,24 +212,12 @@ final class JSONDecoderImpl: Decoder { if type == [UInt64].self { return try unboxArray(from: value, as: [UInt64].self, for: codingPathNode, additionalKey) as! T } - #if compiler(>=6.0) - if #available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) { - if type == [Int128].self { - return try unboxArray(from: value, as: [Int128].self, for: codingPathNode, additionalKey) as! T - } - if type == [UInt128].self { - return try unboxArray(from: value, as: [UInt128].self, for: codingPathNode, additionalKey) as! T - } - } - #endif - return try with(value: value, path: codingPathNode.appending(additionalKey)) { try type.init(from: self) } } - #if !os(Linux) - @available(macOS 12, iOS 15, tvOS 15, watchOS 8, visionOS 1, *) +#if !os(Linux) @inline(__always) func unbox( _ value: JSON, @@ -469,23 +457,7 @@ final class JSONDecoderImpl: Decoder { let intValue: UInt64 = try unboxInteger(elementValue, for: arrayCodingPathNode, indexKey) decodedValue = intValue as! T } else { - #if compiler(>=6.0) - if #available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) { - if T.self == Int128.self { - let intValue: Int128 = try unboxInteger(elementValue, for: arrayCodingPathNode, indexKey) - decodedValue = intValue as! T - } else if T.self == UInt128.self { - let intValue: UInt128 = try unboxInteger(elementValue, for: arrayCodingPathNode, indexKey) - decodedValue = intValue as! T - } else { - decodedValue = try unbox(elementValue, as: T.self, for: arrayCodingPathNode, indexKey) - } - } else { - decodedValue = try unbox(elementValue, as: T.self, for: arrayCodingPathNode, indexKey) - } - #else - decodedValue = try unbox(elementValue, as: T.self, for: arrayCodingPathNode, indexKey) - #endif + decodedValue = try unbox(elementValue, as: T.self, for: arrayCodingPathNode, indexKey) } result.append(decodedValue) @@ -619,12 +591,10 @@ extension JSONDecoderImpl: SingleValueDecodingContainer { try decodeInteger() } - #if compiler(>=6.0) - @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) + @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, macCatalyst 18.0, visionOS 2.0, *) func decode(_: Int128.Type) throws -> Int128 { try decodeInteger() } - #endif func decode(_: UInt.Type) throws -> UInt { try decodeInteger() @@ -646,12 +616,10 @@ extension JSONDecoderImpl: SingleValueDecodingContainer { try decodeInteger() } - #if compiler(>=6.0) - @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) + @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, macCatalyst 18.0, visionOS 2.0, *) func decode(_: UInt128.Type) throws -> UInt128 { try decodeInteger() } - #endif func decode(_ type: T.Type) throws -> T { return try unbox(topValue, as: type, for: codingPathNode, _CodingKey?.none) @@ -834,13 +802,11 @@ private final class DefaultKeyedDecodingContainer: KeyedDecodingCo return try decodeInteger(jsonValue, forKey: key) } - #if compiler(>=6.0) - @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) + @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, macCatalyst 18.0, visionOS 2.0, *) func decode(_: Int128.Type, forKey key: K) throws -> Int128 { let jsonValue = try getValue(forKey: key) return try decodeInteger(jsonValue, forKey: key) } - #endif func decodeIfPresent(_: Int64.Type, forKey key: K) throws -> Int64? { guard let jsonValue = getValueIfPresent(forKey: key) else { @@ -902,13 +868,11 @@ private final class DefaultKeyedDecodingContainer: KeyedDecodingCo return try decodeInteger(jsonValue, forKey: key) } - #if compiler(>=6.0) - @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) + @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, macCatalyst 18.0, visionOS 2.0, *) func decode(_: UInt128.Type, forKey key: K) throws -> UInt128 { let jsonValue = try getValue(forKey: key) return try decodeInteger(jsonValue, forKey: key) } - #endif func decodeIfPresent(_: UInt64.Type, forKey key: K) throws -> UInt64? { guard let jsonValue = getValueIfPresent(forKey: key) else { @@ -1207,13 +1171,11 @@ private final class PreTransformKeyedDecodingContainer: KeyedDecod return try decodeInteger(jsonValue, forKey: key) } - #if compiler(>=6.0) - @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) + @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, macCatalyst 18.0, visionOS 2.0, *) func decode(_: Int128.Type, forKey key: K) throws -> Int128 { let jsonValue = try getValue(forKey: key) return try decodeInteger(jsonValue, forKey: key) } - #endif func decodeIfPresent(_: Int64.Type, forKey key: K) throws -> Int64? { guard let jsonValue = getValueIfPresent(forKey: key) else { @@ -1275,13 +1237,11 @@ private final class PreTransformKeyedDecodingContainer: KeyedDecod return try decodeInteger(jsonValue, forKey: key) } - #if compiler(>=6.0) - @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) + @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, macCatalyst 18.0, visionOS 2.0, *) func decode(_: UInt128.Type, forKey key: K) throws -> UInt128 { let jsonValue = try getValue(forKey: key) return try decodeInteger(jsonValue, forKey: key) } - #endif func decodeIfPresent(_: UInt64.Type, forKey key: K) throws -> UInt64? { guard let jsonValue = getValueIfPresent(forKey: key) else { @@ -1561,13 +1521,11 @@ private struct JSONUnkeyedDecodingContainer: UnkeyedDecodingContainer { return try decodeInteger(value) } - #if compiler(>=6.0) - @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) + @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, macCatalyst 18.0, visionOS 2.0, *) mutating func decode(_: Int128.Type) throws -> Int128 { let value = try valueFromIterator(ofType: Int128.self) return try decodeInteger(value) } - #endif mutating func decode(_: UInt.Type) throws -> UInt { let value = try valueFromIterator(ofType: UInt.self) @@ -1594,13 +1552,11 @@ private struct JSONUnkeyedDecodingContainer: UnkeyedDecodingContainer { return try decodeInteger(value) } - #if compiler(>=6.0) - @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) + @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, macCatalyst 18.0, visionOS 2.0, *) mutating func decode(_: UInt128.Type) throws -> UInt128 { let value = try valueFromIterator(ofType: UInt.self) return try decodeInteger(value) } - #endif mutating func decode(_ type: T.Type) throws -> T { let value = try valueFromIterator(ofType: type) diff --git a/Sources/ReerJSON/JSONEncoderImpl.swift b/Sources/ReerJSON/JSONEncoderImpl.swift index 16ae041..65c73a8 100644 --- a/Sources/ReerJSON/JSONEncoderImpl.swift +++ b/Sources/ReerJSON/JSONEncoderImpl.swift @@ -241,8 +241,28 @@ class JSONEncoderImpl: Encoder { arr.withUnsafeBufferPointer { yyjson_mut_arr_with_sint64(doc, $0.baseAddress, $0.count) } } @inline(__always) func wrapBulkIntArray(_ arr: [Int]) -> UnsafeMutablePointer { - let mapped = arr.map { Int64($0) } - return mapped.withUnsafeBufferPointer { yyjson_mut_arr_with_sint64(doc, $0.baseAddress, $0.count) } + arr.withUnsafeBufferPointer { buffer in + guard let baseAddress = buffer.baseAddress else { + return yyjson_mut_arr(doc)! + } + if MemoryLayout.size == MemoryLayout.size, + MemoryLayout.alignment == MemoryLayout.alignment { + return baseAddress.withMemoryRebound(to: Int64.self, capacity: buffer.count) { + yyjson_mut_arr_with_sint64(doc, $0, buffer.count) + } + } + return withTemporaryAllocation(of: Int64.self, capacity: buffer.count) { values in + var result: UnsafeMutablePointer? + values.withUnsafeMutableBufferPointer { pointer, initializedCount in + for index in 0.. UnsafeMutablePointer { arr.withUnsafeBufferPointer { yyjson_mut_arr_with_uint8(doc, $0.baseAddress, $0.count) } @@ -257,8 +277,28 @@ class JSONEncoderImpl: Encoder { arr.withUnsafeBufferPointer { yyjson_mut_arr_with_uint64(doc, $0.baseAddress, $0.count) } } @inline(__always) func wrapBulkUIntArray(_ arr: [UInt]) -> UnsafeMutablePointer { - let mapped = arr.map { UInt64($0) } - return mapped.withUnsafeBufferPointer { yyjson_mut_arr_with_uint64(doc, $0.baseAddress, $0.count) } + arr.withUnsafeBufferPointer { buffer in + guard let baseAddress = buffer.baseAddress else { + return yyjson_mut_arr(doc)! + } + if MemoryLayout.size == MemoryLayout.size, + MemoryLayout.alignment == MemoryLayout.alignment { + return baseAddress.withMemoryRebound(to: UInt64.self, capacity: buffer.count) { + yyjson_mut_arr_with_uint64(doc, $0, buffer.count) + } + } + return withTemporaryAllocation(of: UInt64.self, capacity: buffer.count) { values in + var result: UnsafeMutablePointer? + values.withUnsafeMutableBufferPointer { pointer, initializedCount in + for index in 0.. UnsafeMutablePointer { let result = yyjson_mut_arr(doc)! diff --git a/Sources/ReerJSON/ReerJSONDecoder.swift b/Sources/ReerJSON/ReerJSONDecoder.swift index 3ac8450..7bd373d 100644 --- a/Sources/ReerJSON/ReerJSONDecoder.swift +++ b/Sources/ReerJSON/ReerJSONDecoder.swift @@ -200,13 +200,7 @@ open class ReerJSONDecoder { if options.json5 { flag |= YYJSON_READ_JSON5 } - let doc = data.withUnsafeBytes { - yyjson_read( - $0.bindMemory(to: CChar.self).baseAddress, - data.count, - flag - ) - } + let doc = yyReadDocument(from: data, flags: flag) guard let doc else { return try decodeWithFoundationDecoder(type, from: data, options: options) } @@ -274,13 +268,7 @@ open class ReerJSONDecoder { if options.json5 { flag |= YYJSON_READ_JSON5 } - let doc = data.withUnsafeBytes { - yyjson_read( - $0.bindMemory(to: CChar.self).baseAddress, - data.count, - flag - ) - } + let doc = yyReadDocument(from: data, flags: flag) guard let doc else { if #available(macOS 14, iOS 17, tvOS 17, watchOS 10, visionOS 1, *) { return try decodeWithFoundationDecoder(type, from: data, options: options, configuration: configuration) diff --git a/Sources/ReerJSON/ReerJSONEncoder.swift b/Sources/ReerJSON/ReerJSONEncoder.swift index fc7ae9b..57a619b 100644 --- a/Sources/ReerJSON/ReerJSONEncoder.swift +++ b/Sources/ReerJSON/ReerJSONEncoder.swift @@ -293,27 +293,33 @@ open class ReerJSONEncoder { private func sortObjectKeys(_ val: UnsafeMutablePointer) { typealias MutVal = UnsafeMutablePointer + typealias Pair = (keyVal: MutVal, val: MutVal, keyStr: UnsafePointer) if yyjson_mut_is_obj(val) { - var pairs: [(keyVal: MutVal, val: MutVal, keyStr: UnsafePointer)] = [] - pairs.reserveCapacity(Int(yyjson_mut_obj_size(val))) + withTemporaryAllocation(of: Pair.self, capacity: Int(yyjson_mut_obj_size(val))) { pairs in + var iter = yyjson_mut_obj_iter() + guard yyjson_mut_obj_iter_init(val, &iter) else { return } - var iter = yyjson_mut_obj_iter() - guard yyjson_mut_obj_iter_init(val, &iter) else { return } + while let keyPtr = yyjson_mut_obj_iter_next(&iter) { + guard let valPtr = yyjson_mut_obj_iter_get_val(keyPtr), + let keyStr = yyjson_mut_get_str(keyPtr) else { continue } + pairs.append((keyPtr, valPtr, keyStr)) + } - while let keyPtr = yyjson_mut_obj_iter_next(&iter) { - guard let valPtr = yyjson_mut_obj_iter_get_val(keyPtr), - let keyStr = yyjson_mut_get_str(keyPtr) else { continue } - pairs.append((keyPtr, valPtr, keyStr)) - } - - pairs.sort { strcmp($0.keyStr, $1.keyStr) < 0 } + pairs.withUnsafeMutableBufferPointer { storage, initializedCount in + var initialized = UnsafeMutableBufferPointer( + start: storage.baseAddress, + count: initializedCount + ) + initialized.sort { strcmp($0.keyStr, $1.keyStr) < 0 } - yyjson_mut_obj_clear(val) + yyjson_mut_obj_clear(val) - for pair in pairs { - sortObjectKeys(pair.val) - yyjson_mut_obj_add(val, pair.keyVal, pair.val) + for pair in initialized { + sortObjectKeys(pair.val) + yyjson_mut_obj_add(val, pair.keyVal, pair.val) + } + } } } else if yyjson_mut_is_arr(val) { var iter = yyjson_mut_arr_iter() diff --git a/Sources/ReerJSON/ReerJSONSerialization.swift b/Sources/ReerJSON/ReerJSONSerialization.swift index 91cd4ef..1a2bbb2 100644 --- a/Sources/ReerJSON/ReerJSONSerialization.swift +++ b/Sources/ReerJSON/ReerJSONSerialization.swift @@ -106,7 +106,7 @@ public enum ReerJSONSerialization { readOptions.insert(.json5) } - let document = try Document(data: data, options: readOptions) + let document = try DocumentRef(data: data, options: readOptions) guard let root = document.root else { throw JSONError.invalidData("Document has no root value") } diff --git a/Sources/ReerJSON/StreamDecoder.swift b/Sources/ReerJSON/StreamDecoder.swift index 05c3f7b..b99c191 100644 --- a/Sources/ReerJSON/StreamDecoder.swift +++ b/Sources/ReerJSON/StreamDecoder.swift @@ -180,29 +180,33 @@ struct ByteChunkBuffer: Sendable { mutating func readChunk( nextByte: () async throws -> UInt8? - ) async rethrows -> (data: Data, reachedEnd: Bool) { + ) async rethrows -> (byteCount: Int, reachedEnd: Bool) { var count = 0 while count < storage.count { guard let byte = try await nextByte() else { - return (data(forByteCount: count), true) + return (count, true) } storage[count] = byte count += 1 } - return (data(forByteCount: count), false) + return (count, false) } - private func data(forByteCount count: Int) -> Data { - guard count > 0 else { return Data() } - return storage.withUnsafeBufferPointer { buffer in - guard let baseAddress = buffer.baseAddress else { return Data() } - return Data(bytes: baseAddress, count: count) + func withUnsafeBufferPointer( + byteCount count: Int, + _ body: (UnsafeBufferPointer) throws -> R + ) rethrows -> R { + try storage.withUnsafeBufferPointer { buffer in + let clampedCount = Swift.min(Swift.max(count, 0), buffer.count) + return try body(UnsafeBufferPointer( + start: buffer.baseAddress, + count: clampedCount + )) } } } /// An `AsyncSequence` that yields ``JSONValue`` items from chunks of `Data`. -@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) public struct JSONValueStream: AsyncSequence, Sendable where Source.Element == Data { public typealias Element = JSONValue @@ -256,7 +260,6 @@ where Source.Element == Data { } /// An `AsyncSequence` that yields ``JSONValue`` items from an `AsyncSequence` of bytes. -@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) public struct JSONValueByteStream: AsyncSequence, Sendable where Source.Element == UInt8 { public typealias Element = JSONValue @@ -301,9 +304,11 @@ where Source.Element == UInt8 { return nil } - let chunk = try await readChunk() - if !chunk.isEmpty { - let values = try parser.parse(chunk) + let byteCount = try await readChunk() + if byteCount > 0 { + let values = try chunkBuffer.withUnsafeBufferPointer(byteCount: byteCount) { + try parser.parse(bytes: $0) + } if !values.isEmpty { pending.replace(with: values) continue @@ -322,13 +327,13 @@ where Source.Element == UInt8 { } /// Reads up to `chunkSize` bytes into reusable storage before - /// constructing the `Data` chunk for parsing. - private mutating func readChunk() async throws -> Data { + /// passing the borrowed bytes directly to the parser. + private mutating func readChunk() async throws -> Int { let chunk = try await chunkBuffer.readChunk { try await sourceIterator.next() } sourceExhausted = chunk.reachedEnd - return chunk.data + return chunk.byteCount } } } @@ -337,7 +342,6 @@ where Source.Element == UInt8 { /// /// Internally this decodes values already parsed by ``JSONStreamParser``, /// skipping the serialization and reparse round-trip. -@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) public struct DecodingStream: AsyncSequence, Sendable where Source.Element == Data { @@ -413,7 +417,6 @@ where Source.Element == Data { // MARK: - AsyncSequence Extensions -@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) extension AsyncSequence where Element == Data, Self: Sendable { /// Returns an `AsyncSequence` of ``JSONValue`` items parsed from this @@ -451,7 +454,6 @@ extension AsyncSequence where Element == Data, Self: Sendable { } } -@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) extension AsyncSequence where Element == UInt8, Self: Sendable { /// Returns an `AsyncSequence` of ``JSONValue`` items parsed from this diff --git a/Sources/ReerJSON/StreamParser.swift b/Sources/ReerJSON/StreamParser.swift index 7ef348b..1d6d52c 100644 --- a/Sources/ReerJSON/StreamParser.swift +++ b/Sources/ReerJSON/StreamParser.swift @@ -130,6 +130,18 @@ public struct JSONStreamParser: Sendable { return try drainValues(finalizing: false) } + /// Feeds borrowed bytes to the parser and returns all complete JSON values found. + public mutating func parse(bytes: Span) throws -> [JSONValue] { + if !bytes.isEmpty { + bytes.withUnsafeBufferPointer { source in + if let base = source.baseAddress { + buffer.append(base, count: source.count) + } + } + } + return try drainValues(finalizing: false) + } + /// Signals end-of-stream and returns any remaining JSON values. /// /// After calling this method, the parser is in a finished state. @@ -160,7 +172,7 @@ public struct JSONStreamParser: Sendable { /// One parsed value's metadata. private struct ParsedItem { /// yyjson document owning the parsed value. - let document: Document + let document: DocumentRef } // MARK: - Drain entry points @@ -215,7 +227,7 @@ public struct JSONStreamParser: Sendable { switch arrayState { case .expectOpenBracket: guard readOffset < buffer.count else { break loop } - let byte = buffer[buffer.startIndex + readOffset] + let byte = byteAtReadOffset() guard byte == UInt8(ascii: "[") else { throw JSONError.invalidJSON("Expected '[' at start of JSON array stream") } @@ -225,7 +237,7 @@ public struct JSONStreamParser: Sendable { case .expectElementOrClose: skipWhitespace() guard readOffset < buffer.count else { break loop } - let byte = buffer[buffer.startIndex + readOffset] + let byte = byteAtReadOffset() if byte == UInt8(ascii: "]") { readOffset += 1 arrayState = .done @@ -238,7 +250,7 @@ public struct JSONStreamParser: Sendable { case .expectElementAfterComma: skipWhitespace() guard readOffset < buffer.count else { break loop } - let byte = buffer[buffer.startIndex + readOffset] + let byte = byteAtReadOffset() if byte == UInt8(ascii: "]") { guard allowsTrailingCommas else { throw JSONError.invalidJSON("Trailing comma is not allowed in JSON array stream") @@ -254,7 +266,7 @@ public struct JSONStreamParser: Sendable { case .expectCommaOrClose: skipWhitespace() guard readOffset < buffer.count else { break loop } - let byte = buffer[buffer.startIndex + readOffset] + let byte = byteAtReadOffset() if byte == UInt8(ascii: ",") { readOffset += 1 arrayState = .expectElementAfterComma @@ -295,10 +307,10 @@ public struct JSONStreamParser: Sendable { // and copies the input — so we do not need to add YYJSON_PADDING_SIZE // bytes ourselves here. let startOffset = readOffset - let result: Document.StreamParseResult = try buffer.withUnsafeBytes { buf in - guard let base = buf.baseAddress else { return .needMoreData } - let ptr = base.advanced(by: startOffset).assumingMemoryBound(to: UInt8.self) - return try Document.streamParse(bytes: ptr, count: available, options: options) + let result: DocumentRef.StreamParseResult = try buffer.withUnsafeBytes { rawBuffer in + guard let base = rawBuffer.baseAddress else { return .needMoreData } + let bytes = base.advanced(by: startOffset).assumingMemoryBound(to: UInt8.self) + return try DocumentRef.streamParse(bytes: bytes, count: available, options: options) } switch result { @@ -329,11 +341,25 @@ public struct JSONStreamParser: Sendable { } private mutating func skipWhitespace() { - let startIdx = buffer.startIndex - while readOffset < buffer.count { - let byte = buffer[startIdx + readOffset] - guard byte == 0x20 || byte == 0x09 || byte == 0x0A || byte == 0x0D else { break } - readOffset += 1 + buffer.withUnsafeBytes { rawBuffer in + guard let base = rawBuffer.baseAddress?.assumingMemoryBound(to: UInt8.self) else { + return + } + while readOffset < rawBuffer.count { + let byte = base.advanced(by: readOffset).pointee + guard byte == 0x20 || byte == 0x09 || byte == 0x0A || byte == 0x0D else { break } + readOffset += 1 + } + } + } + + @inline(__always) + private func byteAtReadOffset() -> UInt8 { + buffer.withUnsafeBytes { rawBuffer in + rawBuffer.baseAddress! + .advanced(by: readOffset) + .assumingMemoryBound(to: UInt8.self) + .pointee } } @@ -368,11 +394,8 @@ public struct JSONStreamParser: Sendable { /// The outcome of feeding a chunk of bytes to a ``JSONIncrementalReader``. /// -/// This is intentionally a non-copyable enum rather than `JSONDocument?` -/// because ``JSONDocument`` is `~Copyable`, and `Optional<~Copyable>` is -/// not yet supported by Swift 5.10 (the minimum required by this package on -/// Linux). Using a custom enum keeps the API ergonomic on every supported -/// toolchain. +/// This is a non-copyable enum because it carries a move-only ``JSONDocument`` +/// in the ready state while keeping the "need more data" state explicit. public enum JSONIncrementalReadResult: ~Copyable { /// The reader has fully assembled a complete document. case ready(JSONDocument) @@ -392,7 +415,7 @@ public enum JSONIncrementalReadResult: ~Copyable { /// for try await chunk in stream { /// switch try reader.feed(chunk) { /// case .ready(let doc): -/// // doc.root is now available +/// // Traverse with doc.withRootValue { ... } /// return doc /// case .needMoreData: /// continue @@ -455,7 +478,7 @@ public final class JSONIncrementalReader: @unchecked Sendable { switch try parser.read() { case .success(let doc): finished = true - return .ready(JSONDocument(_document: doc)) + return .ready(JSONDocument(_storage: doc)) case .needMoreData: return .needMoreData } @@ -473,7 +496,7 @@ public final class JSONIncrementalReader: @unchecked Sendable { switch try parser.read() { case .success(let doc): finished = true - return JSONDocument(_document: doc) + return JSONDocument(_storage: doc) case .needMoreData: throw JSONError.invalidJSON("Incomplete JSON value at end of stream") } diff --git a/Sources/ReerJSON/Utilities.swift b/Sources/ReerJSON/Utilities.swift index 4847b3d..4a3e027 100644 --- a/Sources/ReerJSON/Utilities.swift +++ b/Sources/ReerJSON/Utilities.swift @@ -200,6 +200,97 @@ import yyjson // MARK: - Helper Functions +@inline(__always) +func yyReadDocument( + from buffer: UnsafeRawBufferPointer, + flags: yyjson_read_flag +) -> UnsafeMutablePointer? { + guard let baseAddress = buffer.baseAddress else { return nil } + let pointer = UnsafeMutablePointer( + mutating: baseAddress.assumingMemoryBound(to: CChar.self) + ) + return yyjson_read(pointer, buffer.count, flags) +} + +@inline(__always) +func yyReadDocument( + from buffer: UnsafeRawBufferPointer, + flags: yyjson_read_flag, + error: inout yyjson_read_err +) -> UnsafeMutablePointer? { + guard let baseAddress = buffer.baseAddress else { return nil } + let pointer = UnsafeMutablePointer( + mutating: baseAddress.assumingMemoryBound(to: CChar.self) + ) + return yyjson_read_opts(pointer, buffer.count, flags, nil, &error) +} + +@inline(__always) +func yyReadDocument( + mutating buffer: UnsafeMutableRawBufferPointer, + validByteCount: Int, + flags: yyjson_read_flag +) -> UnsafeMutablePointer? { + guard let baseAddress = buffer.baseAddress else { return nil } + let pointer = baseAddress.assumingMemoryBound(to: CChar.self) + return yyjson_read(pointer, validByteCount, flags) +} + +@inline(__always) +func yyReadDocument( + mutating buffer: UnsafeMutableRawBufferPointer, + validByteCount: Int, + flags: yyjson_read_flag, + error: inout yyjson_read_err +) -> UnsafeMutablePointer? { + guard let baseAddress = buffer.baseAddress else { return nil } + let pointer = baseAddress.assumingMemoryBound(to: CChar.self) + return yyjson_read_opts(pointer, validByteCount, flags, nil, &error) +} + +@inline(__always) +func yyReadDocument( + from data: Data, + flags: yyjson_read_flag +) -> UnsafeMutablePointer? { + data.withUnsafeBytes { buffer in + yyReadDocument(from: buffer, flags: flags) + } +} + +@inline(__always) +func yyReadDocument( + from data: Data, + flags: yyjson_read_flag, + error: inout yyjson_read_err +) -> UnsafeMutablePointer? { + data.withUnsafeBytes { buffer in + yyReadDocument(from: buffer, flags: flags, error: &error) + } +} + +@inline(__always) +func yyReadDocument( + from bytes: RawSpan, + flags: yyjson_read_flag +) -> UnsafeMutablePointer? { + bytes.withUnsafeBytes { buffer in + yyReadDocument(from: buffer, flags: flags) + } +} + +@inline(__always) +func yyReadDocument( + from bytes: RawSpan, + flags: yyjson_read_flag, + error: inout yyjson_read_err +) -> UnsafeMutablePointer? { + bytes.withUnsafeBytes { buffer in + yyReadDocument(from: buffer, flags: flags, error: &error) + } +} + +@usableFromInline @inline(__always) func yyObjGet( _ obj: UnsafeMutablePointer, @@ -240,38 +331,44 @@ func yyFromString( /// avoiding Swift String allocations during sorting. func sortObjectKeys(_ val: UnsafeMutablePointer) throws { typealias MutVal = UnsafeMutablePointer + typealias Pair = (keyVal: MutVal, val: MutVal, keyStr: UnsafePointer) if yyjson_mut_is_obj(val) { - var pairs: [(keyVal: MutVal, val: MutVal, keyStr: UnsafePointer)] = [] - pairs.reserveCapacity(Int(yyjson_mut_obj_size(val))) - - var iter = yyjson_mut_obj_iter() - guard yyjson_mut_obj_iter_init(val, &iter) else { - throw JSONError.invalidData("Failed to initialize object iterator during key sorting") - } - - while let keyPtr = yyjson_mut_obj_iter_next(&iter) { - guard let valPtr = yyjson_mut_obj_iter_get_val(keyPtr) else { - throw JSONError.invalidData("Object key has no associated value during key sorting") + try withTemporaryAllocation(of: Pair.self, capacity: Int(yyjson_mut_obj_size(val))) { pairs in + var iter = yyjson_mut_obj_iter() + guard yyjson_mut_obj_iter_init(val, &iter) else { + throw JSONError.invalidData("Failed to initialize object iterator during key sorting") } - guard let keyStr = yyjson_mut_get_str(keyPtr) else { - throw JSONError.invalidData("Object key is not a string during key sorting") - } - pairs.append((keyPtr, valPtr, keyStr)) - } - pairs.sort { pair1, pair2 in - return strcmp(pair1.keyStr, pair2.keyStr) < 0 - } - - guard yyjson_mut_obj_clear(val) else { - throw JSONError.invalidData("Failed to clear object during key sorting") - } + while let keyPtr = yyjson_mut_obj_iter_next(&iter) { + guard let valPtr = yyjson_mut_obj_iter_get_val(keyPtr) else { + throw JSONError.invalidData("Object key has no associated value during key sorting") + } + guard let keyStr = yyjson_mut_get_str(keyPtr) else { + throw JSONError.invalidData("Object key is not a string during key sorting") + } + pairs.append((keyPtr, valPtr, keyStr)) + } - for pair in pairs { - try sortObjectKeys(pair.val) - guard yyjson_mut_obj_add(val, pair.keyVal, pair.val) else { - throw JSONError.invalidData("Failed to add key back to object during key sorting") + try pairs.withUnsafeMutableBufferPointer { storage, initializedCount in + var initialized = UnsafeMutableBufferPointer( + start: storage.baseAddress, + count: initializedCount + ) + initialized.sort { pair1, pair2 in + strcmp(pair1.keyStr, pair2.keyStr) < 0 + } + + guard yyjson_mut_obj_clear(val) else { + throw JSONError.invalidData("Failed to clear object during key sorting") + } + + for pair in initialized { + try sortObjectKeys(pair.val) + guard yyjson_mut_obj_add(val, pair.keyVal, pair.val) else { + throw JSONError.invalidData("Failed to add key back to object during key sorting") + } + } } } } else if yyjson_mut_is_arr(val) { diff --git a/Sources/ReerJSON/Value.swift b/Sources/ReerJSON/Value.swift index 3af6876..2968026 100644 --- a/Sources/ReerJSON/Value.swift +++ b/Sources/ReerJSON/Value.swift @@ -24,16 +24,20 @@ import yyjson import Foundation -// MARK: - Document (Internal) +// MARK: - Document Storage (Internal) -/// A safe wrapper around a yyjson document. +/// Move-only owner for a yyjson document. /// -/// The document is immutable after creation and safe for concurrent reads. -internal final class Document: @unchecked Sendable { +/// The storage directly owns `yyjson_doc` and the optional in-place input +/// buffer. It is immutable after creation and safe for concurrent reads. +@usableFromInline +internal struct DocumentStorage: ~Copyable, @unchecked Sendable { + @usableFromInline let doc: UnsafeMutablePointer /// Retained data buffer (used when parsing consumes the input). - private var retainedData: Data? + @usableFromInline + let retainedData: Data? /// Creates a document by parsing JSON data. /// @@ -48,22 +52,17 @@ internal final class Document: @unchecked Sendable { // In-place parsing must use the dedicated consuming initializer. flags &= ~yyjson_read_flag(YYJSON_READ_INSITU) - self.retainedData = nil - if data.isEmpty { throw JSONError.invalidJSON("Empty content") } - let result = data.withUnsafeBytes { bytes -> UnsafeMutablePointer? in - guard let baseAddress = bytes.baseAddress else { return nil } - let ptr = UnsafeMutablePointer(mutating: baseAddress.assumingMemoryBound(to: CChar.self)) - return yyjson_read_opts(ptr, data.count, flags, nil, &error) - } + let result = yyReadDocument(from: data, flags: flags, error: &error) guard let doc = result else { throw JSONError(parsing: error) } self.doc = doc + self.retainedData = nil } /// Creates a document by consuming mutable data. @@ -92,38 +91,130 @@ internal final class Document: @unchecked Sendable { data.reserveCapacity(originalCount + paddingSize) data.append(contentsOf: repeatElement(0 as UInt8, count: paddingSize)) + let result = data.withUnsafeMutableBytes { bytes in + yyReadDocument( + mutating: bytes, + validByteCount: originalCount, + flags: flags, + error: &error + ) + } + + guard let doc = result else { + throw JSONError(parsing: error) + } + self.doc = doc self.retainedData = data + data.removeAll(keepingCapacity: false) + } + + deinit { + yyjson_doc_free(doc) + } + + @usableFromInline + @inline(__always) + var root: UnsafeMutablePointer? { + yyjson_doc_get_root(doc) + } +} - let result = self.retainedData!.withUnsafeMutableBytes { bytes -> UnsafeMutablePointer? in - let ptr = bytes.baseAddress?.assumingMemoryBound(to: CChar.self) - return yyjson_read_opts(ptr, originalCount, flags, nil, &error) +// MARK: - Document Reference (Internal) + +/// Reference owner for JSON values that are allowed to escape independently of +/// a `JSONDocument`. +/// +/// This keeps the copyable ``JSONValue`` API safe while letting +/// ``JSONDocument`` use move-only storage for borrowed traversal. +internal final class DocumentRef: @unchecked Sendable { + let doc: UnsafeMutablePointer + + private let retainedData: Data? + + init(data: Data, options: JSONReadOptions = .default) throws { + var error = yyjson_read_err() + var flags = options.yyjsonFlags + flags &= ~yyjson_read_flag(YYJSON_READ_INSITU) + + if data.isEmpty { + throw JSONError.invalidJSON("Empty content") } + let result = yyReadDocument(from: data, flags: flags, error: &error) + guard let doc = result else { throw JSONError(parsing: error) } self.doc = doc + self.retainedData = nil + } + + init(consuming data: inout Data, options: JSONReadOptions = .default) throws { + var error = yyjson_read_err() + var flags = options.yyjsonFlags + flags |= YYJSON_READ_INSITU + + if data.isEmpty { + throw JSONError.invalidJSON("Empty content") + } + + let paddingSize = Int(YYJSON_PADDING_SIZE) + let originalCount = data.count + + data.reserveCapacity(originalCount + paddingSize) + data.append(contentsOf: repeatElement(0 as UInt8, count: paddingSize)) + + let result = data.withUnsafeMutableBytes { bytes in + yyReadDocument( + mutating: bytes, + validByteCount: originalCount, + flags: flags, + error: &error + ) + } + + guard let doc = result else { + throw JSONError(parsing: error) + } + self.doc = doc + self.retainedData = data + data.removeAll(keepingCapacity: false) } - /// Result of a streaming parse attempt. enum StreamParseResult { - case success(Document, consumedBytes: Int) + case success(DocumentRef, consumedBytes: Int) case needMoreData } - /// Attempts to parse one JSON value from bytes with `STOP_WHEN_DONE`. - /// - /// The bytes do not need to be padded; for non-INSITU parsing yyjson - /// allocates and pads its own internal buffer. - /// - /// - Parameters: - /// - bytes: Pointer to the JSON bytes. - /// - count: Number of valid bytes. - /// - options: Options for reading the JSON. - /// - Returns: `.success` with consumed byte count, or `.needMoreData` if incomplete. - /// - Throws: `JSONError` for non-recoverable parse errors. static func streamParse( - bytes: UnsafePointer, count: Int, + bytes: Span, + options: JSONReadOptions + ) throws -> StreamParseResult { + guard !bytes.isEmpty else { return .needMoreData } + + var error = yyjson_read_err() + var flags = options.yyjsonFlags + flags |= YYJSON_READ_STOP_WHEN_DONE + flags &= ~yyjson_read_flag(YYJSON_READ_INSITU) + + let result = yyReadDocument(from: bytes.bytes, flags: flags, error: &error) + + if let doc = result { + let consumed = yyjson_doc_get_read_size(doc) + return .success(DocumentRef(alreadyParsed: doc), consumedBytes: consumed) + } + + if error.code == YYJSON_READ_ERROR_UNEXPECTED_END + || error.code == YYJSON_READ_ERROR_EMPTY_CONTENT { + return .needMoreData + } + + throw JSONError(parsing: error) + } + + static func streamParse( + bytes: UnsafePointer, + count: Int, options: JSONReadOptions ) throws -> StreamParseResult { guard count > 0 else { return .needMoreData } @@ -133,14 +224,12 @@ internal final class Document: @unchecked Sendable { flags |= YYJSON_READ_STOP_WHEN_DONE flags &= ~yyjson_read_flag(YYJSON_READ_INSITU) - let ptr = UnsafeMutablePointer( - mutating: UnsafeRawPointer(bytes).assumingMemoryBound(to: CChar.self) - ) - let result = yyjson_read_opts(ptr, count, flags, nil, &error) + let rawBuffer = UnsafeRawBufferPointer(start: bytes, count: count) + let result = yyReadDocument(from: rawBuffer, flags: flags, error: &error) if let doc = result { let consumed = yyjson_doc_get_read_size(doc) - return .success(Document(alreadyParsed: doc), consumedBytes: consumed) + return .success(DocumentRef(alreadyParsed: doc), consumedBytes: consumed) } if error.code == YYJSON_READ_ERROR_UNEXPECTED_END @@ -151,7 +240,6 @@ internal final class Document: @unchecked Sendable { throw JSONError(parsing: error) } - /// Adopts an already-parsed yyjson_doc, taking ownership. init(alreadyParsed doc: UnsafeMutablePointer) { self.doc = doc self.retainedData = nil @@ -201,8 +289,8 @@ internal final class IncrementalParser { } /// Result of an incremental read. - enum ReadResult { - case success(Document) + enum ReadResult: ~Copyable { + case success(DocumentStorage) case needMoreData } @@ -210,8 +298,8 @@ internal final class IncrementalParser { guard !buffer.isEmpty else { return .needMoreData } do { - let doc = try Document(data: buffer, options: options) - return .success(doc) + let storage = try DocumentStorage(data: buffer, options: options) + return .success(storage) } catch let error as JSONError { if error.readErrorCode == UInt32(YYJSON_READ_ERROR_UNEXPECTED_END) || error.readErrorCode == UInt32(YYJSON_READ_ERROR_EMPTY_CONTENT) { @@ -241,9 +329,14 @@ internal final class IncrementalParser { /// /// ```swift /// let document = try JSONDocument(data: jsonData) -/// if let root = document.root { -/// print(root["name"]?.string ?? "unknown") +/// let name = try document.withRootValue { root in +/// root.withObject { object in +/// object.withValue(forKey: "name") { value in +/// value.string ?? "unknown" +/// } ?? "unknown" +/// } ?? "unknown" /// } +/// print(name) /// ``` /// /// For highest performance with large documents, @@ -255,11 +348,12 @@ internal final class IncrementalParser { /// // `data` is now consumed and should not be used /// ``` public struct JSONDocument: ~Copyable, @unchecked Sendable { - internal let _document: Document + @usableFromInline + internal let _storage: DocumentStorage /// Creates a document from a pre-parsed internal document. - internal init(_document: Document) { - self._document = _document + internal init(_storage: consuming DocumentStorage) { + self._storage = _storage } /// Creates a document by parsing JSON data. @@ -269,7 +363,7 @@ public struct JSONDocument: ~Copyable, @unchecked Sendable { /// - options: Options for reading the JSON. /// - Throws: `JSONError` if parsing fails. public init(data: Data, options: JSONReadOptions = .default) throws { - self._document = try Document(data: data, options: options) + self._storage = try DocumentStorage(data: data, options: options) } /// Creates a document by parsing a JSON string. @@ -282,7 +376,7 @@ public struct JSONDocument: ~Copyable, @unchecked Sendable { guard let data = string.data(using: .utf8) else { throw JSONError.invalidJSON("Invalid UTF-8 string") } - self._document = try Document(data: data, options: options) + self._storage = try DocumentStorage(data: data, options: options) } /// Creates a document by parsing JSON data in place, @@ -300,29 +394,211 @@ public struct JSONDocument: ~Copyable, @unchecked Sendable { /// - options: Options for reading the JSON. /// - Throws: `JSONError` if parsing fails. public init(parsingInPlace data: inout Data, options: JSONReadOptions = .default) throws { - self._document = try Document(consuming: &data, options: options) + self._storage = try DocumentStorage(consuming: &data, options: options) } - /// The root value of the parsed JSON document. + /// Accesses the root value as a non-copyable borrowed view. /// - /// Returns `nil` if the document has no root value. - public var root: JSONValue? { - guard let root = _document.root else { - return nil + /// The borrowed view does not retain the underlying document, so traversing + /// through this API avoids ARC traffic from creating ``JSONValue`` wrappers. + @inlinable + @inline(__always) + public borrowing func withRootValue( + _ body: (borrowing JSONBorrowedValue) throws -> R + ) throws -> R { + guard let root = _storage.root else { + throw JSONError.invalidData("Document has no root value") + } + let value = JSONBorrowedValue(value: root) + return try body(value) + } +} + +// MARK: - Borrowed Value + +/// A non-copyable view of a JSON value borrowed from a ``JSONDocument``. +/// +/// Use ``JSONDocument/withRootValue(_:)`` to create borrowed views. They avoid +/// retaining the owning document while traversing a DOM tree. +public struct JSONBorrowedValue: ~Copyable, @unchecked Sendable { + @usableFromInline + let value: UnsafeMutablePointer? + + @usableFromInline + @inline(__always) + internal init(value: UnsafeMutablePointer?) { + self.value = value + } + + /// Whether this value is null. + @inlinable + @inline(__always) + public var isNull: Bool { + yyjson_is_null(value) + } + + /// Get the string value, or nil if not a string. + @inlinable + @inline(__always) + public var string: String? { + guard let cString = yyjson_get_str(value) else { return nil } + return String(cString: cString) + } + + /// Get the raw C string pointer, or nil if not a string. + @inlinable + @inline(__always) + public var cString: UnsafePointer? { + yyjson_get_str(value) + } + + /// The integer value as `Int64`, or `nil` if not stored as an integer. + @inlinable + @inline(__always) + public var int64: Int64? { + guard yyjson_is_int(value) else { return nil } + return yyjson_get_sint(value) + } + + /// The number value as `Double`, or `nil` if not a number. + @inlinable + @inline(__always) + public var number: Double? { + guard yyjson_is_num(value) else { return nil } + return yyjson_get_num(value) + } + + /// The Boolean value, or `nil` if not a Boolean. + @inlinable + @inline(__always) + public var bool: Bool? { + guard yyjson_is_bool(value) else { return nil } + return yyjson_get_bool(value) + } + + /// Borrows this value as an object. + @inlinable + @inline(__always) + public borrowing func withObject( + _ body: (borrowing JSONBorrowedObject) throws -> R + ) rethrows -> R? { + guard let value, yyjson_is_obj(value) else { return nil } + let object = JSONBorrowedObject(value: value) + return try body(object) + } + + /// Borrows this value as an array. + @inlinable + @inline(__always) + public borrowing func withArray( + _ body: (borrowing JSONBorrowedArray) throws -> R + ) rethrows -> R? { + guard let value, yyjson_is_arr(value) else { return nil } + let array = JSONBorrowedArray(value: value) + return try body(array) + } +} + +/// A non-copyable borrowed view of a JSON object. +public struct JSONBorrowedObject: ~Copyable, @unchecked Sendable { + @usableFromInline + let value: UnsafeMutablePointer + + @usableFromInline + @inline(__always) + internal init(value: UnsafeMutablePointer) { + self.value = value + } + + /// The number of key-value pairs in the object. + @inlinable + @inline(__always) + public var count: Int { + Int(yyjson_get_len(value)) + } + + /// Whether the object has no key-value pairs. + @inlinable + @inline(__always) + public var isEmpty: Bool { + count == 0 + } + + /// Borrows a value by key. + @inlinable + @inline(__always) + public borrowing func withValue( + forKey key: String, + _ body: (borrowing JSONBorrowedValue) throws -> R + ) rethrows -> R? { + guard let child = yyObjGet(value, key: key) else { return nil } + let borrowed = JSONBorrowedValue(value: child) + return try body(borrowed) + } + + /// Borrows each key-value pair in storage order. + @inlinable + @inline(__always) + public borrowing func forEach( + _ body: (String, borrowing JSONBorrowedValue) throws -> Void + ) rethrows { + var iterator = yyjson_obj_iter_with(value) + while let keyValue = yyjson_obj_iter_next(&iterator) { + guard let keyString = yyjson_get_str(keyValue) else { continue } + let child = JSONBorrowedValue(value: yyjson_obj_iter_get_val(keyValue)) + try body(String(cString: keyString), child) } - return JSONValue(value: root, document: _document) } +} + +/// A non-copyable borrowed view of a JSON array. +public struct JSONBorrowedArray: ~Copyable, @unchecked Sendable { + @usableFromInline + let value: UnsafeMutablePointer - /// The root value as an object, or `nil` if the root is not an object - /// or if the document has no root value. - public var rootObject: JSONObject? { - root?.object + @usableFromInline + @inline(__always) + internal init(value: UnsafeMutablePointer) { + self.value = value } - /// The root value as an array, or `nil` if the root is not an array - /// or if the document has no root value. - public var rootArray: JSONArray? { - root?.array + /// The number of elements in the array. + @inlinable + @inline(__always) + public var count: Int { + Int(yyjson_get_len(value)) + } + + /// Whether the array has no elements. + @inlinable + @inline(__always) + public var isEmpty: Bool { + count == 0 + } + + /// Borrows an element by index. + @inlinable + @inline(__always) + public borrowing func withElement( + at index: Int, + _ body: (borrowing JSONBorrowedValue) throws -> R + ) rethrows -> R? { + guard let child = yyjson_arr_get(value, index) else { return nil } + let borrowed = JSONBorrowedValue(value: child) + return try body(borrowed) + } + + /// Borrows each element in order. + @inlinable + @inline(__always) + public borrowing func forEach( + _ body: (borrowing JSONBorrowedValue) throws -> Void + ) rethrows { + var iterator = yyjson_arr_iter_with(value) + while let child = yyjson_arr_iter_next(&iterator) { + let borrowed = JSONBorrowedValue(value: child) + try body(borrowed) + } } } @@ -384,14 +660,14 @@ public struct JSONValue: @unchecked Sendable { } /// The document that owns this value (for lifetime management). - let document: Document + let document: DocumentRef /// Initializes from a yyjson value pointer. /// /// - Parameters: /// - value: The yyjson value pointer, or `nil` for null. /// - document: The document that owns this value (for lifetime management). - init(value: UnsafeMutablePointer?, document: Document) { + init(value: UnsafeMutablePointer?, document: DocumentRef) { self.document = document guard let val = value else { @@ -551,9 +827,9 @@ extension JSONValue: CustomStringConvertible { /// because the underlying yyjson document is immutable after parsing. public struct JSONObject: @unchecked Sendable { internal let value: UnsafeMutablePointer - internal let document: Document + internal let document: DocumentRef - internal init(value: UnsafeMutablePointer, document: Document) { + internal init(value: UnsafeMutablePointer, document: DocumentRef) { self.value = value self.document = document } @@ -610,10 +886,10 @@ extension JSONObject: Sequence { /// Iterator for JSON object key-value pairs. public struct JSONObjectIterator: IteratorProtocol { - private let document: Document + private let document: DocumentRef private var iterator: yyjson_obj_iter - internal init(value: UnsafeMutablePointer, document: Document) { + internal init(value: UnsafeMutablePointer, document: DocumentRef) { self.document = document self.iterator = yyjson_obj_iter_with(value) } @@ -651,9 +927,9 @@ extension JSONObject: CustomStringConvertible { /// because the underlying yyjson document is immutable after parsing. public struct JSONArray: @unchecked Sendable { internal let value: UnsafeMutablePointer - internal let document: Document + internal let document: DocumentRef - internal init(value: UnsafeMutablePointer, document: Document) { + internal init(value: UnsafeMutablePointer, document: DocumentRef) { self.value = value self.document = document } @@ -688,10 +964,10 @@ extension JSONArray: Sequence { /// Iterator for JSON array elements. public struct JSONArrayIterator: IteratorProtocol { - private let document: Document + private let document: DocumentRef private var iterator: yyjson_arr_iter - internal init(value: UnsafeMutablePointer, document: Document) { + internal init(value: UnsafeMutablePointer, document: DocumentRef) { self.document = document self.iterator = yyjson_arr_iter_with(value) } @@ -721,7 +997,7 @@ extension JSONValue { /// - options: Options for reading the JSON. /// - Throws: `JSONError` if parsing fails. public init(data: Data, options: JSONReadOptions = .default) throws { - let document = try Document(data: data, options: options) + let document = try DocumentRef(data: data, options: options) guard let root = document.root else { throw JSONError.invalidData("Document has no root value") } @@ -776,7 +1052,7 @@ extension JSONValue { public static func parseInPlace(consuming data: inout Data, options: JSONReadOptions = .default) throws -> JSONValue { - let document = try Document(consuming: &data, options: options) + let document = try DocumentRef(consuming: &data, options: options) guard let root = document.root else { throw JSONError.invalidData("Document has no root value") } diff --git a/Tests/ReerJSONTests/JSONTests.swift b/Tests/ReerJSONTests/JSONTests.swift index bcab273..848f76c 100644 --- a/Tests/ReerJSONTests/JSONTests.swift +++ b/Tests/ReerJSONTests/JSONTests.swift @@ -433,7 +433,7 @@ final class JSONTests: XCTestCase { } - static var allTests = [ + static let allTests = [ ("testObject", testObject), ("testArray", testArray), ("testEscaping", testEscaping), diff --git a/Tests/ReerJSONTests/StdlibUnittest.swift b/Tests/ReerJSONTests/StdlibUnittest.swift index ea5c64a..875760d 100644 --- a/Tests/ReerJSONTests/StdlibUnittest.swift +++ b/Tests/ReerJSONTests/StdlibUnittest.swift @@ -125,8 +125,8 @@ func _printStackTrace(_ stackTrace: SourceLocStack?) { } } -fileprivate var _anyExpectFailed = AtomicBool(false) -fileprivate var _seenExpectCrash = AtomicBool(false) +nonisolated(unsafe) fileprivate var _anyExpectFailed = AtomicBool(false) +nonisolated(unsafe) fileprivate var _seenExpectCrash = AtomicBool(false) /// Run `body` and expect a failure to happen. /// @@ -699,7 +699,7 @@ func _defaultTestSuiteFailedCallback() { abort() } -var _testSuiteFailedCallback: () -> Void = _defaultTestSuiteFailedCallback +nonisolated(unsafe) var _testSuiteFailedCallback: () -> Void = _defaultTestSuiteFailedCallback public func _setTestSuiteFailedCallback(_ callback: @escaping () -> Void) { _testSuiteFailedCallback = callback @@ -709,7 +709,7 @@ func _defaultTrappingExpectationFailedCallback() { abort() } -var _trappingExpectationFailedCallback: () -> Void +nonisolated(unsafe) var _trappingExpectationFailedCallback: () -> Void = _defaultTrappingExpectationFailedCallback public func _setTrappingExpectationFailedCallback(callback: @escaping () -> Void) { @@ -834,8 +834,8 @@ func _getOSVersion() -> OSVersion { #endif } -var _runningOSVersion: OSVersion = _getOSVersion() -var _overrideOSVersion: OSVersion? +nonisolated(unsafe) var _runningOSVersion: OSVersion = _getOSVersion() +nonisolated(unsafe) var _overrideOSVersion: OSVersion? /// Override the OS version for testing. public func _setOverrideOSVersion(_ v: OSVersion) { diff --git a/Tests/ReerJSONTests/StreamParserTests.swift b/Tests/ReerJSONTests/StreamParserTests.swift index fcec454..bed3636 100644 --- a/Tests/ReerJSONTests/StreamParserTests.swift +++ b/Tests/ReerJSONTests/StreamParserTests.swift @@ -345,7 +345,14 @@ final class JSONIncrementalReaderTests: XCTestCase { func testSingleChunk() throws { let reader = try JSONIncrementalReader(data: Data("{\"key\":\"value\"}".utf8)) let doc = try reader.finish() - XCTAssertEqual(doc.root?["key"]?.string, "value") + let value = try doc.withRootValue { root in + root.withObject { object in + object.withValue(forKey: "key") { value in + value.string ?? "" + } ?? "" + } ?? "" + } + XCTAssertEqual(value, "value") } func testMultipleChunks() throws { @@ -353,14 +360,22 @@ final class JSONIncrementalReaderTests: XCTestCase { // First feed should need more data. switch try reader.feed(Data("y\":\"val".utf8)) { case .ready(let doc): - XCTFail("Should need more data, got doc with root: \(String(describing: doc.root))") + let isNull = try doc.withRootValue { root in root.isNull } + XCTFail("Should need more data, got doc; root is null: \(isNull)") case .needMoreData: break } // Second feed should complete. switch try reader.feed(Data("ue\"}".utf8)) { case .ready(let doc): - XCTAssertEqual(doc.root?["key"]?.string, "value") + let value = try doc.withRootValue { root in + root.withObject { object in + object.withValue(forKey: "key") { value in + value.string ?? "" + } ?? "" + } ?? "" + } + XCTAssertEqual(value, "value") case .needMoreData: XCTFail("Should have completed parsing") } @@ -384,7 +399,12 @@ final class JSONIncrementalReaderTests: XCTestCase { let chunk = Data(jsonData[offset.. String? { + try withRootValue { root in + root.withObject { object in + object.withValue(forKey: key) { value in + value.string ?? "" + } + } ?? nil + } + } + + borrowing func objectNumber(forKey key: String) throws -> Double? { + try withRootValue { root in + root.withObject { object in + object.withValue(forKey: key) { value in + value.number ?? .nan + } + } ?? nil + } + } + + borrowing func rootArrayCount() throws -> Int? { + try withRootValue { root in + root.withArray { array in + array.count + } + } + } + + borrowing func rootArrayNumber(at index: Int) throws -> Double? { + try withRootValue { root in + root.withArray { array in + array.withElement(at: index) { value in + value.number ?? .nan + } + } ?? nil + } + } +} + class JSONDocumentInitTests: XCTestCase { func testInitFromData() throws { let json = #"{"name": "Alice", "age": 30}"# let data = json.data(using: .utf8)! let document = try JSONDocument(data: data) - XCTAssertNotNil(document.root) - XCTAssertEqual(document.root?["name"]?.string, "Alice") - XCTAssertEqual(document.root?["age"]?.number, 30.0) + XCTAssertEqual(try document.objectString(forKey: "name"), "Alice") + XCTAssertEqual(try document.objectNumber(forKey: "age"), 30.0) } func testInitFromString() throws { let json = #"{"key": "value"}"# let document = try JSONDocument(string: json) - XCTAssertNotNil(document.root) - XCTAssertEqual(document.root?["key"]?.string, "value") + XCTAssertEqual(try document.objectString(forKey: "key"), "value") } func testInitFromDataWithOptions() throws { let json = #"{"key": "value"}"# let data = json.data(using: .utf8)! let document = try JSONDocument(data: data, options: .default) - XCTAssertNotNil(document.root) - XCTAssertEqual(document.root?["key"]?.string, "value") + XCTAssertEqual(try document.objectString(forKey: "key"), "value") } func testInitFromStringWithOptions() throws { let json = #"{"key": "value"}"# let document = try JSONDocument(string: json, options: .default) - XCTAssertNotNil(document.root) - XCTAssertEqual(document.root?["key"]?.string, "value") + XCTAssertEqual(try document.objectString(forKey: "key"), "value") } func testInitFromEmptyData() throws { @@ -803,9 +840,8 @@ class JSONDocumentInitTests: XCTestCase { let json = #"{"name": "test", "value": 42}"# var data = json.data(using: .utf8)! let document = try JSONDocument(parsingInPlace: &data) - XCTAssertNotNil(document.root) - XCTAssertEqual(document.root?["name"]?.string, "test") - XCTAssertEqual(document.root?["value"]?.number, 42.0) + XCTAssertEqual(try document.objectString(forKey: "name"), "test") + XCTAssertEqual(try document.objectNumber(forKey: "value"), 42.0) } func testParsingInPlaceEmptyData() throws { @@ -830,59 +866,127 @@ class JSONDocumentInitTests: XCTestCase { } class JSONDocumentRootTests: XCTestCase { - func testRootProperty() throws { + func testBorrowedRootLookup() throws { let document = try JSONDocument(string: #"{"key": "value"}"#) - XCTAssertNotNil(document.root) - XCTAssertEqual(document.root?["key"]?.string, "value") + XCTAssertEqual(try document.objectString(forKey: "key"), "value") } - func testRootObjectProperty() throws { + func testBorrowedRootObject() throws { let document = try JSONDocument(string: #"{"key": "value"}"#) - XCTAssertNotNil(document.rootObject) - XCTAssertEqual(document.rootObject?["key"]?.string, "value") + let isObject = try document.withRootValue { root in + root.withObject { _ in true } ?? false + } + XCTAssertTrue(isObject) + XCTAssertEqual(try document.objectString(forKey: "key"), "value") } - func testRootArrayProperty() throws { + func testBorrowedRootArray() throws { let document = try JSONDocument(string: "[1, 2, 3]") - XCTAssertNotNil(document.rootArray) - XCTAssertEqual(document.rootArray?.count, 3) - XCTAssertEqual(document.rootArray?[0]?.number, 1.0) + XCTAssertEqual(try document.rootArrayCount(), 3) + XCTAssertEqual(try document.rootArrayNumber(at: 0), 1.0) } func testRootObjectOnArray() throws { let document = try JSONDocument(string: "[1, 2, 3]") - XCTAssertNil(document.rootObject) + let isObject = try document.withRootValue { root in + root.withObject { _ in true } ?? false + } + XCTAssertFalse(isObject) } func testRootArrayOnObject() throws { let document = try JSONDocument(string: #"{"key": "value"}"#) - XCTAssertNil(document.rootArray) + XCTAssertNil(try document.rootArrayCount()) } func testRootOnPrimitive() throws { let document = try JSONDocument(string: "42") - XCTAssertNotNil(document.root) - XCTAssertEqual(document.root?.number, 42.0) - XCTAssertNil(document.rootObject) - XCTAssertNil(document.rootArray) + let number = try document.withRootValue { root in + root.number + } + let isObject = try document.withRootValue { root in + root.withObject { _ in true } ?? false + } + XCTAssertEqual(number, 42.0) + XCTAssertFalse(isObject) + XCTAssertNil(try document.rootArrayCount()) } func testRootOnString() throws { let document = try JSONDocument(string: #""hello""#) - XCTAssertNotNil(document.root) - XCTAssertEqual(document.root?.string, "hello") + let string = try document.withRootValue { root in + root.string + } + XCTAssertEqual(string, "hello") } func testRootOnNull() throws { let document = try JSONDocument(string: "null") - XCTAssertNotNil(document.root) - XCTAssertEqual(document.root?.isNull, true) + let isNull = try document.withRootValue { root in + root.isNull + } + XCTAssertTrue(isNull) } func testRootOnBool() throws { let document = try JSONDocument(string: "true") - XCTAssertNotNil(document.root) - XCTAssertEqual(document.root?.bool, true) + let bool = try document.withRootValue { root in + root.bool + } + XCTAssertEqual(bool, true) + } + + func testBorrowedRootObjectLookup() throws { + let document = try JSONDocument(string: #"{"name":"Alice","age":30}"#) + + let name = try document.withRootValue { root in + root.withObject { object in + object.withValue(forKey: "name") { value in + value.string ?? "" + } ?? "" + } ?? "" + } + + let age = try document.withRootValue { root in + root.withObject { object in + object.withValue(forKey: "age") { value in + value.int64 ?? 0 + } ?? 0 + } ?? 0 + } + + XCTAssertEqual(name, "Alice") + XCTAssertEqual(age, 30) + } + + func testBorrowedArrayIteration() throws { + let document = try JSONDocument(string: "[1,2,3]") + var values: [Int64] = [] + + try document.withRootValue { root in + _ = root.withArray { array in + XCTAssertEqual(array.count, 3) + array.forEach { value in + if let int = value.int64 { + values.append(int) + } + } + } + } + + XCTAssertEqual(values, [1, 2, 3]) + } + + func testDocumentStorageBorrowedRootValue() throws { + let document = try JSONDocument(string: #"{"enabled":true}"#) + let enabled = try document.withRootValue { root in + root.withObject { object in + object.withValue(forKey: "enabled") { value in + value.bool ?? false + } ?? false + } ?? false + } + XCTAssertTrue(enabled) } } @@ -898,18 +1002,51 @@ class JSONDocumentComplexTests: XCTestCase { } """ let document = try JSONDocument(string: json) - guard let root = document.root else { - XCTFail("Expected root") - return - } - guard let users = root["users"]?.array else { - XCTFail("Expected users array") - return + try document.withRootValue { root in + try root.withObject { object in + let usersCount = try object.withValue(forKey: "users") { usersValue in + usersValue.withArray { users in + users.count + } ?? 0 + } ?? 0 + XCTAssertEqual(usersCount, 2) + + let firstName = try object.withValue(forKey: "users") { usersValue in + try usersValue.withArray { users in + try users.withElement(at: 0) { user in + user.withObject { userObject in + userObject.withValue(forKey: "name") { name in + name.string ?? "" + } ?? "" + } ?? "" + } ?? "" + } ?? "" + } ?? "" + XCTAssertEqual(firstName, "Alice") + + let secondName = try object.withValue(forKey: "users") { usersValue in + try usersValue.withArray { users in + try users.withElement(at: 1) { user in + user.withObject { userObject in + userObject.withValue(forKey: "name") { name in + name.string ?? "" + } ?? "" + } ?? "" + } ?? "" + } ?? "" + } ?? "" + XCTAssertEqual(secondName, "Bob") + + let count = try object.withValue(forKey: "meta") { meta in + meta.withObject { metaObject in + metaObject.withValue(forKey: "count") { count in + count.number ?? .nan + } ?? .nan + } ?? .nan + } ?? .nan + XCTAssertEqual(count, 2.0) + } } - XCTAssertEqual(users.count, 2) - XCTAssertEqual(users[0]?["name"]?.string, "Alice") - XCTAssertEqual(users[1]?["name"]?.string, "Bob") - XCTAssertEqual(root["meta"]?["count"]?.number, 2.0) } func testLargeDocument() throws { @@ -919,13 +1056,9 @@ class JSONDocumentComplexTests: XCTestCase { } let json = "[\(elements.joined(separator: ", "))]" let document = try JSONDocument(string: json) - guard let array = document.rootArray else { - XCTFail("Expected array") - return - } - XCTAssertEqual(array.count, 100) - XCTAssertEqual(array[0]?.number, 0.0) - XCTAssertEqual(array[99]?.number, 99.0) + XCTAssertEqual(try document.rootArrayCount(), 100) + XCTAssertEqual(try document.rootArrayNumber(at: 0), 0.0) + XCTAssertEqual(try document.rootArrayNumber(at: 99), 99.0) } }