josdejong / svelte-jsoneditor

A web-based tool to view, edit, format, repair, query, transform, and validate JSON
https://jsoneditoronline.org
Other
816 stars 108 forks source link

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

Closed WKBae closed 5 months ago

WKBae commented 5 months ago

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. https://github.com/josdejong/svelte-jsoneditor/blob/278dac5b5bd957d14be4ce2821d4c4824227c229/src/lib/utils/typeUtils.ts#L9-L13 (Related issue: https://github.com/josdejong/svelte-jsoneditor/issues/321)

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

josdejong commented 5 months ago

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 commented 5 months ago

Fixed now in v0.21.3