mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Document the default DatabaseValueConvertible+Codable implementation
This commit is contained in:
1 parent
620eb6f21c
commit
ad48aa6eec
1 file changed
+17
@@ -1552,6 +1552,23 @@ The `databaseValue` property returns [DatabaseValue](#databasevalue), a type tha
|
||||
|
||||
The `fromDatabaseValue()` factory method returns an instance of your custom type if the database value contains a suitable value. If the database value does not contain a suitable value, such as "foo" for Date, `fromDatabaseValue` *must* return nil (GRDB will interpret this nil result as a conversion error, and react accordingly).
|
||||
|
||||
Value types that adopt both `DatabaseValueConvertible` and an archival protocol ([Codable, Encodable or Decodable](https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types)) are automatically coded and decoded from JSON arrays and objects:
|
||||
|
||||
```swift
|
||||
// Encoded as a JSON object in the database:
|
||||
struct Color: Codable, DatabaseValueConvertible {
|
||||
var r: Double
|
||||
var g: Double
|
||||
var b: Double
|
||||
}
|
||||
```
|
||||
|
||||
For such codable value types, GRDB uses the standard [JSONDecoder](https://developer.apple.com/documentation/foundation/jsondecoder) and [JSONEncoder](https://developer.apple.com/documentation/foundation/jsonencoder) from Foundation. By default, Data values are handled with the `.base64` strategy, Date with the `.millisecondsSince1970` strategy, and non conforming floats with the `.throw` strategy.
|
||||
|
||||
In order to customize the JSON format, provide a custom implementation of the `DatabaseValueConvertible` requirements.
|
||||
|
||||
> :point_up: **Note**: standard sequences such as `Array`, `Set`, or `Dictionary` do not conform to `DatabaseValueConvertible`, even conditionally. You won't be able to directly fetch or store arrays, sets, or dictionaries as JSON database values. You can get free JSON support from these standard types when they are embedded as properties of [Codable Records], though.
|
||||
|
||||
|
||||
## Transactions and Savepoints
|
||||
|
||||
|
||||
Reference in new issue
Block a user