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

json-serialize

v1.1.3

Published

JSON-Serialize.js provides conventions and helpers to manage serialization and deserialization of instances to/from JSON.

Readme

JSON-Serialize.js provides conventions and helpers to manage serialization and deserialization of instances to/from JSON.

#Download Latest (1.1.3):

Please see the release notes for upgrade pointers.

###Module Loading

JSON-Serialize.js is compatible with RequireJS, CommonJS, Brunch and AMD module loading. Module names:

  • 'json-serialize' - json-serialize.js.

Examples

  1. JSON-Serialize natively supports nested Date serialization so this "just works":
var embedded_date_objects = [
  new Date(),
  {to: new Date, from: new Date},
  [1, new Date]
];

var json = JSON.serialize(embedded_date_objects);
var deserialized_embedded_date_objects = JSON.deserialize(json);

equal(_.isEqual(embedded_date_objects, deserialized_embedded_date_objects), true, 'the nested dates were deserialized automatically')

Pretty cool, eh?

  1. Creating custom serialization for one of your classes.
class SomeClass
  constructor: (int_value, string_value, date_value) ->
    this.int_value = int_value;
    this.string_value = string_value;
    this.date_value = date_value;

  toJSON: ->
    return {
      _type:'SomeClass',
      int_value:this.int_value,
      string_value:this.string_value,
      date_value:JSON.serialize(this.date_value)
    }

  @fromJSON: (json) ->    # note: this is a class method
    if (json._type!='SomeClass') return null;
    return new SomeClass(json.int_value, json.string_value, JSON.deserialize(json.date_value));

Now you can automatically serialize and deserialize it:

instance = new SomeClass(1, 'two', new Date());
json = JSON.serialize(instance)   # this calls the toJSON function on the instance

instance2 = JSON.deserialize(json)   # this calls the fromJSON function on the class (you need to make sure the constructor can be found)

Conventions

Uses the following configurable conventions:

  1. use a '_type" field in the json that you serialize
  2. for serializing, implement a toJSON function as an instance method.
  3. for deserializing, implement a fromJSON deserialization factory function as an class method. This doesn't need to be a class function but can be any function as long as it can be found (see JSON.deserialize.NAMESPACE_ROOTS).

Options

  • JSON.deserialize.TYPE_FIELD

You can globally choose the type field used when deserializing an instance from JSON.

For example, if you use couchdb, you could use a 'type' field convention:

JSON.deserialize.TYPE_FIELD = 'type'
  • JSON.deserialize.NAMESPACE_ROOTS

If you don't want to pollute the global namespace with your deserialization factory functions, you can put them in any sort of nested namespaces. Just register your namespace roots like:

JSON.deserialize.NAMESPACE_ROOTS.push(window.my_classes)

Building, Running and Testing the library

###Installing:

  1. install node.js: http://nodejs.org
  2. install node packages: 'npm install'

###Commands:

Look at: https://github.com/kmalakoff/easy-bake