Skip to content

Commit c3c1d9c

Browse files
committed
fix: only json supports dictionary, array and string types
1 parent 512483f commit c3c1d9c

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

Sources/ExCodable/ExCodable.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -383,21 +383,21 @@ public extension Encodable {
383383
return try? encoder.encode(self)
384384
}
385385

386-
func encoded(using encoder: DataEncoder = JSONEncoder()) -> [String: Any]? {
386+
func encoded(using encoder: JSONEncoder = JSONEncoder()) -> [String: Any]? {
387387
if let data = encoded(using: encoder) as Data? {
388388
return try? JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed) as? [String: Any]
389389
}
390390
return nil
391391
}
392392

393-
func encoded(using encoder: DataEncoder = JSONEncoder()) -> [Any]? {
393+
func encoded(using encoder: JSONEncoder = JSONEncoder()) -> [Any]? {
394394
if let data = encoded(using: encoder) as Data? {
395395
return try? JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed) as? [Any]
396396
}
397397
return nil
398398
}
399399

400-
func encoded(using encoder: DataEncoder = JSONEncoder()) -> String? {
400+
func encoded(using encoder: JSONEncoder = JSONEncoder()) -> String? {
401401
if let data = encoded(using: encoder) as Data? {
402402
return String(data: data, encoding: .utf8)
403403
}
@@ -414,23 +414,23 @@ public extension Decodable {
414414
return try? decoder.decode(type, from: data)
415415
}
416416

417-
static func decoded(from json: [String: Any], using decoder: DataDecoder = JSONDecoder(), as type: Self.Type = Self.self) -> Self? {
417+
static func decoded(from json: [String: Any], using decoder: JSONDecoder = JSONDecoder(), as type: Self.Type = Self.self) -> Self? {
418418
if JSONSerialization.isValidJSONObject(json),
419419
let data = try? JSONSerialization.data(withJSONObject: json, options: .fragmentsAllowed) {
420420
return try? decoder.decode(type, from: data)
421421
}
422422
return nil
423423
}
424424

425-
static func decoded(from jsonArray: [Any], using decoder: DataDecoder = JSONDecoder(), as type: Self.Type = Self.self) -> Self? {
425+
static func decoded(from jsonArray: [Any], using decoder: JSONDecoder = JSONDecoder(), as type: Self.Type = Self.self) -> Self? {
426426
if JSONSerialization.isValidJSONObject(jsonArray),
427427
let data = try? JSONSerialization.data(withJSONObject: jsonArray, options: .fragmentsAllowed) {
428428
return try? decoder.decode(type, from: data)
429429
}
430430
return nil
431431
}
432432

433-
static func decoded(from string: String, using decoder: DataDecoder = JSONDecoder(), as type: Self.Type = Self.self) -> Self? {
433+
static func decoded(from string: String, using decoder: JSONDecoder = JSONDecoder(), as type: Self.Type = Self.self) -> Self? {
434434
let data = Data(string.utf8)
435435
return try? decoder.decode(type, from: data)
436436
}
@@ -446,7 +446,7 @@ public extension Data {
446446
}
447447

448448
public extension Dictionary {
449-
func decoded<T: Decodable>(using decoder: DataDecoder = JSONDecoder(), as type: T.Type = T.self) -> T? {
449+
func decoded<T: Decodable>(using decoder: JSONDecoder = JSONDecoder(), as type: T.Type = T.self) -> T? {
450450
guard JSONSerialization.isValidJSONObject(self),
451451
let data = try? JSONSerialization.data(withJSONObject: self, options: .fragmentsAllowed) else {
452452
return nil
@@ -456,7 +456,7 @@ public extension Dictionary {
456456
}
457457

458458
public extension Array {
459-
func decoded<T: Decodable>(using decoder: DataDecoder = JSONDecoder(), as type: T.Type = T.self) -> T? {
459+
func decoded<T: Decodable>(using decoder: JSONDecoder = JSONDecoder(), as type: T.Type = T.self) -> T? {
460460
guard JSONSerialization.isValidJSONObject(self),
461461
let data = try? JSONSerialization.data(withJSONObject: self, options: .fragmentsAllowed) else {
462462
return nil
@@ -466,7 +466,7 @@ public extension Array {
466466
}
467467

468468
public extension String {
469-
func decoded<T: Decodable>(using decoder: DataDecoder = JSONDecoder(), as type: T.Type = T.self) -> T? {
469+
func decoded<T: Decodable>(using decoder: JSONDecoder = JSONDecoder(), as type: T.Type = T.self) -> T? {
470470
return Data(self.utf8).decoded(using: decoder, as: type)
471471
}
472472
}

0 commit comments

Comments
 (0)