mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
added catch
This commit is contained in:
1 parent
2826472c19
commit
6262d10794
1 file changed
+17
-1
@@ -158,7 +158,23 @@ private struct DatabaseValueEncodingContainer : SingleValueEncodingContainer {
|
|||||||
// This allows us to encode Date as String, for example.
|
// This allows us to encode Date as String, for example.
|
||||||
encode(dbValueConvertible.databaseValue, key.stringValue)
|
encode(dbValueConvertible.databaseValue, key.stringValue)
|
||||||
} else {
|
} else {
|
||||||
try value.encode(to: PersistableRecordEncoder(codingPath: [key], encode: encode))
|
do {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
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 {
|
||||||
|
throw(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user