Skip to content

Commit e7aeb5f

Browse files
authoredApr 13, 2024
Fix jsonc parser issue with quotes in strings (#15)
1 parent beef315 commit e7aeb5f

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed
 

‎src/objdictgen/jsonod.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ class ValidationError(Exception):
167167

168168
# Remove jsonc annotations
169169
# Copied from https://github.com/NickolaiBeloguzov/jsonc-parser/blob/master/jsonc_parser/parser.py#L11-L39
170-
RE_JSONC = re.compile(r"(\".*?\"|\'.*?\')|(/\*.*?\*/|//[^\r\n]*$)", re.MULTILINE | re.DOTALL)
170+
RE_JSONC = re.compile(r"(\".*?(?<!\\)\"|\'.*?\')|(\s*/\*.*?\*/\s*|\s*//[^\r\n]*$)", re.MULTILINE | re.DOTALL)
171171

172172

173-
def remove_jasonc(text: str) -> str:
173+
def remove_jsonc(text: str) -> str:
174174
""" Remove jsonc annotations """
175175
def _re_sub(match: re.Match[str]) -> str:
176176
if match.group(2) is not None:
@@ -406,7 +406,7 @@ def generate_node(contents: str|TODJson) -> "Node":
406406
if isinstance(contents, str):
407407

408408
# Remove jsonc annotations
409-
jsontext = remove_jasonc(contents)
409+
jsontext = remove_jsonc(contents)
410410

411411
# Load the json
412412
jd: TODJson = json.loads(jsontext)
@@ -426,7 +426,7 @@ def generate_node(contents: str|TODJson) -> "Node":
426426
global SCHEMA # pylint: disable=global-statement
427427
if not SCHEMA:
428428
with open(objdictgen.JSON_SCHEMA, 'r', encoding="utf-8") as f:
429-
SCHEMA = json.loads(remove_jasonc(f.read()))
429+
SCHEMA = json.loads(remove_jsonc(f.read()))
430430

431431
if SCHEMA and jd.get('$version') == JSON_VERSION:
432432
jsonschema.validate(jd, schema=SCHEMA)

‎tests/test_jsonod.py

+43-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,51 @@
11
import pytest
22
from pprint import pprint
33
from objdictgen import Node
4-
from objdictgen.jsonod import generate_jsonc, generate_node
4+
from objdictgen.jsonod import generate_jsonc, generate_node, remove_jsonc
55
from .test_odcompare import shave_equal
66

7+
8+
def test_jsonod_remove_jsonc():
9+
""" Test that the remove_jsonc function works as expected. """
10+
11+
out = remove_jsonc("""{
12+
"a": "abc", // remove
13+
"b": 42
14+
}""")
15+
assert out == """{
16+
"a": "abc",
17+
"b": 42
18+
}"""
19+
20+
# This was a bug where there quoted string made jsonc parsing fail
21+
out = remove_jsonc("""{
22+
"a": "a\\"bc", // remove
23+
"b": 42
24+
}""")
25+
assert out == """{
26+
"a": "a\\"bc",
27+
"b": 42
28+
}"""
29+
30+
out = remove_jsonc("""{
31+
"a": "a\\"bc", /* remove it */ "c": 42,
32+
"b": 42
33+
}""")
34+
assert out == """{
35+
"a": "a\\"bc","c": 42,
36+
"b": 42
37+
}"""
38+
39+
out = remove_jsonc("""{
40+
"a": "a'bc", // remove
41+
"b": 42
42+
}""")
43+
assert out == """{
44+
"a": "a'bc",
45+
"b": 42
46+
}"""
47+
48+
749
def test_jsonod_roundtrip(odjsoneds):
850
""" Test that the file can be exported to json and that the loaded file
951
is equal to the first.

0 commit comments

Comments
 (0)
Please sign in to comment.