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

Values API: Strict obedience to JSON syntax causes hard-to-debug exceptions for common use cases #353

Closed
DamnedScholar opened this issue Dec 17, 2020 · 2 comments

Comments

@DamnedScholar
Copy link

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.

  1. 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.
  2. 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.

@javan
Copy link
Contributor

javan commented Dec 17, 2020

Can you share some sample view template code from the Python world that results in HTML with invalid JSON?

@sect2k
Copy link

sect2k commented Feb 19, 2021

@DamnedScholar in python you need to use json module's dumpmethod to convert native data types (string, dicts, lists, etc...) to json.

Example:

import json
data = {'key': 'value'}
print(json.dumps(data))
#outputs {"key": "value"}

Using native python data types with stimulus in templates will not work.

You can implement a simple template filter, to do it no the fly in the template if needed.

import json
from django import template

register = template.Library()

@register.filter(name='json')
def to_json(value):
    return json.dumps(value)

@javan this is not a stimulus issues, I think it's safe to close. Been using stimulus with django for a while now, and have had no such issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants