@@ -20,64 +20,24 @@ extension JSON {
2020 }
2121}
2222extension JSON . Node {
23- // TODO: optimize this, it should operate at the utf8 level, and be @inlinable
23+ /// A shorthand for constructing the payload of ``string(_:) [case]``.
24+ @inlinable public static func string( _ unescaped: consuming String ) -> Self {
25+ . string( JSON . Literal< String> . init( unescaped) )
26+ }
2427
25- /// Escapes and formats a string as a JSON string literal, including the
26- /// beginning and ending quote characters. This function is used for debug reflection only;
27- /// it is less efficient than the UTF-8 escaping implementation used by the encoder.
28- ///
29- /// - Parameters:
30- /// - string: A string to escape.
31- /// - Returns: A string literal, which includes the `""` delimiters.
32- ///
33- /// This function escapes the following characters: `"`, `\`, `\b`, `\t`, `\n`,
34- /// `\f`, and `\r`. It does not escape forward slashes (`/`).
35- ///
36- /// JSON string literals may contain unicode characters, even after escaping.
37- /// Do not assume the output of this function is ASCII.
38- ///
39- /// > Important: This function should *not* be called on an input to the ``string(_:)``
40- /// case constructor. The library performs string escaping lazily; calling this
41- /// function explicitly will double-escape the input.
42- // static
43- // func escape<S>(_ string:S) -> String where S:StringProtocol
44- // {
45- // var escaped:String = "\""
46- // for character:Character in string
47- // {
48- // switch character
49- // {
50- // case "\"": escaped += "\\\""
51- // case "\\": escaped += "\\\\"
52- // // slash escape is not mandatory, and does not improve legibility
53- // // case "/": escaped += "\\/"
54- // case "\u{08}": escaped += "\\b"
55- // case "\u{09}": escaped += "\\t"
56- // case "\u{0A}": escaped += "\\n"
57- // case "\u{0C}": escaped += "\\f"
58- // case "\u{0D}": escaped += "\\r"
59- // default: escaped.append(character)
60- // }
61- // }
62- // escaped += "\""
63- // return escaped
64- // }
65- }
66- extension JSON . Node : CustomStringConvertible {
67- /// Returns this value serialized as a minified string.
68- ///
69- /// Reparsing and reserializing this string is guaranteed to return the
70- /// same string.
71- public var description : String {
72- switch self {
73- case . null: " null "
74- case . bool( true ) : " true "
75- case . bool( false ) : " false "
76- case . string( let self) : . init( self )
77- case . number( let self) : " \( self ) "
78- case . array( let self) : " \( self ) "
79- case . object( let self) : " \( self ) "
80- }
28+ /// A shorthand for constructing the payload of ``number(_:) [case]``.
29+ @inlinable public static func number< T> ( _ value: T ) -> Self where T: UnsignedInteger {
30+ . number( JSON . Number. init ( value) )
31+ }
32+ /// A shorthand for constructing the payload of ``number(_:) [case]``.
33+ @inlinable public static func number< T> ( _ value: T ) -> Self where T: SignedInteger {
34+ . number( JSON . Number. init ( value) )
35+ }
36+ /// A shorthand for constructing the payload of ``number(_:) [case]``.
37+ @inlinable public static func number< T> (
38+ _ value: T
39+ ) -> Self where T: BinaryFloatingPoint & LosslessStringConvertible {
40+ . number( JSON . Number. init ( value) )
8141 }
8242}
8343extension JSON . Node : ExpressibleByDictionaryLiteral {
@@ -90,17 +50,33 @@ extension JSON.Node: ExpressibleByArrayLiteral {
9050 self = . array( . init( arrayLiteral) )
9151 }
9252}
93- extension JSON . Node : ExpressibleByStringLiteral {
53+ extension JSON . Node : ExpressibleByStringLiteral , ExpressibleByStringInterpolation {
9454 @inlinable public init ( stringLiteral: String ) {
95- self = . string( JSON . Literal < String > . init ( stringLiteral) )
55+ self = . string( stringLiteral)
9656 }
9757}
9858extension JSON . Node : ExpressibleByBooleanLiteral {
9959 @inlinable public init ( booleanLiteral: Bool ) {
10060 self = . bool( booleanLiteral)
10161 }
10262}
103-
63+ extension JSON . Node : CustomStringConvertible {
64+ /// Returns this value serialized as a minified string.
65+ ///
66+ /// Reparsing and reserializing this string is guaranteed to return the
67+ /// same string.
68+ public var description : String {
69+ switch self {
70+ case . null: " null "
71+ case . bool( true ) : " true "
72+ case . bool( false ) : " false "
73+ case . string( let self) : . init( self )
74+ case . number( let self) : " \( self ) "
75+ case . array( let self) : " \( self ) "
76+ case . object( let self) : " \( self ) "
77+ }
78+ }
79+ }
10480extension JSON . Node {
10581 /// Promotes a `nil` result to a thrown ``TypecastError``.
10682 ///
0 commit comments