Skip to content

Performance refactoring #1063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update collection conformance
  • Loading branch information
Alexander Antonov committed May 21, 2020
commit 228cd3e5529fc7d9aed2d1de2e462575b9690e9c
38 changes: 24 additions & 14 deletions Source/SwiftyJSON/SwiftyJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,34 +328,44 @@ extension JSON: Swift.Collection {
public typealias Index = JSONRawIndex

public var startIndex: Index {
switch type {
case .array: return .array(rawArray.startIndex)
case .dictionary: return .dictionary(rawDictionary.startIndex)
switch content {
case .array(let rawArray): return .array(rawArray.startIndex)
case .dictionary(let rawDictionary): return .dictionary(rawDictionary.startIndex)
default: return .null
}
}

public var endIndex: Index {
switch type {
case .array: return .array(rawArray.endIndex)
case .dictionary: return .dictionary(rawDictionary.endIndex)
switch content {
case .array(let rawArray): return .array(rawArray.endIndex)
case .dictionary(let rawDictionary): return .dictionary(rawDictionary.endIndex)
default: return .null
}
}

public func index(after i: Index) -> Index {
switch i {
case .array(let idx): return .array(rawArray.index(after: idx))
case .dictionary(let idx): return .dictionary(rawDictionary.index(after: idx))
default: return .null
switch content {
case .array(let rawArray):
guard case .array(let idx) = i else { return .null }
return .array(rawArray.index(after: idx))
case .dictionary(let rawDictionary):
guard case .dictionary(let idx) = i else { return .null }
return .dictionary(rawDictionary.index(after: idx))
default:
return .null
}
}

public subscript (position: Index) -> (String, JSON) {
switch position {
case .array(let idx): return (String(idx), JSON(rawArray[idx]))
case .dictionary(let idx): return (rawDictionary[idx].key, JSON(rawDictionary[idx].value))
default: return ("", JSON.null)
switch content {
case .array(let rawArray):
guard case .array(let idx) = position else { return ("", JSON.null) }
return (String(idx), JSON(rawArray[idx]))
case .dictionary(let rawDictionary):
guard case .dictionary(let idx) = position else { return ("", JSON.null) }
return (rawDictionary[idx].key, JSON(rawDictionary[idx].value))
default:
return ("", JSON.null)
}
}
}
Expand Down