Skip to content

Commit d1ae077

Browse files
committed
docs: enhance serialization documentation
1 parent ca75a01 commit d1ae077

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

docs/basics/serializables.md

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -198,31 +198,6 @@ have a backing field or getter with the same name. This list showcases all possi
198198
!!! note "Annotations go on the getter"
199199
Annotations for fields must be placed on the getter itself, not on the constructor parameter.
200200

201-
## Default Values
202-
You can set default values for fields, which will be considered when deserializing and serializing
203-
the value of the field.
204-
205-
```dart
206-
@DefaultValue("Alex")
207-
String name;
208-
```
209-
210-
This will set the default value of the field to "Alex". If the **field is not present** in the serialized
211-
data, the **default value will be used**. If the field is present, the default value will be ignored.
212-
When the object is serialized, the serializer will check if the **value is equal to the default value**
213-
and will **omit the field** if it is.
214-
215-
216-
!!! note "Including the default value"
217-
If you want to include the field even if it is equal to the default
218-
value, you can set the `keep` parameter to `true`.
219-
220-
```dart
221-
@DefaultValue("Alex", keep: true)
222-
String name;
223-
```
224-
225-
226201
## Restrictions
227202
To make your serializable classes work with the serialization system, you must follow a few
228203
restrictions, some of them enforced by the code generator and some of them at runtime:
@@ -297,3 +272,20 @@ Depending on your prefer code style, you can use either the imperative or lambda
297272

298273
!!! note "Availability"
299274
The builder is only available for dataclasses and basic serializable classes.
275+
276+
## Special Annotations
277+
278+
Dogs include several annotations that can be used to modify the behavior of serialization and deserialization.
279+
Here is a (non-exhaustive) list of the most common annotations and their usage:
280+
281+
<div class="common-min-size-table" markdown="1">
282+
283+
| Annotation | Applicable To | Description |
284+
|--------------------------------------------------|----------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
285+
| `@PropertyName()` | **Field** | Sets the name of the field when serialized. This is useful if you want to use a different name for the field in the serialized data than in the class. |
286+
| `@EnumProperty()` | **Field** | The name of the enum constant can be overridden using the `name` parameter. Additionally, a single constant can be marked as `fallback` to handle invalid enum values. |
287+
| `@DefaultValue()` | **Field** | Sets a default value for the field. If the field is not present in the serialized data, the default value will be used. If the field is present, the default value will be ignored. If `keep` is set to `true`, the field will be included in the output even if it is equal to the default value. |
288+
| `@excludeNull` | **Field**, **Class** | Excludes the field from serialization if it is null. This is useful for fields that are optional and should not be serialized if they are not set. Can also be applied to a class to exclude all null values from serialization. |
289+
| `@LightweightMigration()` `@RevisionMigration()` | **Class** | Specifies migrations that can be applied before the data is deserialized. |
290+
291+
</div>

docs/stylesheets/extra.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,12 @@
7878
width: 20px;
7979
height: 20px;
8080
left: -8px;
81+
}
82+
83+
.common-min-size-table th:nth-of-type(1) {
84+
min-width: 200px;
85+
}
86+
87+
.common-min-size-table td:nth-of-type(1) {
88+
min-width: 200px;
8189
}

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ nav:
4848
- built/start.md
4949

5050
markdown_extensions:
51+
- tables
5152
- pymdownx.highlight:
5253
anchor_linenums: true
5354
line_spans: __span
@@ -62,6 +63,7 @@ markdown_extensions:
6263
- pymdownx.tabbed:
6364
alternate_style: true
6465
- attr_list
66+
- md_in_html
6567
- pymdownx.emoji:
6668
emoji_index: !!python/name:materialx.emoji.twemoji
6769
emoji_generator: !!python/name:materialx.emoji.to_svg

0 commit comments

Comments
 (0)