typify-json

1.1.2 • Public • Published

Typify JSON

NPM version Build status Dependency Status Downloads

The library for serializing any JavaScript or ECMAScript object to a string which saved the types of partial types like Date.

The initial purpose to create this module is that I want a string which is serialized from an object, then will pass the serialized string to mongo shell. The original JSON.stringify doesn't fit my need enough because it will drop the types of Date.

Installation

$ npm install typify-json --save

Usage

const typifyJSON = require('typify-json');
console.log(typifyJSON.stringify({
  foo: 'abc',
  bar: {
    date: new Date()
  }
}));

Then you will get the following result:

{"foo":"abc","bar":{"date":new Date("Fri Dec 21 2012 08:00:00 GMT+0800 (CST)")}}

Work together with mongo shell

const typifyJSON = require('typify-json');
const exec = require('child_process').execSync;
 
// just insert a document simplify
const obj = {
  foo: 'bar'
};
exec(`mongo --eval '${typifyJSON.stringify(obj)}'`);

API

  • TypifyJSON.stringify(obj[, replacer][, space])
    • Number
    • Boolean
    • String
    • Date
    • Object
    • Array
    • Null
    • ObjectId
  • TypifyJSON.parse(str)

License

MIT

Package Sidebar

Install

npm i typify-json

Weekly Downloads

9

Version

1.1.2

License

MIT

Last publish

Collaborators

  • yorkie