Skip to content

Commit

Permalink
Merge pull request #410 from danibachar/json-decoder-encoder-stateless
Browse files Browse the repository at this point in the history
Remove Stateful JSON encoding and decoding
  • Loading branch information
rlepinski authored Jul 24, 2024
2 parents f5beff1 + a9ba0e3 commit 4113b1c
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 29 deletions.
6 changes: 2 additions & 4 deletions Airship/AirshipCore/Source/ChannelAudienceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ final class ChannelAudienceManager: ChannelAudienceManagerProtocol {
private let audienceOverridesProvider: AudienceOverridesProvider

private let date: AirshipDateProtocol
private let encoder = JSONEncoder()
private let decoder = JSONDecoder()
private let updateLock = AirshipLock()

private let cachedSubscriptionLists: CachedValue<[String]>
Expand Down Expand Up @@ -350,7 +348,7 @@ final class ChannelAudienceManager: ChannelAudienceManagerProtocol {
if let data = self.dataStore.data(
forKey: ChannelAudienceManager.updatesKey
) {
result = try? self.decoder.decode(
result = try? JSONDecoder().decode(
[AudienceUpdate].self,
from: data
)
Expand All @@ -361,7 +359,7 @@ final class ChannelAudienceManager: ChannelAudienceManagerProtocol {

private func storeUpdates(_ operations: [AudienceUpdate]) {
updateLock.sync {
if let data = try? self.encoder.encode(operations) {
if let data = try? JSONEncoder().encode(operations) {
self.dataStore.setObject(
data,
forKey: ChannelAudienceManager.updatesKey
Expand Down
1 change: 0 additions & 1 deletion Airship/AirshipCore/Source/ChannelAuthTokenAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ final class ChannelAuthTokenAPIClient: ChannelAuthTokenAPIClientProtocol, Sendab
private let tokenPath = "/api/auth/device"
private let config: RuntimeConfig
private let session: AirshipRequestSession
private let decoder: JSONDecoder = JSONDecoder()

init(
config: RuntimeConfig,
Expand Down
8 changes: 4 additions & 4 deletions Airship/AirshipCore/Source/ContactAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ final class ContactAPIClient: ContactsAPIClientProtocol {
private let config: RuntimeConfig
private let session: AirshipRequestSession

private let decoder: JSONDecoder = {
private var decoder: JSONDecoder {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in
let container = try decoder.singleValueContainer()
Expand All @@ -84,9 +84,9 @@ final class ContactAPIClient: ContactsAPIClientProtocol {
return date
})
return decoder
}()
}

private let encoder: JSONEncoder = {
private var encoder: JSONEncoder {
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .custom({ date, encoder in
var container = encoder.singleValueContainer()
Expand All @@ -95,7 +95,7 @@ final class ContactAPIClient: ContactsAPIClientProtocol {
)
})
return encoder
}()
}

init(config: RuntimeConfig, session: AirshipRequestSession) {
self.config = config
Expand Down
4 changes: 2 additions & 2 deletions Airship/AirshipCore/Source/ContactChannelsAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class ContactChannelsAPIClient: ContactChannelsAPIClientProtocol {
private let config: RuntimeConfig
private let session: AirshipRequestSession

private let decoder: JSONDecoder = {
private var decoder: JSONDecoder {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in
let container = try decoder.singleValueContainer()
Expand All @@ -26,7 +26,7 @@ final class ContactChannelsAPIClient: ContactChannelsAPIClientProtocol {
return date
})
return decoder
}()
}

init(config: RuntimeConfig, session: AirshipRequestSession) {
self.config = config
Expand Down
7 changes: 2 additions & 5 deletions Airship/AirshipCore/Source/PreferenceDataStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ public final class PreferenceDataStore: @unchecked Sendable {
private let defaults: UserDefaults
private let appKey: String
static let deviceIDKey = "deviceID"

private let decoder = JSONDecoder()
private let encoder = JSONEncoder()

private var pending: [String: [Any?]] = [:]
private var cache: [String: Cached] = [:]
Expand Down Expand Up @@ -177,7 +174,7 @@ public final class PreferenceDataStore: @unchecked Sendable {
return nil
}

return try decoder.decode(T.self, from: data)
return try JSONDecoder().decode(T.self, from: data)
}

public func safeCodable<T: Codable>(forKey key: String) -> T? {
Expand Down Expand Up @@ -209,7 +206,7 @@ public final class PreferenceDataStore: @unchecked Sendable {
return
}

let data = try encoder.encode(codable)
let data = try JSONEncoder().encode(codable)
write(key, value: data)
}

Expand Down
4 changes: 2 additions & 2 deletions Airship/AirshipCore/Source/RemoteDataAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class RemoteDataAPIClient: RemoteDataAPIClientProtocol {
private let session: AirshipRequestSession
private let config: RuntimeConfig

private let decoder: JSONDecoder = {
private var decoder: JSONDecoder {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in
let container = try decoder.singleValueContainer()
Expand All @@ -25,7 +25,7 @@ final class RemoteDataAPIClient: RemoteDataAPIClientProtocol {
return date
})
return decoder
}()
}

init(config: RuntimeConfig, session: AirshipRequestSession) {
self.config = config
Expand Down
7 changes: 2 additions & 5 deletions Airship/AirshipCore/Source/RemoteDataInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ public struct RemoteDataInfo: Sendable, Codable, Equatable, Hashable {
self.contactID = contactID
}

private static let decoder = JSONDecoder()
private static let encoder = JSONEncoder()

static func fromJSON(data: Data) throws -> RemoteDataInfo {
return try RemoteDataInfo.decoder.decode(RemoteDataInfo.self, from: data)
try JSONDecoder().decode(RemoteDataInfo.self, from: data)
}

func toEncodedJSONData() throws -> Data {
return try RemoteDataInfo.encoder.encode(self)
try JSONEncoder().encode(self)
}
}
8 changes: 4 additions & 4 deletions Airship/AirshipCore/Source/SMSValidator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ final class SMSValidatorAPIClient: SMSValidatorAPIClientProtocol {
private let config: RuntimeConfig
private let session: AirshipRequestSession

private let decoder: JSONDecoder = {
private var decoder: JSONDecoder {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in
let container = try decoder.singleValueContainer()
Expand All @@ -104,9 +104,9 @@ final class SMSValidatorAPIClient: SMSValidatorAPIClientProtocol {
return date
})
return decoder
}()
}

private let encoder: JSONEncoder = {
private var encoder: JSONEncoder {
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .custom({ date, encoder in
var container = encoder.singleValueContainer()
Expand All @@ -115,7 +115,7 @@ final class SMSValidatorAPIClient: SMSValidatorAPIClientProtocol {
)
})
return encoder
}()
}

init(config: RuntimeConfig, session: AirshipRequestSession) {
self.config = config
Expand Down
4 changes: 2 additions & 2 deletions Airship/AirshipCore/Source/SubscriptionListAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public final class SubscriptionListAction: AirshipAction {
private let channel: @Sendable () -> AirshipChannelProtocol
private let contact: @Sendable () -> AirshipContactProtocol

private let decoder: JSONDecoder = {
private var decoder: JSONDecoder {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
return decoder
}()
}

public var _decoder: JSONDecoder {
return decoder
Expand Down

0 comments on commit 4113b1c

Please sign in to comment.