linjackson78 / jstyleson

A python library to parse JSON with js-style comments.
MIT License
38 stars 4 forks source link

Looks like don't work in 3.7 #2

Open Artyrm opened 4 years ago

Artyrm commented 4 years ago

====================================================================== ERROR: test_dispose (main.TestDispose)

Traceback (most recent call last): File "jstyleson_test.py", line 62, in test_dispose result = json.loads(disposed_json) File "C:\Python\Python37\lib\json__init__.py", line 348, in loads return _default_decoder.decode(s) File "C:\Python\Python37\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python\Python37\lib\json\decoder.py", line 353, in raw_decode obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 9 column 10 (char 265)

====================================================================== ERROR: test_loads (main.TestDispose)

Traceback (most recent call last): File "jstyleson_test.py", line 68, in test_loads result = jstyleson.loads(json_test_case) File "C:\Python\Python37\lib\site-packages\jstyleson.py", line 123, in loads return json.loads(dispose(text), **kwargs) File "C:\Python\Python37\lib\json__init__.py", line 348, in loads return _default_decoder.decode(s) File "C:\Python\Python37\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python\Python37\lib\json\decoder.py", line 353, in raw_decode obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 9 column 10 (char 265)


YangRongAtGit commented 3 years ago

Could you post your JSON string as well?

Artyrm commented 3 years ago

I guess that was just embedded samples:

json_test_case = """
{
    /*asterisk style comment with escape inside\*/
    "string": "string",
    "string_with_comment_inside": "//single line comment and /*multi-line comment*/",
    "number": 2,
    "foo": "bar", //inline comment
    "array": [1,2, /*comments inside array*/ 3],
    "nested_casual_array": [1, [2.0, 2.5,], 3, {"key_in_arr": "value_in_arr",},/*trailing comma*/],
    //single line comment
    /*multi
    line
    "comment with many "quotes"quotes"quotes"
    * followed sometime after by a / but that does not end the comment
    */
    "object": {
        "key": "中文"
    },
    "nested_casual_object": {
        "key": "中文",
        "key_in_obj": ["1_in_obj", "2_in_obj",/*trailing comma*/],
        "casual_object": {
            "key1": "value1",
            "key2": "value2",/*trailing comma*/
        },//trailing comma
    }
    //comment with 中文
}
"""

json_expected = {
    "string": "string",
    "string_with_comment_inside": "//single line comment and /*multi-line comment*/",
    "number": 2,
    "foo": "bar",
    "array": [1, 2, 3],
    "nested_casual_array": [1, [2.0, 2.5], 3, {"key_in_arr": "value_in_arr"}],
    "object": {
        "key": u"中文"
    },
    "nested_casual_object": {
        "key": u"中文",
        "key_in_obj": ["1_in_obj", "2_in_obj"],
        "casual_object": {
            "key1": "value1",
            "key2": "value2"
        }
    }
}
Arcitec commented 3 years ago

Yeah, JSON specification requires that all keys are in double quotes, like this:

{"foo":1}

This is valid JavaScript but invalid JSON:

{foo:1}

Also, trailing commas are valid JavaScript (and very common) but invalid JSON:

{"foo":1,}

Those are the problems I can see with this parser.

Another problem, @linjackson78, is that https://pypi.org/project/jstyleson/#description is extremely out of date compared to the repo. ;-)

Looks like this project won't solve my problem and I will use regular regex to extract the data from the dirty JavaScript object I have. But thanks anyway and good luck. :-)

Update: Found a solution, see the next comment.

Arcitec commented 3 years ago

@YangRongAtGit @Artyrm

You should switch to JSON5 instead of this library, for much more robust parsing: https://github.com/linjackson78/jstyleson/issues/4