https://github.com/decoverto/decoverto
Convert data to classes using @decorators
https://github.com/decoverto/decoverto
decorators deserialization json parse serialization typescript
Last synced: about 1 year ago
JSON representation
Convert data to classes using @decorators
- Host: GitHub
- URL: https://github.com/decoverto/decoverto
- Owner: decoverto
- License: mit
- Created: 2021-03-09T08:44:14.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-11-11T11:14:31.000Z (over 1 year ago)
- Last Synced: 2025-03-23T23:37:01.073Z (over 1 year ago)
- Topics: decorators, deserialization, json, parse, serialization, typescript
- Language: TypeScript
- Homepage:
- Size: 2.34 MB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/decoverto)
[
](https://github.com/decoverto/decoverto/actions)
[
](https://app.codecov.io/gh/decoverto/decoverto)
[
](https://github.com/decoverto/decoverto/blob/master/LICENSE)
# Decoverto
Convert data into instances of first and third party classes and back using decorators. Enjoy not having to write data-to-class setup code.
**[Test and tinker on the Decoverto playground](https://codesandbox.io/s/github/decoverto/playground?file=/index.ts)**
## Features
- [Convert object literals to and from instances](docs/conversion.md)
- [Multilevel inheritance](docs/inheritance.md)
- [Using discriminators](docs/inheritance.md#discriminator-strategy)
- [Using a predicate](docs/inheritance.md#predicate-strategy)
- [Reflect property types and stay DRY](docs/defining-properties.md#reflect-metadata)
- [Make your own converters](docs/defining-properties.md#mapping-types) Handy for third-party types
- Builtin support for:
- `number`, `string`, `Date`, `ArrayBuffer`, `DataView`, `TypedArray`
- [Collections such as `Array`, `Set`, `Map`](docs/defining-properties.md#collections)
- [Deferred types](docs/defining-properties.md#passing-the-type-to-the-decorator)
- Convert any raw data:
- [Use your own parser](docs/parser.md#custom-parser)
- [Configure the default JSON parser](docs/parser.md#customize-json-parser)
- Works in browser and Node
- Use TypeScript ([playground](https://codesandbox.io/s/github/decoverto/playground?file=/index.ts)) or JavaScript with Babel ([playground](https://codesandbox.io/s/github/decoverto/example-javascript?file=/index.js))
A model looks like this:
```TypeScript
import {model, map, MapShape, property} from 'decoverto';
@model()
class User {
@property()
createdAt: Date;
@property()
givenName: string;
@property(() => String)
referralToken: string | null;
@property(map(() => String, () => Boolean, {shape: MapShape.Object}))
permissions: Map;
}
```
### Quickstart
1. Install decoverto
```shell
yarn add decoverto
```
or
```shell
npm install decoverto
```
1. When using TypeScript, enable `experimentalDecorators` in `tsconfig.json`
1. Define a model
```TypeScript
import {model, map, MapShape, property} from 'decoverto';
@model()
class User {
@property(() => Date)
createdAt: Date;
@property(() => String)
givenName: string;
@property(() => String)
referralToken: string | null;
@property(map(() => String, () => Boolean, {shape: MapShape.Object}))
permissions: Map;
}
```
1. Create a Decoverto instance `const decoverto = new Decoverto()`
1. Convert some data
```TypeScript
// Convert raw data using the default JSON parser
user = decoverto.type(User).rawToInstance(`{
"createdAt": "2021-03-31T14:08:42.009Z",
"givenName": "Mark",
"referralToken": null,
"permissions": {
"canManageUsers": true,
"canCreateProducts": true
}
}`);
// Convert an object literal
user = decoverto.type(User).plainToInstance({
createdAt: new Date(),
givenName: 'Mark',
referralToken: null,
permissions: {
canManageUsers: true,
canCreateProducts: true,
}
});
```
For more information, see links in the [features list](#features) or the [docs directory](docs).
## Examples/Playground
Decoverto has playgrounds where you can view examples and tinker with them. It allows you to see what is possible and experiment.
- [TypeScript playground](https://codesandbox.io/s/github/decoverto/playground?file=/index.ts) This is the main playground.
- [JavaScript + Babel playground](https://codesandbox.io/s/github/decoverto/example-javascript?file=/index.js)