Skip to content

Multi-line string support #82

Closed
Closed

Description

@normanr

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 😞

Activity

  1. dpranke commented on Nov 10, 2024

    @dpranke
    Owner

    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.

  2. self-assigned this
    on Nov 10, 2024
  3. normanr commented on Nov 11, 2024

    @normanr
    Author

    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.

  4. dpranke commented on Nov 11, 2024

    @dpranke
    Owner

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

  5. dpranke commented on Nov 11, 2024

    @dpranke
    Owner

    Okay, fixed now in version v0.9.26.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions