forked from rarestype/swift-json
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON.Node (ext).swift
More file actions
42 lines (40 loc) · 1.61 KB
/
JSON.Node (ext).swift
File metadata and controls
42 lines (40 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import Grammar
import JSONAST
extension JSON.Node {
/// Attempts to parse a complete JSON message (either an ``Array`` or an
/// ``Object``) from raw UTF-8 JSON data.
public init(parsing json: JSON) throws {
self = try JSON.RootRule<Int>.parse(json.utf8)
}
/// Attempts to parse a complete JSON message (either an ``Array`` or an
/// ``Object``) from a raw span.
public init(parsing span: RawSpan) throws {
self = try span.withUnsafeBytes { buffer in
try JSON.RootRule<Int>.parse(buffer)
}
}
/// Attempts to parse a complete JSON message (either an ``Array`` or an
/// ``Object``) from a string.
public init(parsing string: String) throws {
self = try JSON.RootRule<String.Index>.parse(string.utf8)
}
/// Attempts to parse a complete JSON message (either an ``Array`` or an
/// ``Object``) from a substring.
public init(parsing string: Substring) throws {
self = try JSON.RootRule<String.Index>.parse(string.utf8)
}
/// Attempts to parse a a JSON fragment from a string.
public init(parsingFragment string: String) throws {
self = try JSON.NodeRule<String.Index>.parse(string.utf8)
}
/// Attempts to parse a a JSON fragment from a substring.
public init(parsingFragment string: Substring) throws {
self = try JSON.NodeRule<String.Index>.parse(string.utf8)
}
}
extension JSON.Node: LosslessStringConvertible {
/// See ``init(parsingFragment:) (String)``.
public init?(_ description: String) {
do { try self.init(parsingFragment: description) } catch { return nil }
}
}