Skip to content

Commit 4b954e7

Browse files
committed
Add toJSONArray method
1 parent caefc12 commit 4b954e7

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

APIService/Sources/Support/JSONConvertible.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extension Data: JSONConvertible {
4141
/// - maxLength: The maximum length of the resulting string. Defaults to 0 (no limit).
4242
/// - Returns: A JSON string if the conversion is successful, otherwise nil.
4343
public func toJSONString(prettyPrinted: Bool = false, maxLength: Int = 0) -> String? {
44-
guard let jsonObject = toJSON() else {
44+
guard let jsonObject: Any = toJSON() ?? toJSONArray() else {
4545
return self.toUTF8String(maxLength: maxLength)
4646
}
4747

@@ -64,6 +64,13 @@ extension Data {
6464
try? JSONSerialization.jsonObject(with: self, options: []) as? [String: Any]
6565
}
6666

67+
/// Converts Data to a JSON array.
68+
///
69+
/// - Returns: An array if the Data can be successfully converted to JSON, otherwise nil.
70+
public func toJSONArray() -> [[String: Any]]? {
71+
try? JSONSerialization.jsonObject(with: self, options: []) as? [[String: Any]]
72+
}
73+
6774
/// Converts Data to a UTF-8 string.
6875
///
6976
/// - Parameters:

APIServiceExample/APIServiceExample/AppDelegate.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class AppDelegate: NSObject, UIApplicationDelegate {
1818
TokenManager.shared.token = Token(accessToken: "a token", refreshToken: "a refresh token", isExpired: true)
1919
DefaultAPIService.shared.logger = APILoggers.verbose
2020
APILoggers.verbose.log = Logs.console
21+
APILoggers.verbose.prettyPrinted = true
22+
APILoggers.verbose.maxLength = 2000
2123
// APILoggers.intermediate.prettyPrinted = true
2224
// APILoggers.intermediate.maxLength = 1000
2325
// CompactLogger.shared.prefix = nil

0 commit comments

Comments
 (0)