New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

object-deserializer

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-deserializer

Typesafe deserialization, validation and transformation of POJO to data objects

latest
Source
npmnpm
Version
0.6.0
Version published
Weekly downloads
108
134.78%
Maintainers
1
Weekly downloads
 
Created
Source

Typesafe Object Deserializer

badge

This is a simple and lightweight library for typesafe deserialization, convertion and validation of a POJO (Plain Old Javascript Object) which you usually receive from the server as a JSON response.

If any error occures object deserializer throws a DeserializationError with an additional information about a path to the invalid value in a POJO.

This is a simple usage example:

import * from 'object-deserializer';

type Person = {
  name: string;
  birthday?: Date;
};

const response = `
{
  "person": {
    "name": "John",
    "birthday": "1990-01-01"
  }
}`;

const person = deserialize<Person>(JSON.parse(response), d =>
  d.required('person').asObject(d => ({
    name: d.required('name').asString,
    birthday: d.optional('birthday')?.asDate,
  }))
);

You can create your own value mappers like this:

function personMapper(d: ObjectDeserializer): Person {
  return {
    name: d.required('name').asString,
    birthday: d.optional('birthday')?.asDate,
  };
}

And use it later:

const person = d.required('person').asObject(personMapper);

You can find more advanced examples in the unit tests file.

Keywords

deserialize

FAQs

Package last updated on 29 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts