mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
JSON encoding: choose strategies
Data: .base64 Date: .millisecondsSince1970 NonConformingFloat: .throw
This commit is contained in:
1 parent
659db4fdd9
commit
2eb0955420
2 files changed
+24
-14
No files matched your search
@@ -92,7 +92,7 @@ private struct RowKeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContainer
|
||||
guard let data = row.dataNoCopy(atIndex: index) else {
|
||||
fatalConversionError(to: T.self, from: row[index], conversionContext: ValueConversionContext(row).atColumn(index))
|
||||
}
|
||||
return try JSONDecoder().decode(type.self, from: data)
|
||||
return try makeJSONDecoder().decode(type.self, from: data)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -142,7 +142,7 @@ private struct RowKeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContainer
|
||||
guard let data = row.dataNoCopy(atIndex: index) else {
|
||||
fatalConversionError(to: T.self, from: row[index], conversionContext: ValueConversionContext(row).atColumn(index))
|
||||
}
|
||||
return try JSONDecoder().decode(type.self, from: data)
|
||||
return try makeJSONDecoder().decode(type.self, from: data)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -303,6 +303,14 @@ private struct RowSingleValueDecoder: Decoder {
|
||||
/// The error that triggers JSON decoding
|
||||
private struct JSONRequiredError: Error { }
|
||||
|
||||
func makeJSONDecoder() -> JSONDecoder {
|
||||
let encoder = JSONDecoder()
|
||||
encoder.dataDecodingStrategy = .base64
|
||||
encoder.dateDecodingStrategy = .millisecondsSince1970
|
||||
encoder.nonConformingFloatDecodingStrategy = .throw
|
||||
return encoder
|
||||
}
|
||||
|
||||
extension FetchableRecord where Self: Decodable {
|
||||
/// Initializes a record from `row`.
|
||||
public init(row: Row) {
|
||||
|
||||
@@ -50,12 +50,7 @@ private struct PersistableRecordKeyedEncodingContainer<Key: CodingKey> : KeyedEn
|
||||
try value.encode(to: DatabaseValueEncoder(key: key, encode: encode))
|
||||
} catch is JSONRequiredError {
|
||||
// Encode to JSON
|
||||
let encoder = JSONEncoder()
|
||||
if #available(watchOS 4.0, OSX 10.13, iOS 11.0, *) {
|
||||
// guarantee some stability in order to ease record comparison
|
||||
encoder.outputFormatting = .sortedKeys
|
||||
}
|
||||
let jsonData = try encoder.encode(value)
|
||||
let jsonData = try makeJSONEncoder().encode(value)
|
||||
|
||||
// Store JSON String in the database for easier debugging and
|
||||
// database inspection. Thanks to SQLite weak typing, we won't
|
||||
@@ -176,12 +171,7 @@ private struct DatabaseValueEncodingContainer : SingleValueEncodingContainer {
|
||||
try value.encode(to: DatabaseValueEncoder(key: key, encode: encode))
|
||||
} catch is JSONRequiredError {
|
||||
// Encode to JSON
|
||||
let encoder = JSONEncoder()
|
||||
if #available(watchOS 4.0, OSX 10.13, iOS 11.0, *) {
|
||||
// guarantee some stability in order to ease record comparison
|
||||
encoder.outputFormatting = .sortedKeys
|
||||
}
|
||||
let jsonData = try encoder.encode(value)
|
||||
let jsonData = try makeJSONEncoder().encode(value)
|
||||
|
||||
// Store JSON String in the database for easier debugging and
|
||||
// database inspection. Thanks to SQLite weak typing, we won't
|
||||
@@ -365,6 +355,18 @@ private struct JSONRequiredError: Error { }
|
||||
|
||||
private typealias DatabaseValuePersistenceEncoder = (_ value: DatabaseValueConvertible?, _ key: String) -> Void
|
||||
|
||||
func makeJSONEncoder() -> JSONEncoder {
|
||||
let encoder = JSONEncoder()
|
||||
encoder.dataEncodingStrategy = .base64
|
||||
encoder.dateEncodingStrategy = .millisecondsSince1970
|
||||
encoder.nonConformingFloatEncodingStrategy = .throw
|
||||
if #available(watchOS 4.0, OSX 10.13, iOS 11.0, *) {
|
||||
// guarantee some stability in order to ease record comparison
|
||||
encoder.outputFormatting = .sortedKeys
|
||||
}
|
||||
return encoder
|
||||
}
|
||||
|
||||
extension MutablePersistableRecord where Self: Encodable {
|
||||
public func encode(to container: inout PersistenceContainer) {
|
||||
// The inout container parameter won't enter an escaping closure since
|
||||
|
||||
Reference in new issue
Block a user