2022-12-22 10:09:12 -08: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
|
|
|
|
|
//
|
|
|
|
|
// Codable+Extensions.swift
|
|
|
|
|
//
|
|
|
|
|
// Created by Nacho Soto on 11/29/22.
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
extension Encodable {
|
|
|
|
|
|
|
|
|
|
/// - Throws: if encoding failed
|
|
|
|
|
/// - Returns: `nil` if the encoded `Data` can't be serialized into a `String`.
|
|
|
|
|
var prettyPrintedJSON: String? {
|
|
|
|
|
get throws {
|
|
|
|
|
return String(data: try self.prettyPrintedData, encoding: .utf8)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-31 07:11:23 -07:00
|
|
|
/// - Throws: if encoding failed
|
|
|
|
|
/// - Returns: `nil` if the encoded `Data` can't be serialized into a `String`.
|
|
|
|
|
var encodedJSON: String? {
|
|
|
|
|
get throws {
|
|
|
|
|
return String(data: try self.jsonEncodedData, encoding: .utf8)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
|
2022-12-22 10:09:12 -08:00
|
|
|
var prettyPrintedData: Data {
|
|
|
|
|
get throws {
|
|
|
|
|
return try JSONEncoder.prettyPrinted.encode(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-09 11:30:59 -07:00
|
|
|
/// - Note: beginning with iOS 17, the output of this is not guaranteed to be consistent due to key ordering.
|
|
|
|
|
/// For tests, it's better to compare `prettyPrintedData` which does sort keys.
|
2023-05-31 07:11:23 -07:00
|
|
|
var jsonEncodedData: Data {
|
|
|
|
|
get throws {
|
|
|
|
|
return try JSONEncoder.default.encode(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-22 10:09:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension JSONEncoder {
|
|
|
|
|
|
|
|
|
|
static let `default`: JSONEncoder = {
|
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
|
encoder.keyEncodingStrategy = .convertToSnakeCase
|
|
|
|
|
encoder.dateEncodingStrategy = .iso8601
|
|
|
|
|
|
|
|
|
|
return encoder
|
|
|
|
|
}()
|
|
|
|
|
|
2025-02-03 12:17:31 +01:00
|
|
|
/// JSONEncoder (just like the default one, but prettyPrinted and sortedKeys)
|
2022-12-22 10:09:12 -08:00
|
|
|
static let prettyPrinted: JSONEncoder = {
|
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
|
encoder.keyEncodingStrategy = .convertToSnakeCase
|
|
|
|
|
encoder.dateEncodingStrategy = .iso8601
|
2023-06-09 11:30:59 -07:00
|
|
|
encoder.outputFormatting = [.sortedKeys, .prettyPrinted]
|
2022-12-22 10:09:12 -08:00
|
|
|
|
|
|
|
|
return encoder
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension JSONDecoder {
|
|
|
|
|
|
|
|
|
|
static let `default`: JSONDecoder = {
|
|
|
|
|
let decoder = JSONDecoder()
|
|
|
|
|
decoder.keyDecodingStrategy = .convertFromSnakeCase
|
|
|
|
|
decoder.dateDecodingStrategy = .iso8601
|
|
|
|
|
|
|
|
|
|
return decoder
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
}
|