jstyleson
jstyleson copied to clipboard
Looks like don't work in 3.7
====================================================================== 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)
Could you post your JSON string as well?
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"
}
}
}
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.
@YangRongAtGit @Artyrm