From ad48aa6eec153ba8d6654ce7155c9f6dfd0bc0ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 4 Jul 2021 18:36:56 +0200 Subject: [PATCH] Document the default DatabaseValueConvertible+Codable implementation --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 409799a83..ac1045007 100644 --- a/README.md +++ b/README.md @@ -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