Skip to content

Commit c6e901c

Browse files
authored
refactor: AnyJSON type (#573)
* refactor: AnyJSON type * ci, skip visionOS
1 parent 97079ea commit c6e901c

File tree

3 files changed

+57
-23
lines changed

3 files changed

+57
-23
lines changed

.github/workflows/ci.yml

+2-9
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,14 @@ jobs:
2222
command: [test, ""]
2323
platform: [IOS, MAC_CATALYST, MACOS, TVOS, VISIONOS, WATCHOS]
2424
xcode: [15.4, "16.0"]
25+
exclude:
26+
- { platform: VISIONOS }
2527
include:
2628
- { command: test, skip_release: 1 }
2729
steps:
2830
- uses: actions/checkout@v4
2931
- name: Select Xcode ${{ matrix.xcode }}
3032
run: sudo xcode-select -s /Applications/Xcode_${{ matrix.xcode }}.app
31-
- name: Install visionOS runtime
32-
if: matrix.platform == 'visionOS'
33-
run: |
34-
sudo xcodebuild -runFirstLaunch
35-
sudo xcrun simctl list
36-
sudo xcodebuild -downloadPlatform visionOS
37-
sudo xcodebuild -runFirstLaunch
38-
- name: List available devices
39-
run: xcrun simctl list devices available
4033
- name: Cache derived data
4134
uses: actions/cache@v3
4235
with:

Sources/Helpers/AnyJSON/AnyJSON+Codable.swift

+45-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ import Foundation
99

1010
extension AnyJSON {
1111
/// The decoder instance used for transforming AnyJSON to some Codable type.
12+
@available(
13+
*, deprecated, message: "decoder is deprecated, AnyJSON now uses default JSONDecoder()."
14+
)
1215
public static let decoder: JSONDecoder = {
1316
let decoder = JSONDecoder()
1417
decoder.dataDecodingStrategy = .base64
1518
decoder.dateDecodingStrategy = .custom { decoder in
1619
let container = try decoder.singleValueContainer()
1720
let dateString = try container.decode(String.self)
1821

19-
let date = ISO8601DateFormatter.iso8601WithFractionalSeconds.value.date(from: dateString) ?? ISO8601DateFormatter.iso8601.value.date(from: dateString)
22+
let date =
23+
ISO8601DateFormatter.iso8601WithFractionalSeconds.value.date(from: dateString)
24+
?? ISO8601DateFormatter.iso8601.value.date(from: dateString)
2025

2126
guard let decodedDate = date else {
2227
throw DecodingError.dataCorruptedError(
@@ -30,6 +35,9 @@ extension AnyJSON {
3035
}()
3136

3237
/// The encoder instance used for transforming AnyJSON to some Codable type.
38+
@available(
39+
*, deprecated, message: "encoder is deprecated, AnyJSON now uses default JSONEncoder()."
40+
)
3341
public static let encoder: JSONEncoder = {
3442
let encoder = JSONEncoder()
3543
encoder.dataEncodingStrategy = .base64
@@ -47,12 +55,29 @@ extension AnyJSON {
4755
public init(_ value: some Codable) throws {
4856
if let value = value as? AnyJSON {
4957
self = value
58+
} else if let string = value as? String {
59+
self = .string(string)
60+
} else if let bool = value as? Bool {
61+
self = .bool(bool)
62+
} else if let int = value as? Int {
63+
self = .integer(int)
64+
} else if let double = value as? Double {
65+
self = .double(double)
5066
} else {
51-
let data = try AnyJSON.encoder.encode(value)
52-
self = try AnyJSON.decoder.decode(AnyJSON.self, from: data)
67+
let data = try JSONEncoder().encode(value)
68+
self = try JSONDecoder().decode(AnyJSON.self, from: data)
5369
}
5470
}
5571

72+
/// Decodes self instance as `Decodable` type.
73+
public func decode<T: Decodable>(as type: T.Type = T.self) throws -> T {
74+
let data = try JSONEncoder().encode(self)
75+
return try JSONDecoder().decode(T.self, from: data)
76+
}
77+
78+
@available(
79+
*, deprecated, renamed: "decode(as:)", message: "Providing a custom decoder is deprecated."
80+
)
5681
public func decode<T: Decodable>(
5782
as _: T.Type = T.self,
5883
decoder: JSONDecoder = AnyJSON.decoder
@@ -63,6 +88,14 @@ extension AnyJSON {
6388
}
6489

6590
extension JSONArray {
91+
/// Decodes self instance as array of `Decodable` type.
92+
public func decode<T: Decodable>(as _: T.Type = T.self) throws -> [T] {
93+
try AnyJSON.array(self).decode(as: [T].self)
94+
}
95+
96+
@available(
97+
*, deprecated, renamed: "decode(as:)", message: "Providing a custom decoder is deprecated."
98+
)
6699
public func decode<T: Decodable>(
67100
as _: T.Type = T.self,
68101
decoder: JSONDecoder = AnyJSON.decoder
@@ -72,13 +105,22 @@ extension JSONArray {
72105
}
73106

74107
extension JSONObject {
108+
/// Decodes self instance as `Decodable` type.
109+
public func decode<T: Decodable>(as type: T.Type = T.self) throws -> T {
110+
try AnyJSON.object(self).decode(as: type)
111+
}
112+
113+
@available(
114+
*, deprecated, renamed: "decode(as:)", message: "Providing a custom decoder is deprecated."
115+
)
75116
public func decode<T: Decodable>(
76117
as _: T.Type = T.self,
77118
decoder: JSONDecoder = AnyJSON.decoder
78119
) throws -> T {
79120
try AnyJSON.object(self).decode(as: T.self, decoder: decoder)
80121
}
81122

123+
/// Initialize JSONObject from a `Codable` type
82124
public init(_ value: some Codable) throws {
83125
guard let object = try AnyJSON(value).objectValue else {
84126
throw DecodingError.typeMismatch(

Tests/HelpersTests/AnyJSONTests.swift

+10-11
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,20 @@ final class AnyJSONTests: XCTestCase {
6565

6666
func testDecode() throws {
6767
let data = try XCTUnwrap(jsonString.data(using: .utf8))
68-
let decodedJSON = try AnyJSON.decoder.decode(AnyJSON.self, from: data)
68+
let decodedJSON = try JSONDecoder().decode(AnyJSON.self, from: data)
6969

7070
expectNoDifference(decodedJSON, jsonObject)
7171
}
7272

73-
// Commented out as this is failing on CI.
74-
// func testEncode() throws {
75-
// let encoder = AnyJSON.encoder
76-
// encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
77-
//
78-
// let data = try encoder.encode(jsonObject)
79-
// let decodedJSONString = try XCTUnwrap(String(data: data, encoding: .utf8))
80-
//
81-
// expectNoDifference(decodedJSONString, jsonString)
82-
// }
73+
func testEncode() throws {
74+
let encoder = JSONEncoder()
75+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
76+
77+
let data = try encoder.encode(jsonObject)
78+
let decodedJSONString = try XCTUnwrap(String(data: data, encoding: .utf8))
79+
80+
expectNoDifference(decodedJSONString, jsonString)
81+
}
8382

8483
func testInitFromCodable() {
8584
try expectNoDifference(AnyJSON(jsonObject), jsonObject)

0 commit comments

Comments
 (0)