You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/06-concepts/02-models.md
+41Lines changed: 41 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -64,6 +64,46 @@ fields:
64
64
Serverpod's models can easily be saved to or read from the database. You can read more about this in the [Database](database/models) section.
65
65
:::
66
66
67
+
### JSON key aliasing
68
+
69
+
By default, fields are serialized to JSON using their Dart field name as the key. The `jsonKey` property allows you to specify a different key name for JSON serialization and deserialization, which is useful when integrating with external APIs that use different naming conventions.
70
+
71
+
```yaml
72
+
class: User
73
+
fields:
74
+
displayName: String, jsonKey=display_name
75
+
emailAddress: String, jsonKey=email
76
+
createdAt: DateTime, jsonKey=created_at
77
+
```
78
+
79
+
This generates a class where the Dart field names remain camelCase, but the JSON representation uses the specified keys:
- Consuming external APIs that use snake_case or other naming conventions
100
+
- Working with legacy systems that have specific JSON field requirements
101
+
- Integrating with third-party services like MongoDB (e.g., mapping `id` to `_id`)
102
+
103
+
:::info
104
+
The `jsonKey` property affects JSON serialization and deserialization. It does not affect the database column name. To customize the database column name, use the `column` property instead. This is an experimental feature; see the [Experimental documentation](experimental#column-name-override) under *Column name override* for details.
105
+
:::
106
+
67
107
### Immutable classes
68
108
69
109
By default, generated classes in Serverpod are mutable, meaning their fields can be changed after creation. However, you can make a class immutable by setting the `immutable` property to `true`. Immutable classes are especially useful when working with state management solutions or when you need value-based equality.
@@ -782,6 +822,7 @@ fields:
782
822
| [**type (fields)**](#class) | Denotes the data type for a field. | ✅ | ✅ | |
783
823
| [**required**](#required-fields) | Makes the field as required. This keyword can only be used for **nullable** fields. | ✅ | ✅ | |
784
824
| [**scope**](#limiting-visibility-of-a-generated-class) | Denotes the scope for a field. | ✅ | | |
825
+
| [**jsonKey**](#json-key-aliasing) | Sets a custom key name for JSON serialization and deserialization. | ✅ | | |
785
826
| [**persist**](database/models) | A boolean flag if the data should be stored in the database or not can be negated with `!persist` | ✅ | | |
786
827
| [**relation**](database/relations/one-to-one) | Sets a relation between model files, requires a table name to be set. | ✅ | | |
787
828
| [**name**](database/relations/one-to-one#bidirectional-relations) | Give a name to a relation to pair them. | ✅ | | |
0 commit comments