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
13 changes: 11 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ let package = Package(
.library(
name: "IkigaJSON",
targets: ["IkigaJSON"]
)
),
// Embedded Swift compatible library - no Foundation or NIO dependencies
.library(
name: "IkigaJSONCore",
targets: ["_JSONCore"]
),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
Expand All @@ -24,7 +29,11 @@ let package = Package(
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "_JSONCore"
name: "_JSONCore",
swiftSettings: [
// Enable strict concurrency for Embedded Swift compatibility
.enableExperimentalFeature("StrictConcurrency"),
]
),
.target(
name: "_NIOJSON",
Expand Down
8 changes: 3 additions & 5 deletions Sources/_JSONCore/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,11 @@ public enum JSONParserError: Error, Sendable {
case unexpectedToken(line: Int, column: Int, token: UInt8, reason: Reason)
}

public struct TypeConversionError<F: FixedWidthInteger & Sendable>: Error {
let from: F
let to: Any.Type
public struct TypeConversionError<F: FixedWidthInteger & Sendable, T>: Error, Sendable {
public let from: F

public init(from: F, to: Any.Type) {
public init(from: F, to: T.Type) {
self.from = from
self.to = to
}
}

Expand Down
64 changes: 63 additions & 1 deletion Sources/_JSONCore/Parser/JSONParser+Parsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ extension JSONTokenizer {
}

try skipWhitespace() // needed because of the comma
try scanStringLiteral()
try scanObjectKey()
try skipWhitespace()

guard nextByte() == .colon else {
Expand Down Expand Up @@ -280,4 +280,66 @@ extension JSONTokenizer {

throw JSONParserError.missingData(line: line, column: column)
}

/// Scans an object key (string) and computes its hash for fast lookup
@_optimize(speed)
@inlinable
mutating func scanObjectKey() throws(JSONParserError) {
if currentByte != .quote {
throw JSONParserError.unexpectedToken(line: line, column: column, token: currentByte, reason: .expectedObjectKey)
}

var currentIndex: Int = 1
var didEscape = false
defer { advance(currentIndex) }

while currentIndex < count {
defer { currentIndex = currentIndex &+ 1 }

let byte = self[currentIndex]

if byte == .quote {
var escaped = false

if didEscape {
var backwardsOffset = currentIndex &- 1

escapeLoop: while backwardsOffset >= 1 {
defer { backwardsOffset = backwardsOffset &- 1 }

if self[backwardsOffset] == .backslash {
escaped = !escaped
} else {
break escapeLoop
}
}
}

if !escaped {
let start = JSONSourcePosition(byteIndex: currentOffset)
let stringToken = JSONToken.String(
start: start,
byteLength: currentIndex &+ 1,
usesEscaping: didEscape
)

// Compute hash of key bytes (excluding quotes)
// Key content starts at offset 1 (after opening quote)
// and ends just before `currentIndex` (which points to the closing quote).
var hash: UInt32 = 2166136261 // FNV offset basis
for i in 1..<currentIndex {
hash ^= UInt32(self[i])
hash &*= 16777619 // FNV prime
}

destination.objectKeyFound(stringToken, hash: hash)
return
}
} else if byte == .backslash {
didEscape = true
}
}

throw JSONParserError.missingData(line: line, column: column)
}
}
11 changes: 11 additions & 0 deletions Sources/_JSONCore/Parser/JSONTokenizerDestination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ public protocol JSONTokenizerDestination {
mutating func nullFound(_ null: JSONToken.Null)
mutating func stringFound(_ string: JSONToken.String)
mutating func numberFound(_ number: JSONToken.Number)

/// Called when an object key is found, with a pre-computed hash of the key bytes.
/// Default implementation calls `stringFound()` for backward compatibility.
mutating func objectKeyFound(_ string: JSONToken.String, hash: UInt32)
}

extension JSONTokenizerDestination {
@inlinable
public mutating func objectKeyFound(_ string: JSONToken.String, hash: UInt32) {
stringFound(string)
}
}

public enum JSONToken: Sendable, Hashable {
Expand Down
Loading