Skip to content

Commit 66bbb65

Browse files
committed
feat: allow use without decorators
1 parent 1c1c946 commit 66bbb65

13 files changed

Lines changed: 554 additions & 160 deletions

README.md

Lines changed: 126 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ When I was developing a electron app I needed a serializer that fill some requir
3131

3232
`npm install typescript-flat-serializer --save`
3333

34-
You also need to set **experimentalDecorators** and **emitDecoratorMetadata** to true into the tsconfig.json file.
34+
## Usage
35+
36+
### With decorators
37+
38+
#### Preparation
39+
40+
If you want to use our decorators you also need to set **experimentalDecorators** and **emitDecoratorMetadata**
41+
to true into the tsconfig.json file.
3542

3643
For example:
3744

@@ -46,12 +53,12 @@ For example:
4653
}
4754
```
4855

49-
## Usage
56+
#### Usage
5057

5158
First let's create some models
5259

5360
```typescript
54-
import {TSFlatCollection, TSFlatObject} from "typescript-flat-serializer";
61+
import { TSFlatCollection, TSFlatObject } from "typescript-flat-serializer";
5562

5663
@TSFlatObject()
5764
export abstract class Animal {
@@ -84,7 +91,7 @@ export class Food extends Animal {
8491
Now we only need to serialize/deserialize the animal.
8592

8693
```typescript
87-
import {parse, stringify} from "typescript-flat-serializer";
94+
import { parse, stringify } from "typescript-flat-serializer";
8895

8996

9097
const foods = new Set([new Food('all')])
@@ -100,6 +107,67 @@ const parsedAnimal = parse<Animal>(str);
100107

101108
You can find another examples of utilisation on [tests](https://github.com/ynixt/typescript-flat-serializer/tree/master/test).
102109

110+
### Without decorators
111+
112+
First let's create some models
113+
114+
```typescript
115+
import { registerTSFlat } from "typescript-flat-serializer";
116+
117+
export abstract class Animal {
118+
protected constructor(public name: string) {
119+
registerTSFlat(
120+
{
121+
target: Animal,
122+
}
123+
);
124+
}
125+
}
126+
127+
export class Dog extends Animal {
128+
favoriteFoods: Set<Food>;
129+
130+
constructor(name: string, public beautiful: boolean, favoriteFoods: Set<Food>) {
131+
super(name);
132+
registerTSFlat(
133+
{
134+
target: Dog,
135+
},
136+
{ collectionName: 'favoriteFoods', options: {collectionType: 'set'} }
137+
)
138+
this.favoriteFoods = favoriteFoods;
139+
}
140+
}
141+
142+
export class Food extends Animal {
143+
constructor(name: string) {
144+
super(name);
145+
registerTSFlat(
146+
{
147+
target: Food,
148+
},
149+
)
150+
}
151+
}
152+
```
153+
154+
Now we only need to serialize/deserialize the animal.
155+
156+
```typescript
157+
import {parse, stringify} from "typescript-flat-serializer";
158+
159+
160+
const foods = new Set([new Food('all')])
161+
const animal: Animal = new Dog('Luffy', true, foods);
162+
163+
// Let's go serialize our animal
164+
const str = stringify(animal)
165+
// value of str: [{"name":"Luffy","beautiful":true,"favoriteFoods":"#_1_#","__class__":"Dog"},["#_2_#"],{"name":"all","__class__":"Food"}]
166+
167+
// And now we can deserialize the animal
168+
const parsedAnimal = parse<Animal>(str);
169+
```
170+
103171
## API
104172

105173
### Decorators
@@ -165,6 +233,28 @@ Description: The option to customize the serialization/deserialization of the ta
165233

166234
### Methods
167235

236+
#### **registerTSFlat**
237+
238+
Used do register an object and its items.
239+
240+
```
241+
registerTSFlat<T>(objectRegistration: FlatObjectRegistration<T>, ...items: FlatItem<T>[])
242+
```
243+
244+
##### **Parameters**
245+
246+
**objectRegistration**
247+
248+
Type: `FlatObjectRegistration<T>`
249+
Optional: `false`
250+
Description: Information about the class that we want to make serializable.
251+
252+
Type: `FlatItem<T>[]`
253+
Optional: `true`
254+
Description: Items that this class have that we want to make serializable.
255+
256+
---
257+
168258
#### **stringify**
169259

170260
Used to serialize a object.
@@ -209,6 +299,38 @@ parse<T>(str: string): T
209299

210300
#### **Types**
211301

302+
303+
##### **FlatObjectRegistration**
304+
305+
```typescript
306+
export type FlatObjectRegistration<T> = {
307+
target: Type<T>,
308+
options?: TSFlatObjectProperties
309+
}
310+
```
311+
312+
##### **FlatItem\<T>**
313+
314+
```typescript
315+
export type FlatItem<T> = FlatPropertyRegistration<T> | FlatCollectionRegistration<T>;
316+
```
317+
318+
##### **FlatPropertyRegistration\<T>**
319+
320+
```typescript
321+
export type FlatPropertyRegistration<T> = {
322+
propertyName: (keyof T), options?: TSFlatPropertyOptions,
323+
}
324+
```
325+
326+
##### **FlatCollectionRegistration\<T>**
327+
328+
```typescript
329+
export type FlatCollectionRegistration<T> = {
330+
collectionName: (keyof T), options: TSFlatCollectionOptions
331+
}
332+
```
333+
212334
##### **CollectionTypeString**
213335
214336
```typescript

0 commit comments

Comments
 (0)