@@ -122,84 +122,6 @@ public enum AnyJSON: Sendable, Codable, Hashable {
122122 case let . bool( val) : try container. encode ( val)
123123 }
124124 }
125-
126- public func decode< T: Decodable > ( _: T . Type , decoder: JSONDecoder = AnyJSON . decoder) throws -> T {
127- let data = try AnyJSON . encoder. encode ( self )
128- return try decoder. decode ( T . self, from: data)
129- }
130- }
131-
132- extension JSONObject {
133- public func decode< T: Decodable > ( _: T . Type , decoder: JSONDecoder = AnyJSON . decoder) throws -> T {
134- try AnyJSON . object ( self ) . decode ( T . self, decoder: decoder)
135- }
136-
137- public init ( _ value: some Codable ) throws {
138- guard let object = try AnyJSON ( value) . objectValue else {
139- throw DecodingError . typeMismatch (
140- JSONObject . self,
141- DecodingError . Context (
142- codingPath: [ ] ,
143- debugDescription: " Expected to decode value to \( JSONObject . self) . "
144- )
145- )
146- }
147-
148- self = object
149- }
150- }
151-
152- extension JSONArray {
153- public func decode< T: Decodable > ( _: T . Type ) throws -> [ T ] {
154- try AnyJSON . array ( self ) . decode ( [ T ] . self)
155- }
156- }
157-
158- extension AnyJSON {
159- /// The decoder instance used for transforming AnyJSON to some Codable type.
160- public static let decoder : JSONDecoder = {
161- let decoder = JSONDecoder ( )
162- decoder. dataDecodingStrategy = . base64
163- decoder. dateDecodingStrategy = . custom { decoder in
164- let container = try decoder. singleValueContainer ( )
165- let dateString = try container. decode ( String . self)
166-
167- let date = DateFormatter . iso8601. date ( from: dateString) ?? DateFormatter
168- . iso8601_noMilliseconds. date ( from: dateString)
169-
170- guard let decodedDate = date else {
171- throw DecodingError . typeMismatch (
172- Date . self,
173- DecodingError . Context (
174- codingPath: container. codingPath,
175- debugDescription: " String is not a valid Date "
176- )
177- )
178- }
179-
180- return decodedDate
181- }
182- return decoder
183- } ( )
184-
185- /// The encoder instance used for transforming AnyJSON to some Codable type.
186- public static let encoder : JSONEncoder = {
187- let encoder = JSONEncoder ( )
188- encoder. dataEncodingStrategy = . base64
189- encoder. dateEncodingStrategy = . formatted( DateFormatter . iso8601)
190- return encoder
191- } ( )
192- }
193-
194- extension AnyJSON {
195- public init ( _ value: some Codable ) throws {
196- if let value = value as? AnyJSON {
197- self = value
198- } else {
199- let data = try AnyJSON . encoder. encode ( value)
200- self = try AnyJSON . decoder. decode ( AnyJSON . self, from: data)
201- }
202- }
203125}
204126
205127extension AnyJSON : ExpressibleByNilLiteral {
0 commit comments