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

pinkie-json-serializer

v1.0.2

Published

Lightweight library for serializing JSON objects.

Downloads

17

Readme

Pinkie-Serializer

a 100% official logo

Introduction

A simple, lightweight JSON serializer. I was inspired by active-model-serializer for Ruby, but this one is not binded to one specific technology(ActiveModel ORM for example) and can be easily implemented in any situation.

Installation

Pinkie is available on npm.

npm install --save pinkie-json-serializer

Usage

Let's say we fetch data from our database in this form:

var user = {
    "id" : 3,
    "username" : "Mrs.Sparkle",
    "password" : "un4re8da0bl34ash",
    "gender" : "F",
    "last_login" : 14712041
}

It would be nice to have one place in our project, where we could properly format our output. So we hook up this serializer in our data access layer. It may look as follows:

    var Serializer = require('pinkie-serializer');
    var serializer = new Serializer();
    
    function isOwner(id){
        return (id === 4);
    }
    
    var serialization_schema = {
        "name" : {"as" : "username"},
        "gender" : true,
        "password" : {
            "show" : function(json){
                return isOwner(json["id"]);
            }
        },
        "last_login" : {
            "set" : function(json){
                return {
                    "utc" : json["last_login"], //our fetched time
                    "halved" : json["last_login"]/2 
                }
            };
        }
    };
    var user = fetch_by_id(3); //pseudo-function
    var serialized = serializer.serialize(user, schema);
    console.log(serialized);
    
    //Outputs
    {
        "username" : "Mrs.Sparkle",
        "gender" : "F",
        "last_login" : {
            "utc" : 14712041,
            "halved" : 7356020.5
        }
    }

Schema allows nested arrays and objects, but in this case the "show" value must contain next serialization schema.

Integrating with other software

Moongose

I used it as a Moongose plugin when I was building my API. I implemented it as Moongose middleware, but it has some downsides like inability to access request object and parse headers for example(in this case you may want to create serialize method on your model with request arg).

    //src/models/plugins/serializer.js
    var Serializer = require('pinkie-serializer');
    var serializer = new Serializer();
    
    module.exports = function(schema, serialization_schema){
    	schema.methods.toJSON = function(){
    		return serializer.serialize(this.toObject(), serialization_schema, this);
    	};
    };
    //src/models/user.js
    var mongoose = require('mongoose');
    var serializer = require('./plugins/serializer');
 
 
var userSchema = new mongoose.Schema({
	name: {
		type: String
	},
	gender:{
	    type: String,
	    enum: ["M", "F"]
	},
	password:{
	    type: String
	}
	last_login:{
	    type: Number
	}
});

userSchema.plugin(serializer, {
	"_id" : {"as" : "id"},
	"name" : {"as" : "username"},
    "gender" : {
        "set" : function(json, user){ 
        //user is moongose object we passed in serializer.js as a 3rd param. we can use it to access related objects if we need any
            
            if(json["gender"] === "F"){
                return "girl";
            }else{
                return "boy";
            }
        }
    }
});

module.exports = mongoose.model('User', userSchema);

Now if you query your DB via you'll be responded with nicely formatted output.