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
4 changes: 2 additions & 2 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 6 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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: [
Expand Down
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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,
Expand All @@ -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.

Expand Down Expand Up @@ -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

Expand Down
64 changes: 10 additions & 54 deletions Sources/ReerJSON/JSONDecoderImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: DecodableWithConfiguration>(
_ value: JSON,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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<T: Decodable>(_ type: T.Type) throws -> T {
return try unbox(topValue, as: type, for: codingPathNode, _CodingKey?.none)
Expand Down Expand Up @@ -834,13 +802,11 @@ private final class DefaultKeyedDecodingContainer<K: CodingKey>: 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 {
Expand Down Expand Up @@ -902,13 +868,11 @@ private final class DefaultKeyedDecodingContainer<K: CodingKey>: 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 {
Expand Down Expand Up @@ -1207,13 +1171,11 @@ private final class PreTransformKeyedDecodingContainer<K: CodingKey>: 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 {
Expand Down Expand Up @@ -1275,13 +1237,11 @@ private final class PreTransformKeyedDecodingContainer<K: CodingKey>: 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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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<T: Decodable>(_ type: T.Type) throws -> T {
let value = try valueFromIterator(ofType: type)
Expand Down
48 changes: 44 additions & 4 deletions Sources/ReerJSON/JSONEncoderImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<yyjson_mut_val> {
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<Int>.size == MemoryLayout<Int64>.size,
MemoryLayout<Int>.alignment == MemoryLayout<Int64>.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<yyjson_mut_val>?
values.withUnsafeMutableBufferPointer { pointer, initializedCount in
for index in 0..<buffer.count {
pointer[index] = Int64(buffer[index])
}
initializedCount = buffer.count
result = yyjson_mut_arr_with_sint64(doc, pointer.baseAddress, initializedCount)
}
return result!
}
}
}
@inline(__always) func wrapBulkUInt8Array(_ arr: [UInt8]) -> UnsafeMutablePointer<yyjson_mut_val> {
arr.withUnsafeBufferPointer { yyjson_mut_arr_with_uint8(doc, $0.baseAddress, $0.count) }
Expand All @@ -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<yyjson_mut_val> {
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<UInt>.size == MemoryLayout<UInt64>.size,
MemoryLayout<UInt>.alignment == MemoryLayout<UInt64>.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<yyjson_mut_val>?
values.withUnsafeMutableBufferPointer { pointer, initializedCount in
for index in 0..<buffer.count {
pointer[index] = UInt64(buffer[index])
}
initializedCount = buffer.count
result = yyjson_mut_arr_with_uint64(doc, pointer.baseAddress, initializedCount)
}
return result!
}
}
}
@inline(__always) func wrapBulkStringArray(_ arr: [String]) -> UnsafeMutablePointer<yyjson_mut_val> {
let result = yyjson_mut_arr(doc)!
Expand Down
16 changes: 2 additions & 14 deletions Sources/ReerJSON/ReerJSONDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 21 additions & 15 deletions Sources/ReerJSON/ReerJSONEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,27 +293,33 @@ open class ReerJSONEncoder {

private func sortObjectKeys(_ val: UnsafeMutablePointer<yyjson_mut_val>) {
typealias MutVal = UnsafeMutablePointer<yyjson_mut_val>
typealias Pair = (keyVal: MutVal, val: MutVal, keyStr: UnsafePointer<CChar>)

if yyjson_mut_is_obj(val) {
var pairs: [(keyVal: MutVal, val: MutVal, keyStr: UnsafePointer<CChar>)] = []
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()
Expand Down
Loading
Loading