All Projects → AndriiZelenskyi → serialize

AndriiZelenskyi / serialize

Licence: Apache-2.0 license
Serializers for typescript based on decorators

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to serialize

typijs
The Angular CMS Framework for building fully-featured SPA sites powered by NodeJS and MongoDB with TypeScript
Stars: ✭ 141 (+907.14%)
Mutual labels:  decorators, typescript-library
xml-core
xml-core is a set of classes that make it easier to work with XML within the browser and node.
Stars: ✭ 18 (+28.57%)
Mutual labels:  decorators
tiny-typed-emitter
Fully type-checked NodeJS EventEmitter
Stars: ✭ 96 (+585.71%)
Mutual labels:  typescript-library
restgoose
Model-driven REST API framework using decorators
Stars: ✭ 28 (+100%)
Mutual labels:  decorators
decapi
Create GraphQL API by decorating TypeScript classes
Stars: ✭ 81 (+478.57%)
Mutual labels:  decorators
djburger
Framework for safe and maintainable web-projects.
Stars: ✭ 75 (+435.71%)
Mutual labels:  decorators
ifto
A simple debugging module for AWS Lambda (λ) timeout
Stars: ✭ 72 (+414.29%)
Mutual labels:  typescript-library
dataStructure
Implement different Data Structures using TypeScript and JavaScript. Deno Third-party Module.
Stars: ✭ 24 (+71.43%)
Mutual labels:  typescript-library
library-template.ts
Template for authoring TypeScript modules/libraries. Uses Webpack + Karma + Mocha for testing.
Stars: ✭ 35 (+150%)
Mutual labels:  typescript-library
deco
Minimalist Function Decorators for Elixir
Stars: ✭ 21 (+50%)
Mutual labels:  decorators
imtool
🖼️ Client-side canvas-based image manipulation library.
Stars: ✭ 38 (+171.43%)
Mutual labels:  typescript-library
necktie
Necktie – a simple DOM binding tool
Stars: ✭ 43 (+207.14%)
Mutual labels:  typescript-library
final-form-calculate
Decorator for calculating field values based on other field values in 🏁 Final Form
Stars: ✭ 111 (+692.86%)
Mutual labels:  decorators
fnts
λ Minimal Functional Programming Utilities for TypeScript & JavaScript
Stars: ✭ 75 (+435.71%)
Mutual labels:  typescript-library
ts-mock-imports
Intuitive mocking library for Typescript class imports
Stars: ✭ 103 (+635.71%)
Mutual labels:  typescript-library
microbundle-ts-pkg
A TypeScript npm package skeleton/starter project with microbundle, node:test and prettier
Stars: ✭ 20 (+42.86%)
Mutual labels:  typescript-library
alls
Just another library with the sole purpose of waiting till all promises to complete. Nothing more, Nothing less.
Stars: ✭ 13 (-7.14%)
Mutual labels:  typescript-library
typescript-strict-plugin
Typescript plugin that allows turning on strict mode in specific files or directories.
Stars: ✭ 166 (+1085.71%)
Mutual labels:  typescript-library
tsed
📐 Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone.
Stars: ✭ 2,350 (+16685.71%)
Mutual labels:  decorators
twc
TypeScript based, boilerplate-less, Polymer toolbox friendly Polymer Modules
Stars: ✭ 33 (+135.71%)
Mutual labels:  decorators

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;

  1. Change a tsconfig.json:
{
    ...
    compilerOptions: {
        ...
        emitDecoratorMetadata: true,
        ...
    },
    ...
}

Exmaples

Serialization

Simple model:

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:

@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:

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:

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!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].