You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using Python to build pages on my server, and it naturally outputs strings with single quotes when I pass a list into my template. We all know how our friend JSON feels about that: he sends us a sternly worded letter that goes something like Exception: SyntaxError: Unexpected token ' in JSON at position 1 at JSON.parse. Except that when the code that raises the exception is abstracted away beneath a forgiving library like Stimulus, the warning isn't printed to the console, but buried in the JS object you have to retrieve through console.log to find out why your values aren't working. Before Stimulus v2, I had solved the issue by doing a string replace on my input to sanitize it and make JSON friendly to it. If Stimulus is going to be auto-converting values, it should be a little more open-minded about what a string in an array or object looks like, for two reasons.
Python prefers single quotes. Other languages might as well, but the intersection of Python + JavaScript is massive and a liberal interpreter that allows Python's syntax would turn a minor pain point into a non-issue without changing anything.
The easiest way to write an array in HTML is to use alternating quote styles.
I was only getting away with the string replace because I was working with a set of highly controlled URLs that will never have special characters in them, but for general importing-data-from-HTML purposes, it might make sense to use JSON5 to allow for fewer constraints on how the data comes in, or at the very least a try->catch block that replaces all non-escaped single quotes if that SyntaxError comes back.
Thoughts? I'll submit the PR, but my preference would be to pull in the dependency and not worry about the details since they've been outsourced, and that's not as simple as just fixing a bug.
The text was updated successfully, but these errors were encountered:
I'm using Python to build pages on my server, and it naturally outputs strings with single quotes when I pass a list into my template. We all know how our friend JSON feels about that: he sends us a sternly worded letter that goes something like
Exception: SyntaxError: Unexpected token ' in JSON at position 1 at JSON.parse
. Except that when the code that raises the exception is abstracted away beneath a forgiving library like Stimulus, the warning isn't printed to the console, but buried in the JS object you have to retrieve throughconsole.log
to find out why your values aren't working. Before Stimulus v2, I had solved the issue by doing a string replace on my input to sanitize it and make JSON friendly to it. If Stimulus is going to be auto-converting values, it should be a little more open-minded about what a string in an array or object looks like, for two reasons.I was only getting away with the string replace because I was working with a set of highly controlled URLs that will never have special characters in them, but for general importing-data-from-HTML purposes, it might make sense to use JSON5 to allow for fewer constraints on how the data comes in, or at the very least a
try->catch
block that replaces all non-escaped single quotes if thatSyntaxError
comes back.Thoughts? I'll submit the PR, but my preference would be to pull in the dependency and not worry about the details since they've been outsourced, and that's not as simple as just fixing a bug.
The text was updated successfully, but these errors were encountered: