# @itsquarehub/json-serialization

> A typescript library for deserializing JSON to object and serializing object to JSON.

Latest version **0.0.5** (published 2021-03-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install @itsquarehub/json-serialization
pnpm add @itsquarehub/json-serialization
yarn add @itsquarehub/json-serialization
bun add @itsquarehub/json-serialization
```

## Health

**Score 25/100 (F)** — status: abandoned.

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.5 |
| Published | 2021-03-04 |
| First published | 2020-02-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 33.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Arrian Pascual |
| Maintainers | apascual-ish |
| Keywords | object-mapper, json-serialization, typescript mapper, object mapper, json serialization, json-serialization, typescript serialization, serializer |

## Links

- npm: https://www.npmjs.com/package/@itsquarehub/json-serialization
- Repository: https://github.com/apascual-pl/json-serialization
- Homepage: https://github.com/apascual-pl/json-serialization#readme
- Issues: https://github.com/apascual-pl/json-serialization/issues
- npm.io page: https://npm.io/package/@itsquarehub/json-serialization

## Dependencies (1)

- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.0.5 (latest) — 2021-03-04
- 0.0.4 — 2020-09-01
- 0.0.3 — 2020-09-01
- 0.0.2 — 2020-03-30
- 0.0.1 — 2020-02-05

## README

# json-serialization

> A typescript library for JSON object serialization.

## Install

```bash
npm install @itsquarehub/json-serialization
```

or

```bash
yarn add @itsquarehub/json-serialization
```

## Usage

> Note:
> Always use the JsonProperty() to decorate the property you want to serialize/deserialize,
> otherwise it will ignore it.

Example Model Class
```typescript
export class Person {
  @JsonProperty()
  public firstName: string = undefined;

  @JsonProperty()
  public lastName: string = undefined;

  public get fullName(): string {
    return `${this.firstName} ${this.lastName}`;
  }
}
```

Example on how to use the deserialization
```typescript
import { JsonProperty, deserialize } from '@itsquarehub/json-serialization';

const jsonObject: any = {
  firstName: 'John',
  lastName: 'Doe'
};

export class ApiService {
  public getPerson(): Person {
    return deserialize(Person, jsonObject);
  }
}
```

Example on how to use the serialization
```typescript
import { serialize } from '@itsquarehub/json-serialization';

const personObject: Person = new Person();
personObject.firstName = 'John';
personObject.lastName = 'Doe';

export class ApiService {

  public setPerson(): Person {
    return serialize(personObject);
  }
}
```

You can also set your custom deserializer or serializer and it will output based on the predicate
```typescript
import { deserialize, serialize, IJsonDeserializer, IJsonSerializer } from '@itsquarehub/json-serialization';

export enum Gender {
  Male = 1,
  Female = 2
}

export class GenderSerialization implements IJsonSerializer, IJsonDeserializer {
  public serialize(value: any): any {
    return `${Gender[value]}`;
  }

  public deserialize(value: any): any {
    return Gender[value];
  }
}

export class Person {
  @JsonProperty()
  public firstName: string = undefined;

  @JsonProperty({
    serializer: GenderSerialization,
    deserializer: GenderSerialization
  })
  public gender: Gender = undefined;
}

const personObject: Person = new Person();
personObject.firstName = 'John';
personObject.lastName = 'Doe';
personObject.gender = Gender.Male;

export class ApiService {

  public setPerson(): Person {
    return serialize(personObject);
  }
}
```

## License

[MIT](http://vjpr.mit-license.org)

[npm-image]: https://img.shields.io/npm/v/live-xxx.svg
[npm-url]: https://npmjs.org/package/live-xxx
[travis-image]: https://img.shields.io/travis/live-js/live-xxx/master.svg
[travis-url]: https://travis-ci.org/live-js/live-xxx
[coveralls-image]: https://img.shields.io/coveralls/live-js/live-xxx/master.svg
[coveralls-url]: https://coveralls.io/r/live-js/live-xxx?branch=master

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