-
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.
Add maxDepth option to JsonthisOptions and ToJsonOptions to limit de…
…pth of traversal (#6) * Add maxDepth option to JsonthisOptions and ToJsonOptions to limit depth of traversal * Add unit tests for maxDepth option * Update README.md with new "Limit Serialization Depth" section
- Loading branch information
1 parent
395dc71
commit fcb0882
Showing
4 changed files
with
100 additions
and
0 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 |
---|---|---|
|
@@ -32,6 +32,7 @@ for your data management needs. Explore the [Sequelize support](#sequelize-suppo | |
+ [Global Serializer](#global-serializer) | ||
+ [Field-Specific Serializer](#field-specific-serializer) | ||
* [Contextual Field-Specific Serializer](#contextual-field-specific-serializer) | ||
* [Limit Serialization Depth](#limit-serialization-depth) | ||
- [Circular References](#circular-references) | ||
- [Sequelize support](#sequelize-support) | ||
- [Let's Contribute Together!](#lets-contribute-together) | ||
|
@@ -229,6 +230,40 @@ console.log(jsonthis.toJson(user, {context: {maskChar: "-"}})); | |
// { id: 1, email: '[email protected]' } | ||
``` | ||
|
||
### Limit Serialization Depth | ||
|
||
You can limit the depth of serialization by setting the `maxDepth` option at global level in `JsonthisOptions` | ||
at construction time: | ||
|
||
```typescript | ||
class User { | ||
id: number; | ||
name: string; | ||
friend?: User; | ||
|
||
constructor(id: number, name: string) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
} | ||
|
||
const user = new User(1, "John"); | ||
user.friend = new User(2, "Jane"); | ||
user.friend.friend = new User(3, "Bob"); | ||
|
||
const jsonthis = new Jsonthis({maxDepth: 1}); | ||
console.log(jsonthis.toJson(user)); | ||
// { id: 1, name: 'John', friend: { id: 2, name: 'Jane' } } | ||
``` | ||
|
||
You can also set the `maxDepth` option at the method level in `ToJsonOptions`: | ||
|
||
```typescript | ||
const jsonthis = new Jsonthis(); | ||
console.log(jsonthis.toJson(user, {maxDepth: 1})); | ||
// { id: 1, name: 'John', friend: { id: 2, name: 'Jane' } } | ||
``` | ||
|
||
## Circular References | ||
|
||
Jsonthis can detect circular references out of the box. When serializing an object with circular references, the default | ||
|
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