From d81475adc4249d2fb17456efa2d60bbdb92e4947 Mon Sep 17 00:00:00 2001 From: Vlad Alexa Date: Tue, 14 Aug 2018 13:06:26 +0100 Subject: [PATCH] 6262d10 continued --- GRDB/Record/PersistableRecord+Encodable.swift | 48 ++++++++++++------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/GRDB/Record/PersistableRecord+Encodable.swift b/GRDB/Record/PersistableRecord+Encodable.swift index 9308326b7..e7f4005b0 100644 --- a/GRDB/Record/PersistableRecord+Encodable.swift +++ b/GRDB/Record/PersistableRecord+Encodable.swift @@ -46,16 +46,22 @@ private struct PersistableRecordKeyedEncodingContainer : KeyedEn try value.encode(to: PersistableRecordEncoder(codingPath: [key], encode: encode)) } catch { // If value.encode does not work e.g. "unkeyed encoding is not supported" then see if model can be stored as JSON - do { - let encoder = JSONEncoder() - if #available(watchOS 4.0, OSX 10.13, iOS 11.0, *) { - encoder.outputFormatting = .sortedKeys + switch error as! EncodingError { + case .invalidValue(_, let context): + if context.debugDescription == "unkeyed encoding is not supported" { + // Support for keyed containers ( [Codable] ) + let encoder = JSONEncoder() + if #available(watchOS 4.0, OSX 10.13, iOS 11.0, *) { + encoder.outputFormatting = .sortedKeys + } + let json = try encoder.encode(value) + //the Data from the encoder is guaranteed to convert to String + let modelAsString = String(data: json, encoding: .utf8)! + return encode(modelAsString, key.stringValue) + } else { + throw(error) } - let json = try encoder.encode(value) - //the Data from the encoder is guaranteed to convert to String - let modelAsString = String(data: json, encoding: .utf8)! - return encode(modelAsString, key.stringValue) - } catch { + default: throw(error) } } @@ -162,16 +168,22 @@ private struct DatabaseValueEncodingContainer : SingleValueEncodingContainer { try value.encode(to: PersistableRecordEncoder(codingPath: [key], encode: encode)) } catch { // If value.encode does not work e.g. "unkeyed encoding is not supported" then see if model can be stored as JSON - do { - let encoder = JSONEncoder() - if #available(watchOS 4.0, OSX 10.13, iOS 11.0, *) { - encoder.outputFormatting = .sortedKeys + switch error as! EncodingError { + case .invalidValue(_, let context): + if context.debugDescription == "unkeyed encoding is not supported" { + // Support for keyed containers ( [Codable] ) + let encoder = JSONEncoder() + if #available(watchOS 4.0, OSX 10.13, iOS 11.0, *) { + encoder.outputFormatting = .sortedKeys + } + let json = try encoder.encode(value) + //the Data from the encoder is guaranteed to convert to String + let modelAsString = String(data: json, encoding: .utf8)! + return encode(modelAsString, key.stringValue) + } else { + throw(error) } - let json = try encoder.encode(value) - //the Data from the encoder is guaranteed to convert to String - let modelAsString = String(data: json, encoding: .utf8)! - return encode(modelAsString, key.stringValue) - } catch { + default: throw(error) } }