json-bigint icon indicating copy to clipboard operation
json-bigint copied to clipboard

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

Open iravishah opened this issue 5 years ago • 5 comments

`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.

iravishah avatar Aug 20 '20 18:08 iravishah

use the last version instead

npm install --save [email protected]

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.

gitpaladin avatar Sep 15 '20 07:09 gitpaladin

Thanks!

iravishah avatar Sep 15 '20 10:09 iravishah

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

klesun avatar Feb 03 '21 09:02 klesun

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.

chbdetta avatar Dec 15 '21 02:12 chbdetta

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

haoadoreorange avatar Dec 16 '21 00:12 haoadoreorange