Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
iravishah opened this issue Aug 20, 2020 · 5 comments · May be fixed by #47 or #64
Open

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

iravishah opened this issue Aug 20, 2020 · 5 comments · May be fixed by #47 or #64

Comments

@iravishah
Copy link

iravishah commented Aug 20, 2020

`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
Copy link

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.

@iravishah
Copy link
Author

Thanks!

@klesun
Copy link

klesun commented Feb 3, 2021

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

@chbdetta
Copy link

Please merge #47 as the object prototype also provide toString() and valueOf() which implicit conversion from objects to string / number relies on.

@haoadoreorange
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants