Skip to content

Commit 06c8b76

Browse files
authored
Merge pull request #107 from t089/span
Adds initializers to support parsing JSON directly from a Span.
2 parents 294b159 + 50acf3e commit 06c8b76

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Sources/JSONParsing/JSON.Array (ext).swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ extension JSON.Array {
66
public init(parsing json: JSON) throws {
77
self.init(try JSON.NodeRule<Int>.Array.parse(json.utf8))
88
}
9+
/// Attempts to parse a JSON array from a raw span
10+
public init(parsing span: RawSpan) throws {
11+
self.init(
12+
try span.withUnsafeBytes { buffer in
13+
try JSON.NodeRule<Int>.Array.parse(buffer)
14+
}
15+
)
16+
}
917
/// Attempts to parse a JSON array from a string.
1018
public init(parsing string: String) throws {
1119
self.init(try JSON.NodeRule<String.Index>.Array.parse(string.utf8))

Sources/JSONParsing/JSON.Node (ext).swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ extension JSON.Node {
88
self = try JSON.RootRule<Int>.parse(json.utf8)
99
}
1010
/// Attempts to parse a complete JSON message (either an ``Array`` or an
11+
/// ``Object``) from a raw span.
12+
public init(parsing span: RawSpan) throws {
13+
self = try span.withUnsafeBytes { buffer in
14+
try JSON.RootRule<Int>.parse(buffer)
15+
}
16+
}
17+
/// Attempts to parse a complete JSON message (either an ``Array`` or an
1118
/// ``Object``) from a string.
1219
public init(parsing string: String) throws {
1320
self = try JSON.RootRule<String.Index>.parse(string.utf8)

Sources/JSONParsing/JSON.Object (ext).swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ extension JSON.Object {
99
public init(parsing json: JSON) throws {
1010
self.init(try JSON.NodeRule<Int>.Object.parse(json.utf8))
1111
}
12+
/// Attempts to parse a JSON object from a raw span.
13+
public init(parsing span: RawSpan) throws {
14+
self.init(
15+
try span.withUnsafeBytes { buffer in
16+
try JSON.NodeRule<Int>.Object.parse(buffer)
17+
}
18+
)
19+
}
1220
/// Attempts to parse a JSON object from a string.
1321
///
1422
/// > Note:

0 commit comments

Comments
 (0)