Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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