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

Objects with no constructor fails to render in tree mode #391

Closed
WKBae opened this issue Jan 18, 2024 · 2 comments
Closed

Objects with no constructor fails to render in tree mode #391

WKBae opened this issue Jan 18, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@WKBae
Copy link

WKBae commented Jan 18, 2024

Code to reproduce

<script>
  import { JSONEditor } from 'svelte-jsoneditor';
</script>

<JSONEditor content={{json: Object.create(null)}} />

Cause

Object.create(null) creates an object with no constructor set.
estimateSerializedSize recurses json and tests if one is object with value.constructor.name === 'Object', throwing TypeError: Cannot read properties of undefined (reading 'name') on such objects.

export function isObject(value: unknown): value is Record<string, unknown> {
// note that we check constructor.name, not constructor === Object,
// so we can use objects created in a different JS realm like an iframe.
return typeof value === 'object' && value !== null && value.constructor.name === 'Object'
}

(Related issue: #321)

Parse function of json-bigint creates object with Object.create(null), causing the error when used as a custom parser of jsoneditor.

@josdejong
Copy link
Owner

Good point! There is a utility function isObject that tries to identify whether something is a plain object (and not a Date or Map or class or something). We'll have to refine that function to cater for this case.

@josdejong josdejong added the bug Something isn't working label Jan 18, 2024
@josdejong
Copy link
Owner

Fixed now in v0.21.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants