JSONBridge.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import Foundation
  2. enum JSONError: Error {
  3. case invalidString
  4. case invalidDate(String)
  5. case decodingFailed(Error)
  6. case encodingFailed
  7. }
  8. enum JSONBridge {
  9. static func preferences(from: JSON) throws -> Preferences {
  10. try JSONBridge.from(string: from.rawJSON)
  11. }
  12. static func pumpSettings(from: JSON) throws -> PumpSettings {
  13. try JSONBridge.from(string: from.rawJSON)
  14. }
  15. static func bgTargets(from: JSON) throws -> BGTargets {
  16. try JSONBridge.from(string: from.rawJSON)
  17. }
  18. static func basalProfile(from: JSON) throws -> [BasalProfileEntry] {
  19. try JSONBridge.from(string: from.rawJSON)
  20. }
  21. static func insulinSensitivities(from: JSON) throws -> InsulinSensitivities {
  22. try JSONBridge.from(string: from.rawJSON)
  23. }
  24. static func carbRatios(from: JSON) throws -> CarbRatios {
  25. try JSONBridge.from(string: from.rawJSON)
  26. }
  27. static func tempTargets(from: JSON) throws -> [TempTarget] {
  28. try JSONBridge.from(string: from.rawJSON)
  29. }
  30. static func model(from: JSON) -> String {
  31. from.rawJSON
  32. }
  33. static func trioSettings(from: JSON) throws -> TrioSettings {
  34. try JSONBridge.from(string: from.rawJSON)
  35. }
  36. static func glucose(from: JSON) throws -> [BloodGlucose] {
  37. try JSONBridge.from(string: from.rawJSON)
  38. }
  39. static func carbs(from: JSON) throws -> [CarbsEntry] {
  40. try JSONBridge.from(string: from.rawJSON)
  41. }
  42. static func iobResult(from: JSON) throws -> [IobResult] {
  43. try JSONBridge.from(string: from.rawJSON)
  44. }
  45. static func pumpHistory(from: JSON) throws -> [PumpHistoryEvent] {
  46. do {
  47. return try JSONBridge.from(string: from.rawJSON)
  48. } catch {
  49. // see if we got an empty object "{}"
  50. guard let data = from.rawJSON.data(using: .utf8) else {
  51. throw error
  52. }
  53. if let parsedObject = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
  54. parsedObject.isEmpty
  55. {
  56. return []
  57. }
  58. throw error
  59. }
  60. }
  61. static func profile(from: JSON) throws -> Profile {
  62. try JSONBridge.from(string: from.rawJSON)
  63. }
  64. static func computedCarbs(from: JSON) throws -> ComputedCarbs? {
  65. try JSONBridge.from(string: from.rawJSON)
  66. }
  67. static func autosens(from: JSON) throws -> Autosens? {
  68. try JSONBridge.from(string: from.rawJSON)
  69. }
  70. static func clock(from: JSON) throws -> Date {
  71. let dateJson = from.rawJSON.replacingOccurrences(of: "\"", with: "").trimmingCharacters(in: .whitespacesAndNewlines)
  72. if let date = Formatter.iso8601withFractionalSeconds.date(from: dateJson) ?? Formatter.iso8601
  73. .date(from: dateJson)
  74. {
  75. return date
  76. }
  77. throw JSONError.invalidDate(from.rawJSON)
  78. }
  79. static func from<T: Decodable>(string: String) throws -> T {
  80. guard let data = string.data(using: .utf8) else {
  81. throw JSONError.invalidString
  82. }
  83. return try JSONCoding.decoder.decode(T.self, from: data)
  84. }
  85. static func to<T: Encodable>(_ value: T) throws -> String {
  86. let data = try JSONCoding.encoder.encode(value)
  87. guard let string = String(data: data, encoding: .utf8) else {
  88. throw JSONError.encodingFailed
  89. }
  90. return string
  91. }
  92. }