sidorares / json-bigint

JSON.parse/stringify with bigints support
MIT License
808 stars 192 forks source link

Object parse using json-bigint doesn't have Object function #39

Open iravishah opened 4 years ago

iravishah commented 4 years ago

`var JSONbigString = require('json-bigint');

var key = '{ "key": 1234567890123456789 }'; var withString = JSONbigString.parse(key);

console.log(Function.prototype.toString.call(withString.constructor));`

Result `console.log(Function.prototype.toString.call(withString.constructor)); ^

TypeError: Function.prototype.toString requires that 'this' be a Function at toString () at Object. (Ravi/office/mix/check.js:6:41) at Module._compile (internal/modules/cjs/loader.js:776:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Function.Module.runMain (internal/modules/cjs/loader.js:829:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)`

Expected

function Object() { [native code] }

When you use normal JSON.parse you will get the constructor as expected.

gitpaladin commented 4 years ago

use the last version instead

npm install --save json-bigint@0.4.0

After update to 1.0.0, lots of weird problem occurred. I've never mind some library do some prototype pollution. But there is no options to keep the compatibility to the older version.

iravishah commented 4 years ago

Thanks!

klesun commented 3 years ago

I filed a PR to add an option to preserve prototype properties on the parsed objects: https://github.com/sidorares/json-bigint/pull/47

var JSONbig = require('json-bigint');
var JSONBigInt = JSONbig({ objectBaseClass: Object }); // default is null
var key = '{ "key": 1234567890123456789 }';
// should not result in TypeError thanks to `objectBaseClass: Object`
console.log(Function.prototype.toString.call(withString.constructor));`

Upd.: oh, I thought built-in BigInt support was not available in v0.4.0, but apparently it is, so, yeah, better to just stick to v0.4.0

chbdetta commented 2 years ago

Please merge https://github.com/sidorares/json-bigint/pull/47 as the object prototype also provide toString() and valueOf() which implicit conversion from objects to string / number relies on.

haoadoreorange commented 2 years ago

I filed a PR to add an option to preserve prototype properties on the parsed objects: #47

var JSONbig = require('json-bigint');
var JSONBigInt = JSONbig({ objectBaseClass: Object }); // default is null
var key = '{ "key": 1234567890123456789 }';
// should not result in TypeError thanks to `objectBaseClass: Object`
console.log(Function.prototype.toString.call(withString.constructor));`

Upd.: oh, I thought built-in BigInt support was not available in v0.4.0, but apparently it is, so, yeah, better to just stick to v0.4.0

I'm curious, why do we want to have no prototype on parsed object ?

On a side note, since this package seems not being maintained, I forked it and rewrite in TS while fixing couple of issues, including this one. My approach is to call setPrototype on the parsed object later on, is there something wrong with that ? Am I missing something ?

Btw my forked is here https://github.com/haoadoresorange/when-json-met-bigint, feel free to give it a try and let me know if something's wrong xD