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 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",... }'''>>>importjson5>>>json5.version.VERSION'0.9.25'>>>json5.loads(foo)
Traceback (mostrecentcalllast):
File"<stdin>", line1, in<module>File".../site-packages/json5/lib.py", line108, inloadsraiseValueError(err)
ValueError: <string>:2Unexpected""atcolumn14
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 😞
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.
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).
Activity
dpranke commentedon 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:
So, what you're trying to do by default won't work.
However, it's generally my intent for
json5to mirror thejsoninterface as much as possible, but I guess I somehow missed thestrictparameter. I'd be happy to add that, and then I think you'd be okay.normanr commentedon Nov 11, 2024
I think adding a
strictstyle 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 commentedon Nov 11, 2024
Yup, I'll have a fix for this posted in a little bit, just need to write some tests ... :).
dpranke commentedon Nov 11, 2024
Okay, fixed now in version v0.9.26.