Code Monkey home page Code Monkey logo

ajsonify's Introduction

Node: Asynchronous version of JSON.stringify()

/*
   const { createJSONStream } = require('ajsonify');
   createJSONStream( <Object>, [null, [<spaces>, [[<readable options>], <block-size>]]]] ) : <stream>
*/


const { createJSONStream, copy_object, stringify_g } = require('ajsonify');
const { createWriteStream } = require('fs');

let o = {
   hello: 'world',
   num:  1,
   child : { arr: [1,2,3,4,'string'] },
   array: [null,void 0, void 0,null],
   no : null,
   undefined: void 0,
   rus: 'русские буквы',
   rus2: '\\слеш \\ слеш \\ слеш \\ бэкслеш / бэкслеш /',
   kav1: `'`,
   kav2: `"`,
   special: '\t\n\r',
   date: new Date(),
   string: new String('string'),
   number: new Number(123456),
   boolean: new Boolean(false),
   symbol:  Symbol('symbol'),
   empty_array: [],
   empty_object: {}
}
let o_cp = {...o};
//circular ref. protection test
o.o = o;
o[ o.special ] = 'special';
o.child2 = o.child;
o.empty_4 = [];
o.empty_4.length = 4;


console.dir( o , {depth: null});
console.dir( copy_object(o) , {depth: null});
const assert = require('assert').strict;
assert.ok( [...stringify_g(o_cp)].join('') === JSON.stringify(o_cp),'JSON not equal!' );

createJSONStream( o, null,3).pipe(process.stdout);
//you can change object - createJSONStream make a copy to continue processing async.
Object.keys(o).forEach(k => delete o[k]);

//test huge array
let very_big_array = [];
very_big_array[10000000] = 'last';

let out = createWriteStream( __dirname + '/out.json', 'utf8');
 console.time('stream');
 createJSONStream( very_big_array, null,2).pipe(out).on('close',()=>{
     console.timeEnd('stream');
 });
 very_big_array.length = 0; //it's ok!

setInterval(()=>process.stdout.write('.'),1000).unref();

ajsonify's People

Contributors

vovach777 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

atkins126

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.