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 🙏

© 2026 – Pkg Stats / Ryan Hefner

typed-serializer

v2.1.19

Published

Typed serializer

Readme

Typed-Serializer

Typed-serializer like JSON.stringify() and JSON.parse() for typed objects (class instance).
Typed-serializer Parser.parse() method return typed objects, which class definition in modules.

Use browserify framework for browsers.
Install:

npm i typed-serializer

Table of Contents

  1. Features
  2. Example
  3. Service Attributes
  4. References
  5. Use global

Features

  • save referenced objects types;
  • resolve circular reference;
  • resolve reference to the same objects.

Example

const Serializer = require('typed-serializer').Serializer;
const Stringifier = require('typed-serializer').Stringifier;
const Parser = require('typed-serializer').Parser;

const includedClasses = require('./includedClasses');
const includedClasses2 = require('./includedClasses2');
const excludedClasses = require('./excludedClasses');
const { CustomContent } = require('../classes/includedClasses');

//array of modules which classes including to stringify and parse process
var modules = [includedClasses, includedClasses2];

//object to stringify
var customContent = new CustomContent()

//stringify object to json
var stringifier = new Stringifier(modules);
var json = stringifier.stringify(customContent, true);

//parse object from json
var parser = new Parser(modules);
resultCustomContent = parser.parse(json);

//use referenced objects Set
var arr = Array.from(parser.referencedObjectsSet);
assert.ok(arr[0] instanceof Unit);
assert.ok(arr[1] instanceof Unit);
assert.ok(arr[2] instanceof CustomUnit);
assert.ok(arr[3] instanceof CustomContent);
assert.ok(arr.length === 4);

For example we define classes in three modules:

  • includedclasses - classes including to stringify and parse process;
  • includedclasses2 - classes including to stringify and parse process;
  • excludedclasses - classes excluded from stringify and parse process.

Module: includedClasses

import { Layout, Hint } from "./excludedClasses";
import { CustomUnit, Unit } from "./includedClasses2";

export class Content {
    contentName;
    constructor() {
        this.contentName = 'Content';
    }
}

export class CustomContent extends Content {
    customContentName;
    units: Array<Unit> = new Array<Unit>();
    layouts: Array<Layout> = new Array<Layout>();
    hint: Hint;
    customUnit: CustomUnit;
    constructor() {
        super();
        this.customUnit = new CustomUnit(this);
        this.customContentName = 'CustomContent';

        this.units.push(new Unit());
        var unit = new Unit();
        this.units.push(unit);
        this.units.push(unit);

        this.layouts.push(new Layout());
        this.layouts.push(new Layout());
        this.layouts.push(new Layout());

        this.hint = new Hint();
    }
}

Module: includedClasses2

import { Content, CustomContent } from "./includedClasses";

export class Unit {
    unitName;
    constructor() {
        this.unitName = "Unit";
    }
}

export class CustomUnit extends Unit {
    customUnitName: string;
    content: Content;
    constructor(content: Content) {
        super();
        this.customUnitName = "CustomUnitUnit";
        this.content = content;
    }
}

Module: excludedClasses

export class Layout {
    layoutName;
    hint;
    constructor() {
        this.layoutName = "Layout";
        this.hint = new Hint();
    }
}

export class Hint {
    hintName;
    constructor() {
        this.hintName = 'Hint';
    }
}

Result: resultCustomContent (see "Example")

Result of parsing json to typed object: Result:resultCustomContent Fig. 1. Result object "resultCustomContent"

Object resultCustomContent has type "CustomContent" from module "includedClasses". Reference object "customUnit" has type "CustomUnit" from module "includedClasses2". References objects "units" have type "Unit" from module "includedClasses2".

  1. "customUnit" has circular reference "content" to parent object. Return value "resultCustomContent".
  2. "hint" and "layouts" are referencing to objects which classes defined in module "excludedClasses". This module missing in array of modules including in stringify and parse process. Return value null.
  3. "units[1]" and "units[2]" are referencing to the same object.

Result: referencedObjectsSet (see "Example")

All objects which type parsed added to Set "referencedObjectsSet". Result:referensedObjectsSet Fig. 2. Result object "resultCustomContent" - objects added to "referencedObjectsSet"

Compare figure 2 objects types and example code (units[1] and units[2] - the same objects):

...
var arr = Array.from(parser.referencedObjectsSet);
assert.ok(arr[0] instanceof Unit);
assert.ok(arr[1] instanceof Unit);
assert.ok(arr[2] instanceof CustomUnit);
assert.ok(arr[3] instanceof CustomContent);
assert.ok(arr.length === 4);

Service attributes

All referenced objects after parsing have service attributes (see figure 3):

  • typedSerializer_id,
  • typedSerializer_nativeClassName. To remove this attrs from parsed objects run parser.removeTheExcess() after parsing json

Service attributes Fig. 3. Referenced object - service attributes

References

References Fig. 4. Serializer class diagramm

Serializer

constructor()

@param context - module or module array to finds object types
constructor(context: any)

getGlobal()

Static method. Return global context.

@param namespace - namespace in global context (optional)
static getGlobal(namespace: string): any

setGlobal()

Static method. Set modules exports into global context.

@param context - module or module array
@param namespace - namespace in global context (optional)
static setGlobal(context: any, namespace: string): void

clearGlobal()

Static method. Remove from global context all modules exports.

@param context - module or module array
@param namespace - namespace in global context (optional)
static clearGlobal(context: any, namespace: string): void

Stringifier

constructor()

@param context - module or module array to finds object types
constructor(context: any)

stringify()

Stringify obj like JSON.stringify().
Saves objects types in property 'nativeClassName'. Objects types looks in modules or global context.
If ignoreNotFound == true and object types not found in modules or global context - return undefined.

@param obj - object to stringify
@param ignoreNotFound - if true - ignore object
stringify(obj: any, ignoreNotFound: boolean = false): string

Parser

referencedObjectsSet

Set of referenced objects. Your can call Parser.parse() several times. That adds referenced objects in Set. After parsing your can call some method for each object in set.

constructor()

@param context - module or module array to finds object types
constructor(context: any)

parse()

Parse json string like JSON.parse(). Include objects types. Objects types looks in modules or global context.

@param json - json string
parse(json: string): Object

removeTheExcess()

Removes attrs "typedSerializer_id" and "typedSerializer_nativeClassName" from parsed objects.

removeTheExcess(): void

Using global context example

Using global

...

//array of modules which classes including to stringify and parse process
var modules = [includedClasses, includedClasses2];

//Set modules exports into global context
Serializer.setGlobal(modules);

//object to stringify
var customContent = new CustomContent()

//stringify object to json
var stringifier = new Stringifier();
var json = stringifier.stringify(customContent, true);

//parse object from json
var parser = new Parser();
resultCustomContent = parser.parse(json);

//Remove modules exports from global context
Serializer.clearGlobal(modules);

Using global with namespace

...

//array of modules which classes including to stringify and parse process
var modules = [includedClasses, includedClasses2];

//Set modules exports into global context
Serializer.setGlobal(modules, 'testParseNameSpace');
var globalNameSpace = Serializer.getGlobal('testParseNameSpace');

//object to stringify
var customContent = new CustomContent()

//stringify object to json
var stringifier = new Stringifier(globalNameSpace);
var json = stringifier.stringify(customContent, true);

//parse object from json
var parser = new Parser(globalNameSpace);
resultCustomContent = parser.parse(json);

//Remove modules exports from global context
Serializer.clearGlobal(modules, 'testParseNameSpace');