question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

missing hasOwnProperty function after Parse

See original GitHub issue

I got big number serializing problem in JSON, so I decide to override JSON.parse and JSON.stringify with this package globally, then I encountered some errors in other js codes,

it seems Object parsed by JSONbig lost hasOwnProperty function:

import * as JSONbig from "json-bigint";

var json = '{ "value" : 9223372036854775807, "v2": 123 }';

console.log(
    JSON.parse(json).hasOwnProperty("value")
);

console.log(
    JSONbig.parse(json).hasOwnProperty("value")
);

module def for typescript:

declare module "json-bigint" {
    function parse(text: string, reviver?: (this: any, key: string, value: any) => any): any;
    function stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string;
    function stringify(value: any, replacer?: (number | string)[] | null, space?: string | number): string;
}

got output:

true
console.log(JSONbig.parse(json).hasOwnProperty("value"));
                                ^

TypeError: JSONbig.parse(...).hasOwnProperty is not a function
    at Object.<anonymous> (\resources\json-bigint\dist\index.js:27:33)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47
error Command failed with exit code 1.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
sidorarescommented, Jul 27, 2020

@goxr3plus this is a recent change, to add better protection from prototype pollution I changed the way objects are created from {} to Object.create(null), unfortunately this has a side effect you posted. Not sure if there is a better way to make it to turn '{"__proto___": { "test": true }}' string into {__proto___: {test: true }} object rather than {} with {test: true} prototype

3reactions
sidorarescommented, Jul 27, 2020

Objects can be created with null prototype, in that case they won’t have .hasOwnProperty method.

Safer way to call:

const example = Object.create(null)
Object.hasOwnProperty.call(example, 'value')
// > false
example.value = 111
Object.hasOwnProperty.call(example, 'value')
// > true

const hasOwnProperty = (object, propertyName) => Object.hasOwnProperty.call(object, propertyName)
hasOwnProperty(example, 'value2')
// > false
hasOwnProperty(example, 'value')
// > true
Read more comments on GitHub >

github_iconTop Results From Across the Web

hasOwnProperty is not a function in Node.js? - Stack Overflow
A way around the error is to call hasOwnProperty on Object explicitly, and bind it to the object, like so: // Calls "hasOwnProperty"...
Read more >
Object.prototype.hasOwnProperty() - JavaScript | MDN
The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to ...
Read more >
CoffeeScript
In JavaScript, the this keyword is dynamically scoped to mean the object that the current function is attached to. If you pass a...
Read more >
Lodash Documentation
identity] (Function): The iteratee invoked per element. Returns. (Array): Returns the new array of filtered values. Example. _.differenceBy ...
Read more >
ECMAScript® 2023 Language Specification - TC39
... 4.4.32 Symbol type; 4.4.33 Symbol object; 4.4.34 function; 4.4.35 built-in function; 4.4.36 property; 4.4.37 method; 4.4.38 built-in method ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found