Skip to content

Multi-line string support #82

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

Closed
normanr opened this issue Nov 9, 2024 · 4 comments
Closed

Multi-line string support #82

normanr opened this issue Nov 9, 2024 · 4 comments
Assignees

Comments

@normanr
Copy link

normanr commented Nov 9, 2024

I'm trying to get multi-line strings (from input I don't control) to work (the README claims "multi-line string literals are allowed", so I'm assuming it should work), but it doesn't seem to:

>>> foo='''{
... "key": "value
... over two lines",
... }'''
>>> import json5
>>> json5.version.VERSION
'0.9.25'
>>> json5.loads(foo)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../site-packages/json5/lib.py", line 108, in loads
    raise ValueError(err)
ValueError: <string>:2 Unexpected "
" at column 14

The built in python json library does support multi-line strings (with strict=False), but can't handle the trailing comma (which is why I'm using json5).

Is there an option to support this? Am I doing something wrong?

Context: I'm trying to parse user data that's currently being parsed with the Newtonsoft.Json c# parser. It seems to accept bare-newlines in json strings 😞

@dpranke
Copy link
Owner

dpranke commented Nov 10, 2024

Hi!

Per the spec, multiline strings in JSON5 (and Javascript, unless you're using template literals) require a line continuation mark ("") at the end of the line to work:

>>> foo='''{
... "key": "value\\
... over two lines",
... }'''
>>> foo
'{\n"key": "value\\\nover two lines",\n}'
>>> import json5
>>> json5.VERSION
'0.9.24'
>>> json5.loads(foo)
{'key': 'valueover two lines'}

So, what you're trying to do by default won't work.

However, it's generally my intent for json5 to mirror the json interface as much as possible, but I guess I somehow missed the strict parameter. I'd be happy to add that, and then I think you'd be okay.

@dpranke dpranke self-assigned this Nov 10, 2024
@normanr
Copy link
Author

normanr commented Nov 11, 2024

I think adding a strict style parameter would be fine.

Unfortunately I don't control the json I'm parsing and I know it's malformed, but "the Newtonsoft.Json c# parser can parse it fine", so I'm trying to find a Python based parser that can do the same (at least consume the input without raising an exception).

Note that Newtonsoft.Json, doesn't seem to handle line continuation at all: https://dotnetfiddle.net/lHgHvp, but I'm fine with the parsing result being different between Python and c# as I'm not reading the field with the raw newlines anyways.

@dpranke
Copy link
Owner

dpranke commented Nov 11, 2024

Yup, I'll have a fix for this posted in a little bit, just need to write some tests ... :).

@dpranke
Copy link
Owner

dpranke commented Nov 11, 2024

Okay, fixed now in version v0.9.26.

@dpranke dpranke closed this as completed Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants