Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: revert AnyJSON codable #580

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading