Skip to content

Commit 19c73ad

Browse files
authored
docs: Added documentation on new jsonKey model alias feature (#405)
1 parent 4aa6c16 commit 19c73ad

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

docs/06-concepts/02-models.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,46 @@ fields:
6464
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.
6565
:::
6666

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:
80+
81+
```dart
82+
// Dart field names
83+
var user = User(
84+
displayName: 'John Doe',
85+
emailAddress: 'john@example.com',
86+
createdAt: DateTime.parse('2024-01-15T10:30:00.000Z'),
87+
);
88+
89+
// Serializes to JSON with custom keys
90+
// {
91+
// "display_name": "John Doe",
92+
// "email": "john@example.com",
93+
// "created_at": "2024-01-15T10:30:00.000Z"
94+
// }
95+
```
96+
97+
This is particularly helpful when:
98+
99+
- 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+
67107
### Immutable classes
68108

69109
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:
782822
| [**type (fields)**](#class) | Denotes the data type for a field. | ✅ | ✅ | |
783823
| [**required**](#required-fields) | Makes the field as required. This keyword can only be used for **nullable** fields. | ✅ | ✅ | |
784824
| [**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. | ✅ | | |
785826
| [**persist**](database/models) | A boolean flag if the data should be stored in the database or not can be negated with `!persist` | ✅ | | |
786827
| [**relation**](database/relations/one-to-one) | Sets a relation between model files, requires a table name to be set. | ✅ | | |
787828
| [**name**](database/relations/one-to-one#bidirectional-relations) | Give a name to a relation to pair them. | ✅ | | |

0 commit comments

Comments
 (0)