Prechádzať zdrojové kódy

Merge pull request #204 from bjorkert/svg_decimals

 Improve JSON Decoding in NightScout.swift and ShareClientExtension.swift
Marion Barker 3 rokov pred
rodič
commit
d03bbc76d7

+ 18 - 1
LoopFollow/Controllers/NightScout.swift

@@ -193,8 +193,25 @@ extension MainViewController {
                 
             }
             
+            var entriesResponse: [ShareGlucoseData]?
             let decoder = JSONDecoder()
-            let entriesResponse = try? decoder.decode([ShareGlucoseData].self, from: data)
+            do {
+                entriesResponse = try decoder.decode([ShareGlucoseData].self, from: data)
+            } catch let DecodingError.dataCorrupted(context) {
+                print("Data corrupted: \(context)")
+            } catch let DecodingError.keyNotFound(key, context) {
+                print("Key '\(key)' not found: \(context.debugDescription)")
+                print("codingPath: \(context.codingPath)")
+            } catch let DecodingError.valueNotFound(value, context) {
+                print("Value '\(value)' not found: \(context.debugDescription)")
+                print("codingPath: \(context.codingPath)")
+            } catch let DecodingError.typeMismatch(type, context)  {
+                print("Type '\(type)' mismatch: \(context.debugDescription)")
+                print("codingPath: \(context.codingPath)")
+            } catch {
+                print("Error decoding JSON: \(error)")
+            }
+
             if var nsData = entriesResponse {
                 DispatchQueue.main.async {
                     // transform NS data to look like Dex data

+ 31 - 3
LoopFollow/Extensions/ShareClientExtension.swift

@@ -10,9 +10,37 @@ import Foundation
 import ShareClient
 
 public struct ShareGlucoseData: Codable {
-   var sgv: Int
-   var date: TimeInterval
-   var direction: String?
+    var sgv: Int
+    var date: TimeInterval
+    var direction: String?
+
+    enum CodingKeys: String, CodingKey {
+        case sgv
+        case date
+        case direction
+    }
+
+    public init(from decoder: Decoder) throws {
+        let container = try decoder.container(keyedBy: CodingKeys.self)
+
+        if let sgvAsDouble = try? container.decode(Double.self, forKey: .sgv) {
+            sgv = Int(sgvAsDouble.rounded())
+        } else if let sgvAsInt = try? container.decode(Int.self, forKey: .sgv) {
+            sgv = sgvAsInt
+        } else {
+            throw DecodingError.dataCorruptedError(forKey: .sgv, in: container, debugDescription: "Expected to decode an Integer or Double.")
+        }
+
+        // Decode the other properties
+        date = try container.decode(TimeInterval.self, forKey: .date)
+        direction = try container.decodeIfPresent(String.self, forKey: .direction)
+    }
+
+    public init(sgv: Int, date: TimeInterval, direction: String?) {
+        self.sgv = sgv
+        self.date = date
+        self.direction = direction
+    }
 }
 
 private var TrendTable: [String] = [