npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typescript-deserializer

v1.0.2

Published

A typescript powered deserializer library

Downloads

86

Readme

Typescript Deserializer

npm David npm bundle size (minified)

A typescript based JSON deserializer that permits converting raw Javascript/JSON Objects into typescript classes. Using decorators it becomes simple to define which class members should be deserialized and optionaly mapped into different values.

Goals

  • Class deserialization from raw Javascript/JSO Objects
  • Class member deserialization using decorator for greater flexibility
  • Allow key transformation to change those snake_cases into camelCases
  • Allow value transformation for automatic convertions and calculations

Installation

Via yarn

yarn add typescript-deserializer

Via npm

npm install typescript-deserializer

Usage

Import

import { JsonProperty, Deserializer } from 'typescript-deserializer';

Simple usage

import { JsonProperty, Deserializer } from 'typescript-deserializer';

class ExampleClass {
    @JsonProperty()
    public prop1: string;
}

const instance = Deserializer.deserialize<ExampleClass>(ExampleClass, { prop1: 'Example Prop 1' });

instance.prop1;
// → "Example Prop 1"

Key transformation

import { JsonProperty, Deserializer } from 'typescript-deserializer';

class ExampleClass {
    @JsonProperty({ jsonKey: 'ugly_prop' })
    public prop2: string;
}

const instance = Deserializer.deserialize<ExampleClass>(ExampleClass, { ugly_prop: 'Example Prop 2' });

instance.prop2;
// → "Example Prop 2"

Value transformation

import { JsonProperty, Deserializer } from 'typescript-deserializer';

class ExampleClass {
    @JsonProperty({
        map: (value: string) => parseInt(value, 10),
    })
    public prop3: number;
}

const instance = Deserializer.deserialize<ExampleClass>(ExampleClass, { prop3: '33' });

instance.prop3;
// → 33

typeof instance.prop3;
// → "number"

Ignoring properties without decorators

import { JsonProperty, Deserializer } from 'typescript-deserializer';

class ExampleClass {
    public prop4: string;
}

const instance = Deserializer.deserialize<ExampleClass>(ExampleClass, { prop4: 'Example Prop 4' });

instance.prop4;
// → undefined

Preserving default values

import { JsonProperty, Deserializer } from 'typescript-deserializer';

class ExampleClass {
    @JsonProperty()
    public prop5: string = 'Awesome Default Value';
}

const instance = Deserializer.deserialize<ExampleClass>(ExampleClass, { otherProp: 'Other Example Prop' });

instance.prop5;
// → "Awesome Default Value"

Deserialize function has static method

import { JsonProperty, Deserializer } from 'typescript-deserializer';

class ExampleClass {
    @JsonProperty()
    public prop6: string;

    static deserialize (jsonObject: Object): ExampleClass {
        return Deserializer.deserialize<ExampleClass>(ExampleClass, jsonObject);
    }
}

const instance = ExampleClass.deserialize({ prop6: 'Example Prop 6' });
instance.prop6;

// → "Example Prop 6"

Deserialize function has static method

import { JsonProperty, Deserializer } from 'typescript-deserializer';

class ExampleClass {
    @JsonProperty()
    public prop6: string;

    static deserialize (jsonObject: Object): ExampleClass {
        return Deserializer.deserialize<ExampleClass>(ExampleClass, jsonObject);
    }
}

const instance = ExampleClass.deserialize({ prop6: 'Example Prop 6' });
instance.prop6;

// → "Example Prop 6"

Built in mapping functions

import { JsonProperty, Deserializer } from 'typescript-deserializer';

class ExampleClass {
    @JsonProperty({
        map: Deserializer.toDate,
    })
    public prop7: Date;

    @JsonProperty({
        map: Deserializer.parseInt,
    })
    public prop8: number;

    @JsonProperty({
        map: Deserializer.fromArray(OtherClass.deserialize),
    })
    public prop9: Array<OtherClass>;

    static deserialize (jsonObject: Object): ExampleClass {
        return Deserializer.deserialize<ExampleClass>(ExampleClass, jsonObject);
    }
}

const instance = ExampleClass.deserialize({
    prop7: '1995-12-17T03:24:00',
    prop8: '123',
    prop9: [
        { (...) },
        { (...) },
        { (...) },
    ],
});

instance.prop7;
// → Sun Dec 17 1995 03:24:00 GMT...

typeof instance.prop7;
// → "Object" ~ Date

instance.prop8;
// → 123

typeof instance.prop8;
// → "number"

instance.prop9;
// → [ <OtherClass>, <OtherClass>, <OtherClass> ]

Building/Testing

  • Runs linter and tests with:
yarn test
  • Builds library and types:
yarn build

TODO

  • [ ] Remove @loopback/metadata dependency