-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added documentation for deserialization
- Loading branch information
1 parent
5caf952
commit 2f7ea76
Showing
5 changed files
with
63 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
sidebar_position: 3 | ||
--- | ||
|
||
# Deserialize | ||
|
||
For this documentation page the following `Definable` class will be used: | ||
```ts | ||
import { Definable, DefinableDefinition } from "definable" | ||
|
||
class Country extends Definable { | ||
name: string = "Germany" | ||
code: string = "DE" | ||
population: number = 83200000 | ||
|
||
definition({ useProp }: DefinableDefinition): void { | ||
useProp("name") | ||
.useDeserializer<string>((data) => this.name = data ?? "") | ||
.useSerializer<string>(() => this.name) | ||
useProp("code") | ||
.useDeserializer<string>((data) => this.code = data ?? "") | ||
.useSerializer<string>(() => this.code) | ||
useProp("population") | ||
.useDeserializer<number>((data) => this.population = data ?? 0) | ||
.useSerializer<number>(() => this.population) | ||
} | ||
} | ||
``` | ||
|
||
## Basic deserialization | ||
|
||
Basic deserialization can be done with the `deserialize()` method that every `Definable` has. | ||
This will deserialize all properties from a JSON data object to the class properties to be used as a class. | ||
|
||
```ts | ||
// data = { name: "Germany", code: "DE", population: 83200000 } | ||
const country = new County().deserialize(data) | ||
``` | ||
|
||
## Advanced deserialization | ||
|
||
You can parse some options to the `deserialize()` method to change the behavior of the deserialized JSON data. | ||
|
||
### Deserialize only certain properties | ||
|
||
While most of the times deserialization of the whole JSON data is recommended, sometimes deserializing the whole JSON data isn't the ideal solution. | ||
You can pass an array of property names (`string`) to the `props` option to deserialize only certain properties. | ||
|
||
This will then use the default value returned by your deserializer (`useDeserializer`) for this property instead of the actual value. | ||
|
||
```ts | ||
// data = { name: "Germany", code: "DE", population: 83200000 } | ||
const country = new County().deserialize({ | ||
props: ["name", "code"] | ||
}) | ||
console.log(country.name) // "Germany" | ||
console.log(country.code) // "DE" | ||
console.log(country.population) // 0 (default value) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 2 | ||
sidebar_position: 4 | ||
--- | ||
|
||
# Validation | ||
|
This file was deleted.
Oops, something went wrong.