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 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsonj

v0.1.4

Published

Serialize and deserialize Javascript objects into JSON, including metadata to restore objects to their original prototype

Downloads

6

Readme

jsonj (JSON serializer / deserializer)

Serialize and deserialize Javascript objects into JSON adding metadata if necessary for type information so objects are deserialized with the corresponding prototype. Typescript support.

Note: preliminary documentation

It will serialize and deserialize these values (which JSON.stringify and JSON.parse will not):

  • NaN
  • Infinity
  • Date
  • Buffer
  • RegExp
  • all builtin error constructs (Error, SyntaxError, TypeError, ReferenceError, RangeError, EvalError, URIError)

If any core javascript objects are missing, please submit an issue or pull request

Installation

npm install jsonj

Dependencies

none

API

jsonj.register(name, constructor, [ optionalFunctions ])

registers a constructor to serialize and deserialize

optionalFunctions

An object with either or both the below properties:

  • serialize - Function to serialize an object. May return any value.
  • deserialize - Function to reconstruct an object. Must return a constructed object

jsonj.serialize(value, [type])

Serializes value to a JSON representation. If the type is provided, no metadata will be included since it is assumed that the deserializing function knows what is the object is receiving.

jsonj.deserialize(json, [type])

Deserializes json to the provided type. If no type is provided, it will be obtained from metadata within the json object, if present.


var jsonj = require('jsonj');

function Person(name, age) {
    this.name = name;
    this.age = age;
}

Person.prototype.greet = function () {
    return 'hello, my name is ' + this.name + ' and I am ' + this.age + ' years old';
};

jsonj.register('Person', Person);

var me = new Person('Jayce', 22);

var json = jsonj.serialize(me); //no type information is given, so it is added as metadata
// {"__type":"Person","__data":{"name":"Jayce","age":22}}

var json2 = jsonj.serialize(me, Person); // resulting json can be simplified since we specify the type
// {"name":"Jayce","age":22}

var newMe = jsonj.deserialize(json);

newMe.greet();
// 'hello, my name is Jayce and I am 22 years old'

var newMe2 = jsonj.deserialize(json2,Person); // "cast" as Person when deserializing.
newMe2.greet();
// 'hello, my name is Jayce and I am 22 years old'

Custom serializers/deserializers

You can add custom serializers/derserializers by adding a serialize or deserialize function to your class. In JavaScript, as a function attached to the constructor. In TypeScript, these would be public static functions. You can also add serializers/deserializers by passing the functions to the register function.

serialize

If a serialize function was passed in the register call as an option, it is used. Otherwise, if the constructor has a serialize function, it is used to serialize the object instead. If neither are specified, then the object is serialized as key/value by default.

deserialize

If a deserialize function was passed in the registercall as an option, it is used. Otherwise, jsonj will try to call a deserialize static method of the class. If neither are specified, jsonj will copy the json data memberwise.

jsonj.unregister(name)

unregister a constructor from jsonj

serializeMetadata

You can add a dictionary with type metadata to a constructor to specify a white list of properties that you want serialized, along with type information that will allow for serializing without embedded metadata.


function Person(name, age) {
    this.name = name;
    this.age = age;
    this.birthDate = null;
    this.schools = [];
}

Person.prototype.sayHello = function () {

    console.log("Hello, I am " + this.name);

}

function School(name) {
    this.name = name;
}

Person.serializeMetadata = {
    name: "String",
    birthDate: "Date",
    schools: "School[]",
}

Credits

Some ideas, the builtins and some example text taken from jaycetde's jsoni parser, https://github.com/jaycetde/jsoni, though jsonj serializes to JSON rather than parsing JSON.