# serialize-ts

> Serializers for typescript

Latest version **2.6.0** (published 2019-10-19) · ISC license · 0 weekly downloads

## Install

```sh
npm install serialize-ts
pnpm add serialize-ts
yarn add serialize-ts
bun add serialize-ts
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.6.0 |
| Published | 2019-10-19 |
| First published | 2017-07-31 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 343.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 16 |
| Author | xoll |
| Maintainers | xoll |
| Keywords | typescript, serialize, serialization, deserialization, converters, json, http |

## Links

- npm: https://www.npmjs.com/package/serialize-ts
- Repository: https://github.com/AndreyZelenskiy/serialize
- Homepage: https://github.com/AndreyZelenskiy/serialize#readme
- Issues: https://github.com/AndreyZelenskiy/serialize/issues
- npm.io page: https://npm.io/package/serialize-ts

## Dependencies (2)

- [@types/jasmine](https://npm.io/package/@types/jasmine.md) ^3.4.0
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.10

## 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

- 2.6.0 (latest) — 2019-10-19
- 2.5.4 — 2019-02-11
- 2.5.3 — 2019-02-06
- 2.5.2 — 2019-02-06
- 2.5.1 — 2019-01-04
- 2.5.0 — 2018-12-18
- 2.3.3 — 2018-12-05
- 2.3.2 — 2018-10-16
- 2.3.1 — 2018-02-07
- 2.2.1 — 2018-02-02
- 2.3.0 — 2018-01-22
- 2.2.0 — 2018-01-18
- 2.1.0 — 2017-08-01
- 2.0.0 — 2017-08-01
- 1.0.0 — 2017-07-31

## README

# Serialize TS

Metadata library that created to resolve your pain with all mappers and type-checking of objects after serialization or deserialization.

Don`t use the spread operator for copying of your models because prototypes chain will be lost after it!

## Getting started

### Instalation

1. Add a dependency to your package.json file:

`npm install serialize-ts --save` or `yarn add serialize-ts`;

2. Change a tsconfig.json:

```json

{
    ...
    compilerOptions: {
        ...
        emitDecoratorMetadata: true,
        ...
    },
    ...
}

```

### Exmaples

#### Serialization

Simple model:

```typescript
class TestModel {
  @Field()
  @Name("server-id")
  id: number;

  @Field()
  fullName: string;

  ignoredField: any;
}

const model = new TestModel();
model.id = 12;
model.fullName = "Default full name";
model.ignoredField = { customName: "test" };

console.log(serialize(model));

/*Output -> {
    server-id: 12,
    fullName: 'Default full name'
}*/

const obj = {
  "server-id": 12,
  fullName: "Default full name",
  ignoredField: "some ignored value"
};

console.log(deserialize(obj, TestModel));

/*Output -> TestModel {
    id: 12,
    fullName: 'Default full name'
}*/
```

Nested models:

```typescript

@Model()
class NestedModel {
    @Field()
    firstField: number;
    @Field()
    secondField: string;
}

class OuterModel {
    @Field()
    id: number;

    @Field()
    nestedModel: NestedModel;
}

const obj = {
    id: 12,
    nestedModel: {
        firstField: 24,
        secondField: 'Some awesome string!'
    }
};

const outerModel = deserialize(obj, OuterModel);
console.log(outerModel);

/*
    Output -> OuterModel {
        id: 12,
        nestedModel: NestedModel {
            firstField: 24,
            secondField: "Some awesome string!"
        }
    }
*/

console.log(serialize(outerModel));

/*
    Output -> {
        id: 12,
        nestedModel: {
            firstField: 24,
            secondField: "Some awesome string!"
        }
    }
*/

```

Arrays:

```typescript
class Test {
    @Field()
    id: number;

    @Field()
    @Name('numbers')
    @Type(new ArraySerializer(new PrimitiveSerializer()))
    arrayOfNumbers: number[];
}

const obj = {
    id: 12,
    numbers: [12, 24, 36, 48]
};

const deserializedModel = deserialize(obj, Test);
console.log(deserializedModel);
/*
    Output -> Test {
        id: 12,
        arrayOfNumber:  [12, 24, 36, 48]
    }
*/

```

Custom serializers:

```typescript
class CustomSerializer implements Serializer<Object> {
    serialize(model: Object) {
        // Do some custom logic
    }

    deserialize(json: Object) {
        // Do your custom logic and return deserialized object
    }
}

class Model {
    @Field()
    id: 12;

    @Field()
    @Type(new CustomSerializer())
    customField: any;
}

```

If you have some troubles with serialization without serializer decorator definition, you are able to define it with ```@Serializer(YourCustomModel)``` or with some default type like Date or String.
I am waiting for the issues of course!

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