11
11
12
12
[ download-url ] : https://npmjs.org/package/jsonthis
13
13
14
- Jsonthis is the perfect TypeScript library to convert your models to JSON objects.
15
- It supports custom property serializers, conditionally-visible properties , and much more.
14
+ Jsonthis is a versatile TypeScript library designed to effortlessly convert your models into JSON objects.
15
+ It offers extensive support for custom property serializers, conditional property visibility , and more.
16
16
17
- Jsonthis is also the perfect companion to the [ Sequelize] ( https://sequelize.org/ ) ORM library!
18
- To learn more, check out the [ Sequelize support] ( #sequelize-support ) section.
17
+ Jsonthis seamlessly integrates with the [ Sequelize] ( https://sequelize.org/ ) ORM library, making it an ideal companion
18
+ for your data management needs. Explore the [ Sequelize support] ( #sequelize-support ) section for detailed instructions .
19
19
20
20
## Getting Started
21
21
22
- This is the simplest way to use Jsonthis :
22
+ Getting started with Jsonthis is quick and straightforward. Here's a simple example to get you going :
23
23
24
24
``` typescript
25
25
import {Json , JsonField , Jsonthis } from " jsonthis" ;
@@ -28,7 +28,7 @@ import {Json, JsonField, Jsonthis} from "jsonthis";
28
28
class User {
29
29
id: number ;
30
30
email: string ;
31
- @JsonField (false )
31
+ @JsonField (false ) // visible=false - the "password" property will not be included in the JSON output
32
32
password: string ;
33
33
registeredAt: Date = new Date ();
34
34
@@ -46,14 +46,14 @@ console.log(jsonthis.toJson(user));
46
46
// { id: 1, email: '[email protected] ', registeredAt: 2024-04-13T15:29:35.583Z }
47
47
```
48
48
49
- You can also use the ` @JsonField ` decorator to customize how ` Jsonthis ` serializes your properties.
50
- In the example above, the ` password ` property is hidden from the JSON output .
49
+ Additionally, the ` @JsonField ` decorator empowers you to fine-tune the serialization process of your properties
50
+ with Jsonthis. You can define custom serializers, change property visibility, and more .
51
51
52
- Jsonthis also supports custom serializers and serialization options.
52
+ ## Customizing Serialization
53
53
54
- ## Change property name casing
54
+ ### Change Property Name Casing
55
55
56
- You can enforce a specific casing for your properties in the JSON output.
56
+ Jsonthis allows you to enforce specific casing for property names in the JSON output.
57
57
By default, Jsonthis uses whatever casing you use in your TypeScript code,
58
58
but you can change it to ` camelCase ` , ` snake_case ` , or ` PascalCase ` :
59
59
@@ -76,13 +76,11 @@ console.log(new Jsonthis({case: "pascal"}).toJson(user));
76
76
// { Id: 123, UserName: 'john-doe', RegisteredAt: 2024-04-13T20:42:22.121Z }
77
77
```
78
78
79
- ## Change property visibility
80
-
81
- The simplest customization you can do is to hide a property from the JSON output.
82
- As shown in the "Getting Started" example, you can use the ` @JsonField ` decorator to hide a property.
79
+ ### Change Property Visibility
83
80
84
- You can pass the visible option directly to the ` @JsonField ` decorator,
85
- or you can use the ` JsonFieldOptions ` options object to specify more complex options:
81
+ You can hide a property from the JSON output by setting the ` visible ` option to ` false ` .
82
+ You can achieve this by passing ` false ` to the ` @JsonField ` decorator directly
83
+ or by using the ` JsonFieldOptions ` object:
86
84
87
85
``` typescript
88
86
@Json
@@ -94,10 +92,10 @@ class User {
94
92
}
95
93
```
96
94
97
- ### Conditional visibility
95
+ #### Conditional Visibility
98
96
99
- Jsonthis serialization supports a user-defined context object that can be used to influence the serialization process .
100
- You can use this feature to conditionally hide or show properties based on the context .
97
+ Jsonthis supports conditional property visibility based on a user-defined context .
98
+ This allows you to dynamically show or hide properties as needed .
101
99
102
100
In the following example, the ` email ` property is only visible if the email owner is requesting it:
103
101
@@ -133,7 +131,7 @@ console.log(jsonthis.toJson(user, {callerId: 2}));
133
131
// { id: 1 }
134
132
```
135
133
136
- Note that this also works with nested objects:
134
+ This also works with nested objects:
137
135
138
136
``` typescript
139
137
const user
= new User (
1 ,
" [email protected] " );
@@ -147,14 +145,14 @@ console.log(jsonthis.toJson(user, {callerId: 2}));
147
145
// { id: 1, friend: { id: 2, email: '[email protected] ' } }
148
146
```
149
147
150
- ## Custom serializers
148
+ ### Custom serializers
151
149
152
- You can use custom serializers to transform your properties in the final JSON output .
153
- Custom serializers can be ** Global ** or ** Field -specific** .
150
+ Jsonthis allows you to define custom serializers to transform property values during serialization .
151
+ These can be either ** global ** or ** field -specific** .
154
152
155
- ### Global serializer
153
+ #### Global Serializer
156
154
157
- You can register a global serializer for a specific type using the ` Jsonthis.registerGlobalSerializer() ` method :
155
+ Register a global serializer for a specific type using ` Jsonthis.registerGlobalSerializer() ` :
158
156
159
157
``` typescript
160
158
function dateSerializer(value : Date ): string {
@@ -175,9 +173,9 @@ console.log(jsonthis.toJson(user));
175
173
// { id: 1, registeredAt: 'Sat, 13 Apr 2024 15:50:35 GMT' }
176
174
```
177
175
178
- ### Field-specific serializer
176
+ #### Field-Specific Serializer
179
177
180
- You can use the ` @JsonField ` decorator to specify a custom serializer for a specific property:
178
+ Utilize the ` @JsonField ` decorator to specify a custom serializer for a specific property:
181
179
182
180
``` typescript
183
181
function maskEmail(value : string ): string {
@@ -197,7 +195,7 @@ console.log(jsonthis.toJson(user));
197
195
// { id: 1, email: 'j******[email protected] ' }
198
196
```
199
197
200
- ### Contextual field-specific serializer
198
+ #### Contextual Field-Specific Serializer
201
199
202
200
Jsonthis serialization supports a user-defined context object that can be used to further influence the serialization
203
201
process:
@@ -225,9 +223,8 @@ console.log(jsonthis.toJson(user, {maskChar: "-"}));
225
223
```
226
224
227
225
## Sequelize support
228
-
229
- Jsonthis can work seamlessly with the [ Sequelize] ( https://sequelize.org/ ) ORM library. In order to use Jsonthis with
230
- Sequelize, you need to specify it in library constructor:
226
+ Jsonthis seamlessly integrates with the [ Sequelize] ( https://sequelize.org/ ) ORM library.
227
+ To utilize Jsonthis with Sequelize, simply specify it in the library constructor:
231
228
232
229
``` typescript
233
230
const sequelize = new Sequelize ({ ... });
@@ -237,7 +234,8 @@ const jsonthis = new Jsonthis({
237
234
});
238
235
```
239
236
240
- Now you can use the ` toJSON() ` method with Sequelize models and Jsonthis will intercept the serialization process:
237
+ Now, Jsonthis will seamlessly intercept the serialization process when using the ` toJSON() ` method
238
+ with Sequelize models:
241
239
242
240
``` typescript
243
241
function maskEmail(value : string ): string {
0 commit comments