2021-09-01 17:14:11 +02:00
|
|
|
//
|
|
|
|
|
// Copyright RevenueCat Inc. All Rights Reserved.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the MIT License (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
|
//
|
|
|
|
|
// JSONDecoder+Extensions.swift
|
|
|
|
|
//
|
|
|
|
|
// Created by Juanpe Catalán on 25/8/21.
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
enum CodableError: Error, CustomStringConvertible {
|
|
|
|
|
|
|
|
|
|
case unexpectedValue(Any.Type)
|
2021-10-25 12:13:50 -07:00
|
|
|
case valueNotFound(value: Any.Type, context: DecodingError.Context)
|
2022-03-14 14:30:35 -05:00
|
|
|
case invalidJSONObject(value: [String: Any])
|
2021-09-01 17:14:11 +02:00
|
|
|
|
|
|
|
|
var description: String {
|
|
|
|
|
switch self {
|
2022-03-14 14:30:35 -05:00
|
|
|
case let .unexpectedValue(type):
|
2021-09-08 18:40:06 -03:00
|
|
|
return Strings.codable.unexpectedValueError(type: type).description
|
2022-03-14 14:30:35 -05:00
|
|
|
case let .valueNotFound(value, context):
|
2021-10-25 12:13:50 -07:00
|
|
|
return Strings.codable.valueNotFoundError(value: value, context: context).description
|
2022-03-14 14:30:35 -05:00
|
|
|
case let .invalidJSONObject(value):
|
|
|
|
|
return Strings.codable.invalid_json_error(jsonData: value).description
|
2021-09-01 17:14:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension JSONDecoder {
|
|
|
|
|
|
2022-04-01 14:26:02 -07:00
|
|
|
/// Decodes a top-level value of the given type from the given Data containing a JSON representation of `type`.
|
2021-09-01 17:14:11 +02:00
|
|
|
///
|
|
|
|
|
/// - Parameters:
|
|
|
|
|
/// - type: The type of the value to decode. The default is `T.self`.
|
2022-04-01 14:26:02 -07:00
|
|
|
/// - data: The data to decode from.
|
2021-09-01 17:14:11 +02:00
|
|
|
/// - Returns: A value of the requested type.
|
2022-03-14 14:30:35 -05:00
|
|
|
/// - throws: `CodableError` or `DecodableError` if the data is invalid or can't be deserialized.
|
2021-09-01 17:14:11 +02:00
|
|
|
func decode<T: Decodable>(
|
|
|
|
|
_ type: T.Type = T.self,
|
2022-04-01 14:26:02 -07:00
|
|
|
jsonData: Data,
|
|
|
|
|
logErrors: Bool = true
|
2022-03-14 14:30:35 -05:00
|
|
|
) throws -> T {
|
|
|
|
|
do {
|
2022-04-01 14:26:02 -07:00
|
|
|
return try self.decode(type, from: jsonData)
|
2022-03-14 14:30:35 -05:00
|
|
|
} catch {
|
2022-04-01 14:26:02 -07:00
|
|
|
if logErrors {
|
|
|
|
|
ErrorUtils.logDecodingError(error)
|
|
|
|
|
}
|
2022-03-14 14:30:35 -05:00
|
|
|
throw error
|
2021-09-01 17:14:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:26:02 -07:00
|
|
|
/// Decodes a top-level value of the given type from the given Dictionary.
|
|
|
|
|
///
|
|
|
|
|
/// - Parameters:
|
|
|
|
|
/// - type: The type of the value to decode. The default is `T.self`.
|
|
|
|
|
/// - dictionary: The dictionary to decode from.
|
|
|
|
|
/// - Returns: A value of the requested type.
|
|
|
|
|
/// - Throws: `CodableError` or `DecodableError` if the data is invalid or can't be deserialized.
|
|
|
|
|
/// - Note: this method logs the error before throwing, so it's "safe" to use with `try?`
|
|
|
|
|
func decode<T: Decodable>(
|
|
|
|
|
_ type: T.Type = T.self,
|
|
|
|
|
dictionary: [String: Any],
|
|
|
|
|
logErrors: Bool = true
|
|
|
|
|
) throws -> T {
|
|
|
|
|
if JSONSerialization.isValidJSONObject(dictionary) {
|
|
|
|
|
return try self.decode(type,
|
|
|
|
|
jsonData: try JSONSerialization.data(withJSONObject: dictionary),
|
|
|
|
|
logErrors: logErrors)
|
|
|
|
|
} else {
|
|
|
|
|
throw CodableError.invalidJSONObject(value: dictionary)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-01 17:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension KeyedDecodingContainer {
|
|
|
|
|
|
|
|
|
|
/// Decodes a value of the given type for the given key.
|
|
|
|
|
/// - Parameters:
|
|
|
|
|
/// - type: The type of value to decode.
|
|
|
|
|
/// - key: The key that the decoded value is associated with.
|
|
|
|
|
/// - defaultValue: The default value returned if decoding fails.
|
|
|
|
|
/// - Returns: A value of the requested type, or the given default value
|
|
|
|
|
/// if decoding fails.
|
|
|
|
|
func decode<T: Decodable>(_ type: T.Type, forKey key: Self.Key, defaultValue: T) -> T {
|
|
|
|
|
do {
|
|
|
|
|
return try decode(type, forKey: key)
|
|
|
|
|
} catch {
|
|
|
|
|
return defaultValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2021-10-25 12:13:50 -07:00
|
|
|
|
2022-04-12 15:11:22 -07:00
|
|
|
extension JSONSerialization {
|
|
|
|
|
|
|
|
|
|
static func dictionary(withData data: Data) throws -> [String: Any] {
|
|
|
|
|
let object = try JSONSerialization.jsonObject(with: data)
|
|
|
|
|
|
|
|
|
|
guard let object = object as? [String: Any] else {
|
|
|
|
|
throw CodableError.unexpectedValue(type(of: object))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return object
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-25 12:13:50 -07:00
|
|
|
// MARK: Decoding Error handling
|
|
|
|
|
private extension ErrorUtils {
|
|
|
|
|
|
|
|
|
|
static func logDecodingError(_ error: Error) {
|
|
|
|
|
guard let decodingError = error as? DecodingError else {
|
|
|
|
|
Logger.error(Strings.codable.decoding_error(error))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch decodingError {
|
|
|
|
|
case .dataCorrupted(let context):
|
|
|
|
|
Logger.error(Strings.codable.corrupted_data_error(context: context))
|
|
|
|
|
case .keyNotFound(let key, let context):
|
2021-11-02 09:13:16 -07:00
|
|
|
// This is expected to happen occasionally, the backend doesn't always populate all key/values.
|
|
|
|
|
Logger.debug(Strings.codable.keyNotFoundError(key: key, context: context))
|
2021-10-25 12:13:50 -07:00
|
|
|
case .valueNotFound(let value, let context):
|
2021-11-02 09:13:16 -07:00
|
|
|
Logger.debug(Strings.codable.valueNotFoundError(value: value, context: context))
|
2021-10-25 12:13:50 -07:00
|
|
|
case .typeMismatch(let type, let context):
|
|
|
|
|
Logger.error(Strings.codable.typeMismatch(type: type, context: context))
|
2021-11-18 09:58:17 -08:00
|
|
|
@unknown default:
|
2021-10-25 12:13:50 -07:00
|
|
|
Logger.error("Unhandled DecodingError: \(decodingError)\n\(Strings.codable.decoding_error(decodingError))")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2021-12-29 13:54:53 -08:00
|
|
|
|
|
|
|
|
extension Encodable {
|
|
|
|
|
|
|
|
|
|
func asDictionary() throws -> [String: Any] {
|
|
|
|
|
let data = try JSONEncoder().encode(self)
|
|
|
|
|
let result = try JSONSerialization.jsonObject(with: data, options: [])
|
|
|
|
|
|
|
|
|
|
guard let result = result as? [String: Any] else {
|
|
|
|
|
throw CodableError.unexpectedValue(type(of: result))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2022-04-01 14:22:29 -07:00
|
|
|
|
|
|
|
|
extension JSONEncoder {
|
|
|
|
|
|
|
|
|
|
static let `default`: JSONEncoder = {
|
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
|
encoder.keyEncodingStrategy = .convertToSnakeCase
|
2022-04-01 14:29:41 -07:00
|
|
|
encoder.dateEncodingStrategy = .iso8601
|
2022-04-01 14:22:29 -07:00
|
|
|
|
|
|
|
|
return encoder
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension JSONDecoder {
|
|
|
|
|
|
|
|
|
|
static let `default`: JSONDecoder = {
|
|
|
|
|
let decoder = JSONDecoder()
|
|
|
|
|
decoder.keyDecodingStrategy = .convertFromSnakeCase
|
2022-04-01 14:29:41 -07:00
|
|
|
decoder.dateDecodingStrategy = .iso8601
|
2022-04-01 14:22:29 -07:00
|
|
|
|
|
|
|
|
return decoder
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
}
|