Skip to content

Commit

Permalink
fix: revert AnyJSON codable (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev authored Oct 24, 2024
1 parent 7266b64 commit bfb6ed7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 60 deletions.
36 changes: 4 additions & 32 deletions Sources/Helpers/AnyJSON/AnyJSON+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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<T: Decodable>(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<T: Decodable>(
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<T: Decodable>(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<T: Decodable>(
as _: T.Type = T.self,
decoder: JSONDecoder = AnyJSON.decoder
Expand All @@ -106,13 +85,6 @@ extension JSONArray {

extension JSONObject {
/// Decodes self instance as `Decodable` type.
public func decode<T: Decodable>(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<T: Decodable>(
as _: T.Type = T.self,
decoder: JSONDecoder = AnyJSON.decoder
Expand Down
57 changes: 29 additions & 28 deletions Tests/HelpersTests/AnyJSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit bfb6ed7

Please sign in to comment.