-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
All JSON loading done by JSON scanner
- Loading branch information
1 parent
64b798f
commit 6df4b72
Showing
14 changed files
with
148 additions
and
598 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/// Array storage for JMES | ||
typealias JMESArray = [Any] | ||
|
||
extension JMESArray { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import Foundation | ||
|
||
/// JMESExpression extensions for Data | ||
extension JMESExpression { | ||
/// Search JSON | ||
/// | ||
/// - Parameters: | ||
/// - any: JSON to search | ||
/// - as: Swift type to return | ||
/// - runtime: JMES runtime (includes functions) | ||
/// - Throws: JMESPathError | ||
/// - Returns: Search result | ||
public func search<Value>(json: Data, as: Value.Type = Value.self, runtime: JMESRuntime = .init()) throws -> Value { | ||
let searchResult = try self.search(json: json, runtime: runtime) | ||
guard let value = searchResult as? Value else { | ||
throw JMESPathError.runtime("Expected \(Value.self)) but got a \(type(of: searchResult))") | ||
} | ||
return value | ||
} | ||
|
||
/// Search JSON | ||
/// | ||
/// - Parameters: | ||
/// - any: JSON to search | ||
/// - runtime: JMES runtime (includes functions) | ||
/// - Throws: JMESPathError | ||
/// - Returns: Search result | ||
public func search(json: Data, runtime: JMESRuntime = .init()) throws -> Any? { | ||
let value = try JMESJSON.parse(json: json) | ||
return try runtime.interpret(JMESVariable(from: value), ast: self.ast).collapse() | ||
} | ||
} | ||
|
||
/// Parse json in the form of Data | ||
extension JMESJSON { | ||
static func parse(json: Data) throws -> Any { | ||
try json.withBufferView { view in | ||
var scanner = JSONScanner(bytes: view, options: .init()) | ||
let map = try scanner.scan() | ||
guard let value = map.loadValue(at: 0) else { throw JMESPathError.runtime("Empty JSON file") } | ||
return try JMESJSONVariable(value: value).getValue(map) | ||
} | ||
} | ||
} |
Oops, something went wrong.