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

typescript-json-object-mapper

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typescript-json-object-mapper

Json Object mapper writing in TypeScript

latest
Source
npmnpm
Version
1.4.5
Version published
Weekly downloads
86
14.67%
Maintainers
1
Weekly downloads
 
Created
Source

(TypeScript) Json-Object-Mapper

Donate

This a simple package to mapping a json object.

Getting Started

Install

npm install typescript-json-object-mapper
yarn add typescript-json-object-mapper

Configure

To work with decorators, you need first enable emitDecoratorMetadata y experimentalDecorators on you tsconfig.json. Example:

{
    "compilerOptions": {
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true
    }
}

Create you own Views

This example tries to show all possible cases in which you might need to use this utility.

class UserView extends JsonView {
    @JsonProperty
    username: string;
    
    @JsonProperty({
        ignore: true
    })
    password: string;
    
    @JsonProperty({
        topic: 'custom'
    })
    birthday: string;
    @JsonProperty({
        topic: 'custom2'
    })
    phone: string;
}

Define you data object

const json = {
    username: "annon",
    password: "12345678",
    birthday: "1992-03-20",
    phone: "+0123456789"
};

Serilize(without topic's)

const serialized = JsonObjectMapper.serialize(json, UserView).toString();

results:

{
    username: "annon",
    birthday: "1992-03-20",
    phone: "+0123456789"
}

Serilize(with topic)

const serialized = JsonObjectMapper.serialize(json, UserView, ['custom']).toString();

results:

{
    username: "annon",
    birthday: "1992-03-20"
}

Serilize(with topic)

const serialized = JsonObjectMapper.serialize(json, UserView, ['custom', 'custom2']).toString();

results:

{
    username: "annon",
    birthday: "1992-03-20",
    phone: "+0123456789"
}

Features

  • No-Initiation(Using only reference to class)
  • Renaming properties
  • Change data types
    • to Date
      • from String using Date.parse
      • from Integer using Date
    • to Integer
      • from String using Number
    • to Float
      • from String using Number
    • to Boolean
    • to String
    • to Object
  • Sub-Views(Recursivity)
    • Array sub-views
    • Single sub-view
  • Date values(String, Number, Date)
  • Serialize from Object Array
  • Serialize from Object
  • Serialize from String
  • Property Topic's
  • Smart Object, Constructor, Function, Date, ObjectId to String
    • Using toString method when it exists
      • If toString return [object object], JsonObjectMapper will try iterate object to create a plain object with defualts values.

API:

JsonObjectMapper.serialize

This function always return Serialization object. And can using it with data as Array or Object.

Example
// from Array
JsonObjectMapper.serialize([
    {
        email: "john.smith@example.com",
        password: "123456"
    },
    {
        email: "john.smith@example.com",
        password: "123456"
    },
    {
        email: "john.smith@example.com",
        password: "123456"
    }
], UserView);

// from Object
JsonObjectMapper.serialize({
   email: "john.smith@example.com",
   password: "123456"
}, UserView);

Serialization

Serialization.toString(spaces:number = 4): string

This method return a string representation of object.

Serialization.toJson()

This method return a json object representation.

Keywords

typescript

FAQs

Package last updated on 17 May 2019

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