Latest Threat ResearchGlassWorm Loader Hits Open VSX via Developer Account Compromise.Details
Socket
Book a DemoInstallSign in
Socket

jsoni

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsoni

Serialize and deserialize Javascript objects into JSON

Source
npmnpm
Version
0.0.0
Version published
Weekly downloads
1
-66.67%
Maintainers
1
Weekly downloads
 
Created
Source

jsoni

My attempt at javascript object serialization and deserialization

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

  • NaN
  • Infinity
  • new Date()
  • new Buffer()
  • new 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

undefined values will not stringify or parse

API

jsoni.stringify(value, [ replacer ], [ space ])

same as JSON.stringify

jsoni.parse(value, [ reviver ])

same as JSON.parse

jsoni.register(name, constructor, [ options ])

registers a constructor to serialize and deserialize

options

  • serialize - Function to serialize an object. May return any value that can be stringified.
  • deserialize - Function to reconstruct an object. Should return a constructed object

var jsoni = require('jsoni');

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';
};

jsoni.register('Person', Person, {
    serialize: function (person) {
        return {
            name: person.name,
            age: person.age
        };
    },
    deserialize: function (obj) {
        return new Person(obj.name, obj.age);
    }
});

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

var str = jsoni.stringify(me);
// '{"constructName":"Person","data":{"name":"Jayce","age":22}}'

var newMe = jsoni.parse(str);

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

toJSON

If a constructor's prototype has a toJSON method, then serialize does not need to be specified. serialize will take precedence over toJSON if it is specified. If neither are specified, then the default behavior of JSON.stringify is used.

fromJSON

If a constructor has a fromJSON method (ex: Person.fromJSON, not Person.prototype.fromJSON), then deserialize does not need to be specified. deserialize will take precedence over fromJSON if it is specified. If neither are specified, then the json data will be passed directly into the constructor (new Constructor(data)).

jsoni.unregister(name)

unregister a constructor from jsoni

FAQs

Package last updated on 23 Jan 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts