Skip to content

Commit da5da90

Browse files
committed
Fix #155, handle an edge case with code fences in object value context
1 parent 8eee599 commit da5da90

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ requires = ["setuptools>=61.0"]
33
build-backend = "setuptools.build_meta"
44
[project]
55
name = "json_repair"
6-
version = "0.52.1"
6+
version = "0.52.2"
77
license = "MIT"
88
license-files = ["LICENSE"]
99
authors = [

src/json_repair/parse_string.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,17 @@ def parse_string(self: "JSONParser") -> JSONReturnType:
206206
# Check if the object is really over, to avoid doubling the closing brace
207207
i = self.skip_whitespaces_at(idx=1, move_main_index=False)
208208
next_c = self.get_char_at(i)
209+
if next_c and next_c == "`":
210+
# This could be a special case in which the LLM added code fences after the object
211+
# So we need to check if there are another two ` after this one`
212+
next_c = self.get_char_at(i + 1)
213+
if next_c and next_c == "`":
214+
next_c = self.get_char_at(i + 2)
215+
if next_c and next_c == "`":
216+
self.log(
217+
"While parsing a string in object value context, we found a } that closes the object before code fences, stopping here",
218+
)
219+
break
209220
if not next_c:
210221
self.log(
211222
"While parsing a string in object value context, we found a } that closes the object, stopping here",

tests/test_parse_object.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def test_parse_object_edge_cases():
6363
assert repair_json("{text:words{words in brackets}}") == '{"text": "words{words in brackets}"}'
6464
assert repair_json("{text:words{words in brackets}m}") == '{"text": "words{words in brackets}m"}'
6565
assert repair_json('{"key": "value, value2"```') == '{"key": "value, value2"}'
66+
assert repair_json('{"key": "value}```') == '{"key": "value"}'
6667
assert repair_json("{key:value,key2:value2}") == '{"key": "value", "key2": "value2"}'
6768
assert repair_json('{"key:"value"}') == '{"key": "value"}'
6869
assert repair_json('{"key:value}') == '{"key": "value"}'

0 commit comments

Comments
 (0)