Skip to content

Commit 8ea0617

Browse files
committed
Mention different json key styles for drit/dart
1 parent 7295a3a commit 8ea0617

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

docs/docs/dart_api/rows.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,18 +441,28 @@ Generated row classes can be converted from and to JSON:
441441

442442
### Key names
443443

444-
By default, drift uses column names in `snake_case` as JSON keys:
444+
For tables and views defined as Dart classes, drift uses the name of the getter defining columns as
445+
a JSON key:
445446

446447
{{ load_snippet('default-json-keys','lib/snippets/dart_api/dataclass.dart.excerpt.json') }}
447448

448449
```json
449450
{
450451
"id": 1,
451452
"title": "Todo 1",
452-
"created_at": "2024-02-29T12:00:00Z"
453+
"createdAt": "2024-02-29T12:00:00Z"
453454
}
454455
```
455456

457+
For elements defined in [drift files](../sql_api/drift_files.md), drift uses the name of the column
458+
in SQL instead. This can be a problem when mixing the two declarations. A view defined in a drift file
459+
that selects from the Dart table would have different JSON keys, for instance:
460+
461+
```sql
462+
-- JSON keys are id, title, created_at
463+
CREATE VIEW todos_view AS SELECT * FROM todos;
464+
```
465+
456466
#### Custom json keys
457467

458468
To use a custom name for JSON serialization, use the `@JsonKey` annotation.

0 commit comments

Comments
 (0)