Svelte: Feature request: Call JSON.stringify before displaying objects

Created on 1 May 2019  路  11Comments  路  Source: sveltejs/svelte

<script>
  let arr = [{"name": "Alice"},{"name":"Bob"}]
</script>
{arr}

will display

[object Object],[object Object]

Instead, it would be very helpful for debugging if it displayed the output of JSON.stringify:

[{"name": "Alice"},{"name":"Bob"}]

Most helpful comment

Maybe build something like the @html expression:
{@json myObject}

What do you think?

All 11 comments

Svelte can't generally tell at compile time whether something is an array/POJO/etc. Adding this would introduce an additional runtime cost for everyone, as the component would need to check the type of each thing it's displaying. (Not to mention it being a breaking change, if someone were for example relying on the current way that arrays are stringified.)

You can access globals directly from the template as well though, so you could just use something like {JSON.stringify(arr)} if you know you want to be displaying the stringified version of something.

I expect there would be a runtime hit. Perhaps only in debug mode. Sorry to hear this won't be considered, it's a great feature in other frameworks

We definitely don't want behaviour to differ between dev and prod in a significant way like that, because it would result in broken apps in production 馃榾 It's always better to be predictable and explicit than to save a few keystrokes

We definitely don't want behaviour to differ between dev and prod in a significant way like that, because it would result in broken apps in production

I certainly understand and agree with this. Thanks for the consideration.

How about a bit of template syntax which means "print a JSON string instead, using a cycle-safe and stable JSON printer rather than the lousy one built in to JS" ? As a compiler feature, it would cost nothing for programs that don't use it.

Maybe build something like the @html expression:
{@json myObject}

What do you think?

I'm writing {JSON.stringify(list, null, 2)} quite often. A shorthand would definitely save time here. How about {@json list} or {@list}?

You could configure your editor with a snippet (in VSCode):

    "Insert json stringify": {
        "prefix": "json",
        "body": "JSON.stringify($1$TM_SELECTED_TEXT, null, 2)"
    }

https://code.visualstudio.com/docs/editor/userdefinedsnippets

You always can create a component like this one - https://www.npmjs.com/package/svelte-debugger

<Debugger
  data={example}
  indent={2}
  colorOptions={{ falseColor: '#ff3e00', trueColor: '#40b3ff' }}
/>

@ftonato I tried to use the Debugger component with a binding mechanism but it seems like the output is static. Is there a chance to keep it consistent (e.g. reevaluate the component output based on input changes updates)?

@ftonato I tried to use the Debugger component with a binding mechanism but it seems like the output is static. Is there a chance to keep it consistent (e.g. reevaluate the component output based on input changes updates)?

Hello master,
Thanks for sharing, this should already be working, I will open an issue, and I will notify you when it is ready, can you provide an example to reproduce the problem? it will be great!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

juniorsd picture juniorsd  路  3Comments

sskyy picture sskyy  路  3Comments

matt3224 picture matt3224  路  3Comments

Rich-Harris picture Rich-Harris  路  3Comments

ricardobeat picture ricardobeat  路  3Comments