Skip to content

Commit

Permalink
Added documentation for deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
janwuesten committed Jan 19, 2024
1 parent 5caf952 commit 2f7ea76
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 186 deletions.
59 changes: 59 additions & 0 deletions docs/docs/definable/deserialize.mdx
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)
```
4 changes: 2 additions & 2 deletions docs/docs/definable/events.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 3
sidebar_position: 5
---

# Events
Expand All @@ -9,7 +9,7 @@ sidebar_position: 3
By using events you can execture actions everytime your data is serialized or deserialized.
While most of the times events aren't necessary as you can use the serializer and deserializer to modify your class properties, events can be helpful in many situations.

Events are also called when only serializing or deserializing partial properties.
Events are also called when only [serializing](/definable/docs/definable/serialize#serialize-only-certain-properties) or [deserializing](/definable/docs/definable/deserialize#deserialize-only-certain-properties) partial properties.

Possible events are:
- `deserialize.after`: Will be executed after the deserialization is done.
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/definable/serialize.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Country extends Definable {
.useDeserializer<string>((data) => this.code = data ?? "")
.useSerializer<string>(() => this.code)
useProp("population")
.useDeserializer<number>((data) => this.population = data ?? "")
.useDeserializer<number>((data) => this.population = data ?? 0)
.useSerializer<number>(() => this.population)
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/definable/validation.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 2
sidebar_position: 4
---

# Validation
Expand Down
182 changes: 0 additions & 182 deletions test/performance.test.ts

This file was deleted.

0 comments on commit 2f7ea76

Please sign in to comment.