@@ -14,166 +14,177 @@ public struct JSON {
1414 public typealias Object = [ String : JSON . Value ]
1515
1616 /// JSON value type.
17- /// Guarenteed to be valid JSON if root value is array or object.
17+ ///
18+ /// - Note: Guarenteed to be valid JSON if root value is array or object.
1819 public enum Value : RawRepresentable , Equatable , CustomStringConvertible {
1920
20- case Null
21+ /// JSON value is a null placeholder.
22+ case null
2123
22- /// JSON value is a String Value
23- case String ( StringValue )
24+ /// JSON value is an array of other JSON values.
25+ case array ( SwiftFoundation . JSON . Array )
2426
25- /// JSON value is a Number Value (specific subtypes)
26- case Number ( JSON . Number )
27+ /// JSON value a JSON object.
28+ case object ( SwiftFoundation . JSON . Object )
2729
28- /// JSON value is an Array of other JSON values
29- case Array ( JSONArray )
30+ /// JSON value is a `String`.
31+ case string ( Swift . String )
3032
31- /// JSON value a JSON object
32- case Object ( JSONObject )
33+ /// JSON value is a `Bool`.
34+ case boolean ( Bool )
3335
34- // MARK: - Extract Values
36+ /// JSON value is a `Int`.
37+ case integer( Int )
3538
36- public var arrayValue : JSON . Array ? {
37-
38- switch self {
39-
40- case let . Array( value) : return value
41-
42- default : return nil
43- }
44- }
39+ /// JSON value is a `Double`.
40+ case double( Swift . Double )
41+ }
42+ }
43+
44+ // MARK: Extract Values
45+
46+ public extension JSON . Value {
47+
48+ public var arrayValue : JSON . Array ? {
4549
46- public var objectValue : JSON . Object ? {
47-
48- switch self {
49-
50- case let . Object( value) : return value
51-
52- default : return nil
53- }
54- }
50+ guard case let . array( value) = self else { return nil }
51+
52+ return value
53+ }
54+
55+ public var objectValue : JSON . Object ? {
56+
57+ guard case let . object( value) = self else { return nil }
58+
59+ return value
60+ }
61+
62+ public var stringValue : String ? {
63+
64+ guard case let . string( value) = self else { return nil }
65+
66+ return value
5567 }
5668
57- // MARK: - JSON Number
69+ public var booleanValue : Bool ? {
70+
71+ guard case let . boolean( value) = self else { return nil }
72+
73+ return value
74+ }
5875
59- public enum Number : RawRepresentable , Equatable , CustomStringConvertible {
76+ public var integerValue : Int ? {
6077
61- case Boolean ( Bool )
78+ guard case let . integer ( value ) = self else { return nil }
6279
63- case Integer( Int )
80+ return value
81+ }
82+
83+ public var doubleValue : Double ? {
84+
85+ guard case let . double( value) = self else { return nil }
6486
65- case Double ( DoubleValue )
87+ return value
6688 }
6789}
6890
69- // MARK: - RawRepresentable
91+ // MARK: RawRepresentable
7092
7193public extension JSON . Value {
7294
7395 var rawValue : Any {
7496
7597 switch self {
7698
77- case . Null: return SwiftFoundation . Null ( )
99+ case . null: return SwiftFoundation . Null ( )
100+
101+ case . string( let value) : return value
102+
103+ case . integer( let value) : return value
78104
79- case . String ( let string ) : return string
105+ case . boolean ( let value ) : return value
80106
81- case . Number ( let number ) : return number . rawValue
107+ case . double ( let value ) : return value
82108
83- case . Array ( let array) : return array. rawValues
109+ case . array ( let array) : return array. rawValues
84110
85- case . Object ( let dictionary) :
111+ case . object ( let dictionary) :
86112
87- var dictionaryValue = [ StringValue : Any] ( minimumCapacity: dictionary. count)
113+ var dictionaryValue = [ String : Any] ( minimumCapacity: dictionary. count)
88114
89115 for (key, value) in dictionary {
90116
91117 dictionaryValue [ key] = value. rawValue
92118 }
93119
94- return dictionaryValue
120+ return dictionary
95121 }
96122 }
97123
98124 init ? ( rawValue: Any ) {
99125
100- guard ( rawValue as? SwiftFoundation . Null) == nil else {
126+ guard rawValue is SwiftFoundation . Null == false else {
101127
102- self = . Null
128+ self = . null
103129 return
104130 }
105131
106132 if let string = rawValue as? Swift . String {
107133
108- self = . String ( string)
134+ self = . string ( string)
109135 return
110136 }
111137
112- if let number = JSON . Number ( rawValue: rawValue ) {
138+ if let integer = rawValue as? Int {
113139
114- self = . Number ( number )
140+ self = . integer ( integer )
115141 return
116142 }
117143
118- if let rawArray = rawValue as? [ Any ] , let jsonArray : [ JSON . Value ] = JSON . Value . from ( rawValues : rawArray ) {
144+ if let boolean = rawValue as? Bool {
119145
120- self = . Array( jsonArray)
146+ self = . boolean( boolean)
147+ return
148+ }
149+
150+ if let double = rawValue as? Double {
151+
152+ self = . double( double)
153+ return
154+ }
155+
156+ if let rawArray = rawValue as? [ Any ] ,
157+ let jsonArray: [ JSON . Value ] = JSON . Value. from ( rawValues: rawArray) {
158+
159+ self = . array( jsonArray)
121160 return
122161 }
123162
124163 if let rawDictionary = rawValue as? [ Swift . String : Any ] {
125164
126- var jsonObject = [ StringValue : JSONValue ] ( minimumCapacity: rawDictionary. count)
165+ var jsonObject : [ Swift . String : JSON . Value ] = Dictionary ( minimumCapacity: rawDictionary. count)
127166
128167 for (key, rawValue) in rawDictionary {
129168
130- guard let jsonValue = JSON . Value ( rawValue: rawValue) else { return nil }
169+ guard let jsonValue = JSON . Value ( rawValue: rawValue)
170+ else { return nil }
131171
132172 jsonObject [ key] = jsonValue
133173 }
134174
135- self = . Object ( jsonObject)
175+ self = . object ( jsonObject)
136176 return
137177 }
138178
139179 return nil
140180 }
141181}
142182
143- public extension JSON . Number {
144-
145- public var rawValue : Any {
146-
147- switch self {
148- case . Boolean( let value) : return value
149- case . Integer( let value) : return value
150- case . Double( let value) : return value
151- }
152- }
153-
154- public init ? ( rawValue: Any ) {
155-
156- if let value = rawValue as? Bool { self = . Boolean( value) ; return }
157- if let value = rawValue as? Int { self = . Integer( value) ; return }
158- if let value = rawValue as? DoubleValue { self = . Double( value) ; return }
159-
160- return nil
161- }
162- }
163-
164183
165- // MARK: - CustomStringConvertible
184+ // MARK: CustomStringConvertible
166185
167186public extension JSON . Value {
168187
169- public var description : Swift . String {
170-
171- return " \( rawValue) "
172- }
173- }
174-
175- public extension JSON . Number {
176-
177188 public var description : String {
178189
179190 return " \( rawValue) "
@@ -186,72 +197,20 @@ public func ==(lhs: JSON.Value, rhs: JSON.Value) -> Bool {
186197
187198 switch ( lhs, rhs) {
188199
189- case ( . Null, . Null) : return true
190-
191- case let ( . String( leftValue) , . String( rightValue) ) : return leftValue == rightValue
200+ case ( . null, . null) : return true
192201
193- case let ( . Number ( leftValue) , . Number ( rightValue) ) : return leftValue == rightValue
202+ case let ( . string ( leftValue) , . string ( rightValue) ) : return leftValue == rightValue
194203
195- case let ( . Array ( leftValue) , . Array ( rightValue) ) : return leftValue == rightValue
204+ case let ( . boolean ( leftValue) , . boolean ( rightValue) ) : return leftValue == rightValue
196205
197- case let ( . Object ( leftValue) , . Object ( rightValue) ) : return leftValue == rightValue
206+ case let ( . integer ( leftValue) , . integer ( rightValue) ) : return leftValue == rightValue
198207
199- default : return false
200- }
201- }
202-
203- public func == ( lhs: JSON . Number , rhs: JSON . Number ) -> Bool {
204-
205- switch ( lhs, rhs) {
208+ case let ( . double( leftValue) , . double( rightValue) ) : return leftValue == rightValue
206209
207- case let ( . Boolean ( leftValue) , . Boolean ( rightValue) ) : return leftValue == rightValue
210+ case let ( . array ( leftValue) , . array ( rightValue) ) : return leftValue == rightValue
208211
209- case let ( . Integer( leftValue) , . Integer( rightValue) ) : return leftValue == rightValue
210-
211- case let ( . Double( leftValue) , . Double( rightValue) ) : return leftValue == rightValue
212+ case let ( . object( leftValue) , . object( rightValue) ) : return leftValue == rightValue
212213
213214 default : return false
214215 }
215216}
216-
217- // MARK: - Protocol
218-
219- /// Type can be converted to JSON.
220- public protocol JSONEncodable {
221-
222- /// Encodes the reciever into JSON.
223- func toJSON( ) -> JSON . Value
224- }
225-
226- /// Type can be converted from JSON.
227- public protocol JSONDecodable {
228-
229- /// Decodes the reciever from JSON.
230- init ? ( JSONValue: JSON . Value )
231- }
232-
233- /// Type can be converted from JSON according to parameters.
234- public protocol JSONParametrizedDecodable {
235-
236- associatedtype JSONDecodingParameters
237-
238- /// Decodes the reciever from JSON according to the specified parameters.
239- init ? ( JSONValue: JSON . Value , parameters: JSONDecodingParameters )
240- }
241-
242- // Typealiases due to compiler error
243-
244- public typealias JSONValue = JSON . Value
245-
246- public typealias JSONArray = [ JSONValue ]
247-
248- public typealias JSONObject = [ String : JSONValue ]
249-
250- public typealias StringValue = String
251-
252- public typealias FloatValue = Float
253-
254- public typealias DoubleValue = Double
255-
256- public typealias NullValue = Null
257-
0 commit comments