# class-json

> JSON serializer/derserializer with decorators

Latest version **0.4.2** (published 2024-11-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install class-json
pnpm add class-json
yarn add class-json
bun add class-json
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.2 |
| Published | 2024-11-26 |
| First published | 2019-09-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 176.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Alexander Kit |
| Maintainers | tenbits |

## Links

- npm: https://www.npmjs.com/package/class-json
- Repository: https://github.com/atmajs/class-json
- Homepage: https://github.com/atmajs/class-json#readme
- Issues: https://github.com/atmajs/class-json/issues
- npm.io page: https://npm.io/package/class-json

## Recent versions

- 0.4.2 (latest) — 2024-11-26
- 0.4.1 — 2024-07-17
- 0.3.8 — 2023-07-27
- 0.3.7 — 2021-07-29
- 0.3.6 — 2021-07-21
- 0.3.5 — 2021-07-14
- 0.3.4 — 2021-07-14
- 0.3.3 — 2020-11-18
- 0.3.2 — 2020-09-14
- 0.3.1 — 2020-05-26
- 0.3.0 — 2020-05-26
- 0.2.99 — 2020-05-25
- 0.2.98 — 2020-05-25
- 0.2.97 — 2020-05-25
- 0.2.95 — 2020-05-13
- … 33 more at https://npm.io/package/class-json/versions

## README

<h1 align='center'>
    <b><code>{</code></b>
    <code>class:json</code>
    <b><code>}</code></b>
</h1>

<p align="center">
    <a href='https://travis-ci.com/atmajs/class-json' target='_blank'>
        <img src='https://travis-ci.com/atmajs/class-json.png?branch=master' />
    </a>
    <a href='http://badge.fury.io/js/class-json' target='_blank'>
        <img src='https://badge.fury.io/js/class-json.svg' />
    </a>
</p>


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<T>`

```ts
interface Serializable<T> {

    constructor (partial: Partial<T>)

    /** 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<T>(model, settings): T`


---

© 2021 Atmajs

---
_Source: https://npm.io/package/class-json · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
