forked from rarestype/swift-json
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON.Object (ext).swift
More file actions
40 lines (39 loc) · 1.37 KB
/
JSON.Object (ext).swift
File metadata and controls
40 lines (39 loc) · 1.37 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
import Grammar
import JSONAST
extension JSON.Object {
/// Attempts to parse a JSON object from raw UTF-8 JSON data.
///
/// > Note:
/// Unlike BSON lists, you cannot reparse JSON arrays as objects.
public init(parsing json: JSON) throws {
self.init(try JSON.NodeRule<Int>.Object.parse(json.utf8))
}
/// Attempts to parse a JSON object from a raw span.
public init(parsing span: RawSpan) throws {
self.init(
try span.withUnsafeBytes { buffer in
try JSON.NodeRule<Int>.Object.parse(buffer)
}
)
}
/// Attempts to parse a JSON object from a string.
///
/// > Note:
/// Unlike BSON lists, you cannot reparse JSON arrays as objects.
public init(parsing string: String) throws {
self.init(try JSON.NodeRule<String.Index>.Object.parse(string.utf8))
}
/// Attempts to parse a JSON object from a substring.
///
/// > Note:
/// Unlike BSON lists, you cannot reparse JSON arrays as objects.
public init(parsing string: Substring) throws {
self.init(try JSON.NodeRule<String.Index>.Object.parse(string.utf8))
}
}
extension JSON.Object: LosslessStringConvertible {
/// See ``init(parsing:) (String)``.
public init?(_ description: String) {
do { try self.init(parsing: description) } catch { return nil }
}
}