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

Add objectBaseClass option which can be set to Object instead of null to keep prototype methods like hasOwnProperty #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

klesun
Copy link

@klesun klesun commented Feb 3, 2021

As discussed in #38, after the c85a430 (v1.0.0) base class for parsed objects is now null rather than Object. This differs from the JSON.parse() behaviour and breaks compatibility for codebases that are using prototype methods like hasOwnProperty().

By specifying the objectBaseClass option introduced in this PR, users of the lib will be able to use it even in a code that relies on parsed object to have hasOwnProperty, toString and other prototype methods:

var JSONbig = require('json-bigint');
var JSONBigInt = JSONbig({ objectBaseClass: Object }); // default is null
var json = '{ "value" : 9223372036854775807, "v2": 123 }';
// does not result in "hasOwnProperty is not a function" TypeError thanks to `objectBaseClass: Object`
console.log(JSONBigInt.parse(json).hasOwnProperty("value"));

Closes #39, #38

…`null` to preserve prototype methods like `hasOwnProperty`

sidorares#38
@klesun klesun changed the title Add objectBaseClass option which can be set to Object instead of null to preserve prototype methods like hasOwnProperty Add objectBaseClass option which can be set to Object instead of null to keep prototype methods like hasOwnProperty Feb 3, 2021
corrigancd added a commit to sirensolutions/json-bigint that referenced this pull request Jul 14, 2021
@egargan
Copy link

egargan commented Oct 27, 2021

Anything stopping this being merged??

@egargan
Copy link

egargan commented Oct 27, 2021

Anything stopping this being merged??

Ah because it doesn't actually do anything! The 'objectBaseClass' option isn't actually applied to the _options variable that parse reads from.

I've just opened #64 with a similar fix. It won't let you provide an arbitrary prototype for the parsed objects, but it will let you choose between Object.create(null) and {}.

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

Successfully merging this pull request may close these issues.

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