| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import Foundation
- enum JSONError: Error {
- case invalidString
- case invalidDate(String)
- case decodingFailed(Error)
- case encodingFailed
- }
- enum JSONBridge {
- static func preferences(from: JSON) throws -> Preferences {
- try JSONBridge.from(string: from.rawJSON)
- }
- static func pumpSettings(from: JSON) throws -> PumpSettings {
- try JSONBridge.from(string: from.rawJSON)
- }
- static func bgTargets(from: JSON) throws -> BGTargets {
- try JSONBridge.from(string: from.rawJSON)
- }
- static func basalProfile(from: JSON) throws -> [BasalProfileEntry] {
- try JSONBridge.from(string: from.rawJSON)
- }
- static func insulinSensitivities(from: JSON) throws -> InsulinSensitivities {
- try JSONBridge.from(string: from.rawJSON)
- }
- static func carbRatios(from: JSON) throws -> CarbRatios {
- try JSONBridge.from(string: from.rawJSON)
- }
- static func tempTargets(from: JSON) throws -> [TempTarget] {
- try JSONBridge.from(string: from.rawJSON)
- }
- static func model(from: JSON) -> String {
- from.rawJSON
- }
- static func trioSettings(from: JSON) throws -> TrioSettings {
- try JSONBridge.from(string: from.rawJSON)
- }
- static func pumpHistory(from: JSON) throws -> [PumpHistoryEvent] {
- try JSONBridge.from(string: from.rawJSON)
- }
- static func profile(from: JSON) throws -> Profile {
- try JSONBridge.from(string: from.rawJSON)
- }
- static func autosens(from: JSON) throws -> Autosens? {
- try JSONBridge.from(string: from.rawJSON)
- }
- static func clock(from: JSON) throws -> Date {
- let dateJson = from.rawJSON.replacingOccurrences(of: "\"", with: "").trimmingCharacters(in: .whitespacesAndNewlines)
- if let date = Formatter.iso8601withFractionalSeconds.date(from: dateJson) ?? Formatter.iso8601
- .date(from: dateJson)
- {
- return date
- }
- throw JSONError.invalidDate(from.rawJSON)
- }
- static func from<T: Decodable>(string: String) throws -> T {
- guard let data = string.data(using: .utf8) else {
- throw JSONError.invalidString
- }
- return try JSONCoding.decoder.decode(T.self, from: data)
- }
- static func to<T: Encodable>(_ value: T) throws -> String {
- let data = try JSONCoding.encoder.encode(value)
- guard let string = String(data: data, encoding: .utf8) else {
- throw JSONError.encodingFailed
- }
- return string
- }
- }
|