diff --git a/Sources/Helpers/AnyJSON/AnyJSON+Codable.swift b/Sources/Helpers/AnyJSON/AnyJSON+Codable.swift index 18e34547..f3b1f997 100644 --- a/Sources/Helpers/AnyJSON/AnyJSON+Codable.swift +++ b/Sources/Helpers/AnyJSON/AnyJSON+Codable.swift @@ -9,9 +9,6 @@ import Foundation extension AnyJSON { /// The decoder instance used for transforming AnyJSON to some Codable type. - @available( - *, deprecated, message: "decoder is deprecated, AnyJSON now uses default JSONDecoder()." - ) public static let decoder: JSONDecoder = { let decoder = JSONDecoder() decoder.dataDecodingStrategy = .base64 @@ -35,9 +32,6 @@ extension AnyJSON { }() /// The encoder instance used for transforming AnyJSON to some Codable type. - @available( - *, deprecated, message: "encoder is deprecated, AnyJSON now uses default JSONEncoder()." - ) public static let encoder: JSONEncoder = { let encoder = JSONEncoder() encoder.dataEncodingStrategy = .base64 @@ -64,38 +58,23 @@ extension AnyJSON { } else if let double = value as? Double { self = .double(double) } else { - let data = try JSONEncoder().encode(value) - self = try JSONDecoder().decode(AnyJSON.self, from: data) + let data = try AnyJSON.encoder.encode(value) + self = try AnyJSON.decoder.decode(AnyJSON.self, from: data) } } /// Decodes self instance as `Decodable` type. - public func decode(as type: T.Type = T.self) throws -> T { - let data = try JSONEncoder().encode(self) - return try JSONDecoder().decode(T.self, from: data) - } - - @available( - *, deprecated, renamed: "decode(as:)", message: "Providing a custom decoder is deprecated." - ) public func decode( - as _: T.Type = T.self, + as type: T.Type = T.self, decoder: JSONDecoder = AnyJSON.decoder ) throws -> T { let data = try AnyJSON.encoder.encode(self) - return try decoder.decode(T.self, from: data) + return try decoder.decode(type, from: data) } } extension JSONArray { /// Decodes self instance as array of `Decodable` type. - public func decode(as _: T.Type = T.self) throws -> [T] { - try AnyJSON.array(self).decode(as: [T].self) - } - - @available( - *, deprecated, renamed: "decode(as:)", message: "Providing a custom decoder is deprecated." - ) public func decode( as _: T.Type = T.self, decoder: JSONDecoder = AnyJSON.decoder @@ -106,13 +85,6 @@ extension JSONArray { extension JSONObject { /// Decodes self instance as `Decodable` type. - public func decode(as type: T.Type = T.self) throws -> T { - try AnyJSON.object(self).decode(as: type) - } - - @available( - *, deprecated, renamed: "decode(as:)", message: "Providing a custom decoder is deprecated." - ) public func decode( as _: T.Type = T.self, decoder: JSONDecoder = AnyJSON.decoder diff --git a/Tests/HelpersTests/AnyJSONTests.swift b/Tests/HelpersTests/AnyJSONTests.swift index 58407b44..2c2197a1 100644 --- a/Tests/HelpersTests/AnyJSONTests.swift +++ b/Tests/HelpersTests/AnyJSONTests.swift @@ -7,24 +7,13 @@ import CustomDump import Foundation -@testable import Helpers import XCTest +@testable import Helpers + final class AnyJSONTests: XCTestCase { let jsonString = """ - { - "array" : [ - 1, - 2, - 3, - 4, - 5 - ], - "bool" : true, - "double" : 3.14, - "integer" : 1, - "null" : null, - "object" : { + { "array" : [ 1, 2, @@ -37,13 +26,25 @@ final class AnyJSONTests: XCTestCase { "integer" : 1, "null" : null, "object" : { + "array" : [ + 1, + 2, + 3, + 4, + 5 + ], + "bool" : true, + "double" : 3.14, + "integer" : 1, + "null" : null, + "object" : { + }, + "string" : "A string value" }, "string" : "A string value" - }, - "string" : "A string value" - } - """ + } + """ let jsonObject: AnyJSON = [ "integer": 1, @@ -65,20 +66,20 @@ final class AnyJSONTests: XCTestCase { func testDecode() throws { let data = try XCTUnwrap(jsonString.data(using: .utf8)) - let decodedJSON = try JSONDecoder().decode(AnyJSON.self, from: data) + let decodedJSON = try AnyJSON.decoder.decode(AnyJSON.self, from: data) expectNoDifference(decodedJSON, jsonObject) } - func testEncode() throws { - let encoder = JSONEncoder() - encoder.outputFormatting = [.prettyPrinted, .sortedKeys] - - let data = try encoder.encode(jsonObject) - let decodedJSONString = try XCTUnwrap(String(data: data, encoding: .utf8)) - - expectNoDifference(decodedJSONString, jsonString) - } + func testEncode() throws { + let encoder = AnyJSON.encoder + encoder.outputFormatting = [.prettyPrinted, .sortedKeys] + + let data = try encoder.encode(jsonObject) + let decodedJSONString = try XCTUnwrap(String(data: data, encoding: .utf8)) + + expectNoDifference(decodedJSONString, jsonString) + } func testInitFromCodable() { try expectNoDifference(AnyJSON(jsonObject), jsonObject)