josdejong / svelte-jsoneditor

A web-based tool to view, edit, format, repair, query, transform, and validate JSON

Home Page:https://jsoneditoronline.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Objects with no `constructor` fails to render in tree mode

WKBae opened this issue · comments

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.

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.

Fixed now in v0.21.3