Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/atmajs/class-json
JSON-Serialization Decorators
https://github.com/atmajs/class-json
Last synced: 6 days ago
JSON representation
JSON-Serialization Decorators
- Host: GitHub
- URL: https://github.com/atmajs/class-json
- Owner: atmajs
- Created: 2019-09-22T20:14:03.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-07-17T23:16:26.000Z (4 months ago)
- Last Synced: 2024-10-29T20:56:27.480Z (16 days ago)
- Language: JavaScript
- Size: 464 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
{
class:json
}
Comprehensive JSON library for a class.
> with TypeScript support
1. Decorators
1.1 Converting* `@Json.type(Ctor: Function, options?)`
* `@Json.array(Ctor: Function, options?)`
* `@Json.name(jsonName: string)`
* `@Json.ignore()`
* `@Json.converter(converter: IJsonConverter)`1.2 Validation
* `@Rule.required(message?)`
* `@Rule.minLength(count: number, message?)`
* `@Rule.maxLength(count: number, message?)`
* `@Rule.minimum(val: number, message?)`
* `@Rule.maximum(val: number, message?)`
* `@Rule.pattern(rgx: RegExp, message?)`
* `@Rule.validate(validator: IValueValidator)`Message Type:
* string
* string templates: `Interpolations: ~[value] ~[property] ~[model.foo]`
* Custom Function: `(value, model) => string````ts
import { Json, Rule } from 'class-json'class Transaction {
@Json.type(BigInt)
@Rule.minimum(100n)
value: bigint@Json.type(Date)
@Ruke.required()
executeAt: Date
}
```2. Classes
* `Serializable`
```ts
interface Serializable {constructor (partial: Partial)
/** Create an instance from json, all Types from decorators will be restored. */
static fromJSON (json): T/** Use Rules from decorators to validate the instance */
static validate (instance: T): IRuleError[]/** Serialize instance to JSON object. All Types from decorators will be properly converted to JSON supported types */
toJSON(): object
}
```> Constructor: accepts partial object, example:
```ts
// instead of:
let foo = new Foo();
foo.lorem = 'lorem';
foo.ipsum = 'ipsum';// more convinient way would be:
let foo = new Foo({
lorem: 'lorem',
ipsum: 'ipsum'
});
```3. Namespaces
* `JsonConvert`
* `toJSON(model, settings)`
* `fromJSON(model, settings): T`---
© 2021 Atmajs