JSONConverter.swift 740 B

12345678910111213141516171819202122
  1. import Foundation
  2. class JSONConverter {
  3. /// this is temporarily used to parse the fetched Core Data objects to JSON in order to pass it to DetermineBasal()
  4. func convertToJSON<T: Encodable>(_ value: T?) -> String {
  5. guard let value = value else { return "" }
  6. let encoder = JSONEncoder()
  7. encoder.keyEncodingStrategy = .useDefaultKeys
  8. do {
  9. let jsonData = try encoder.encode(value)
  10. if let jsonString = String(data: jsonData, encoding: .utf8) {
  11. return jsonString
  12. }
  13. } catch {
  14. debugPrint("\(DebuggingIdentifiers.failed) could not convert object to JSON: \(error)")
  15. }
  16. return "could not convert object to JSON"
  17. }
  18. }