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 🙏

© 2025 – Pkg Stats / Ryan Hefner

deneric2

v1.4.5

Published

JSON parser

Readme

deneric2

Deneric2 help you parsing data from Json Object into your Entity. Save your time & safety when working with json.

Getting start

Install

npm

npm install deneric2

Yarn

yarn add deneric2

Using

  • Define your schema & class
  • using fromJson method to parsing Json object to your entity
  • using toJson method to transform your entity to Json Object

Deneric

Deneric is an abstract class. Your class must be extended from Deneric and its constructor have to set your schema in it. | Methods | Detail | | -------------- | ------------------------ | | clone() | Clone instance | | fromJson(json) | parse json to Entity | | toJson() | transform entity to Json |

Example

import Deneric, { DenericSchema } from 'deneric2'

const SCHEMA: DenericSchema = {} // Your schema

class MyClass extends Deneric { // Your class
    constructor(){
        super(SCHEMA)
    }
}

DenericSchema

DenericSchema is a object<key, value>. key is String and value is a Tuple/Array with rule: | DenericSchema | Detail | | ------------------- | ------------------------------------------------------------------------- | | DenericSchema.key | key as your class proprety | | DenericSchema.value | [dataPath: string, dataType: DenericDataType, jsonIgnore?: boolean] |

DenericDataType

| DataType | Description | Example | | ------------------- | ------------------------ | -------------------------------------------- | | String | string | ['data_path', String] | | Number | number | ['data_path', Number] | | Boolean | boolean | ['data_path', Boolean] | | Array | Array of any thing | ['data_path', Array] | | Object | Object | ['data_path', Object] | | Deneric.Array | Array of DenericDataType | ['data_path', Deneric.Array(Number)] | | Deneric.Map | Map of DenericDataType | ['data_path', Deneric.Map(MyClass)] | | Instance of Deneric | Your class | ['data_path', My Class] |

Notes:

  • Deneric.Array: Using to parse your search response
  • Deneric.Map: Using to parse your mget response

Example

{
    fullName: ['profile.full_name', String],
    age: ['profile.age', Number],
    isMale: ['profile.is_male', Boolean],
    github: ['social.github', String, true] // is mean this property will be ignore when you call toJson method
}

Deneric

Using Example:

You have JSON Object like this

const json = {
  profile: {
    full_name: 'John Smith',
    age: 12
  },
  others: {
    is_male: true,
    roles: ['1', '2', '2a', '2b'],
    school_name: 'ABC School'
  },
  jobs: {
    2021: ['A', 'B', 'C'],
    2025: ['B', 'D']
  }
}

Define Your Class & schema

import Deneric from 'deneric2'

class Student extends Deneric {
  fullName: string = 'noname' // default value of this property
  age: number = -1
  isMale: boolean = false
  roles: string[] = ['ABC', 'DEF']
  jobs: { [key: string]: string[] } = { 2021: ['Covid'] }

  constructor() {
    super({
      fullName: ['profile.full_name', String],
      age: ['profile.age', Number],
      isMale: ['others.is_male', Boolean],
      roles: ['others.roles', Deneric.Array(String)],
      jobs: ['jobs', Object],
    })
  }
}

const student1 = new Student(json)
student1.fromJson(json)

So you have variable student1 instance of Class Student.

// student1 
{
    fullName: 'John Smith',
    age: 12,
    isMale: true,
    roles: ['1', '2', '2a', '2b'],
    jobs: {
        2021: ['A', 'B', 'C'],
        2025: ['B', 'D']
    }
}

And have function to get json with schema from student1 (call: student1.toJson()):

// student1.toJson()
{
    profile: {
        full_name: 'John Smith',
        age: 12
    },
    others: {
        is_male: true,
        roles: ['1', '2', '2a', '2b']
    },
    jobs: {
        2021: ['A', 'B', 'C'],
        2025: ['B', 'D']
    }
}

Mores example:

class ClassRoom extends Deneric {
    monitor!: Student
    students!: Student[]
    mapStudents!: Record<string, Student>

    constructor() {
      super({
          monitor: ['class_monitor', Student], // property as an Deneric Entity
          students: ['my_student', Deneric.Array(Student)], // property as an Array of Deneric Entity
          mapStudents: ['map_student', Deneric.Map(Student)] // property as an Map with value is Deneric Entity
      })
    }
}

Json Ignore Example

You can define schema to ignore property when call method toJson

import Deneric from 'deneric2'

class StudentIgnoreJob extends Deneric {
  fullName: string = 'noname'
  jobs: { [key: string]: string[] } = {}

  constructor() {
    super({
      fullName: ['profile.full_name', String],
      jobs: ['jobs', Deneric.Map(Deneric.Array(String)), true] // json ignore. This schema will be ignore when call toJson
    })
  }
}

const temp = new StudentIgnoreJob(json)
temp.fromJson(json)

So you have variable temp instance of Class StudentIgnoreJob.

// console.log(temp)
{
    fullName: 'John Smith',
    age: 12,
    isMale: true,
    roles: ['1', '2', '2a', '2b'],
    jobs: {
        2021: ['A', 'B', 'C'],
        2025: ['B', 'D']
    }
}

When call toJson, property jobs will be ignored.

// console.log(temp.toJson())
{
    fullName: 'John Smith',
    age: 12,
    isMale: true,
    roles: ['1', '2', '2a', '2b']
}