json-repair 0.28.2__tar.gz → 0.28.4__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: json_repair
3
- Version: 0.28.2
3
+ Version: 0.28.4
4
4
  Summary: A package to repair broken json strings
5
5
  Author-email: Stefano Baccianella <4247706+mangiucugna@users.noreply.github.com>
6
6
  License: MIT License
@@ -45,6 +45,21 @@ This simple package can be used to fix an invalid json string. To know all cases
45
45
 
46
46
  Inspired by https://github.com/josdejong/jsonrepair
47
47
 
48
+ ---
49
+ # How to cite
50
+ If you are using this library in your academic work (as I know many folks are) please find the BibTex here:
51
+
52
+ @software{Baccianella_JSON_Repair_-_2024,
53
+ author = {Baccianella, Stefano},
54
+ month = aug,
55
+ title = {{JSON Repair - A python module to repair invalid JSON, commonly used to parse the output of LLMs}},
56
+ url = {https://github.com/mangiucugna/json_repair},
57
+ version = {0.28.3},
58
+ year = {2024}
59
+ }
60
+
61
+ Thank you for citing my work and please send me a link to the paper if you can!
62
+
48
63
  ---
49
64
  # Offer me a beer
50
65
  If you find this library useful, you can help me by donating toward my monthly beer budget here: https://github.com/sponsors/mangiucugna
@@ -82,6 +97,18 @@ or just
82
97
 
83
98
  decoded_object = json_repair.repair_json(json_string, return_objects=True)
84
99
 
100
+ ### Avoid this antipattern
101
+ Some users of this library adopt the following pattern:
102
+
103
+ obj = {}
104
+ try:
105
+ obj = json.loads(string)
106
+ except json.JSONDecodeError as e:
107
+ obj = json_repair.loads(string)
108
+ ...
109
+
110
+ This is wasteful because `json_repair` will already verify for you if the JSON is valid, if you still want to do that then add `skip_json_loads=True` to the call as explained the section below.
111
+
85
112
  ### Read json from a file or file descriptor
86
113
 
87
114
  JSON repair provides also a drop-in replacement for `json.load()`:
@@ -122,6 +149,7 @@ Some rules of thumb to use:
122
149
  - Setting `return_objects=True` will always be faster because the parser returns an object already and it doesn't have serialize that object to JSON
123
150
  - `skip_json_loads` is faster only if you 100% know that the string is not a valid JSON
124
151
  - If you are having issues with escaping pass the string as **raw** string like: `r"string with escaping\""`
152
+
125
153
  ## Adding to requirements
126
154
  **Please pin this library only on the major version!**
127
155
 
@@ -7,6 +7,21 @@ This simple package can be used to fix an invalid json string. To know all cases
7
7
 
8
8
  Inspired by https://github.com/josdejong/jsonrepair
9
9
 
10
+ ---
11
+ # How to cite
12
+ If you are using this library in your academic work (as I know many folks are) please find the BibTex here:
13
+
14
+ @software{Baccianella_JSON_Repair_-_2024,
15
+ author = {Baccianella, Stefano},
16
+ month = aug,
17
+ title = {{JSON Repair - A python module to repair invalid JSON, commonly used to parse the output of LLMs}},
18
+ url = {https://github.com/mangiucugna/json_repair},
19
+ version = {0.28.3},
20
+ year = {2024}
21
+ }
22
+
23
+ Thank you for citing my work and please send me a link to the paper if you can!
24
+
10
25
  ---
11
26
  # Offer me a beer
12
27
  If you find this library useful, you can help me by donating toward my monthly beer budget here: https://github.com/sponsors/mangiucugna
@@ -44,6 +59,18 @@ or just
44
59
 
45
60
  decoded_object = json_repair.repair_json(json_string, return_objects=True)
46
61
 
62
+ ### Avoid this antipattern
63
+ Some users of this library adopt the following pattern:
64
+
65
+ obj = {}
66
+ try:
67
+ obj = json.loads(string)
68
+ except json.JSONDecodeError as e:
69
+ obj = json_repair.loads(string)
70
+ ...
71
+
72
+ This is wasteful because `json_repair` will already verify for you if the JSON is valid, if you still want to do that then add `skip_json_loads=True` to the call as explained the section below.
73
+
47
74
  ### Read json from a file or file descriptor
48
75
 
49
76
  JSON repair provides also a drop-in replacement for `json.load()`:
@@ -84,6 +111,7 @@ Some rules of thumb to use:
84
111
  - Setting `return_objects=True` will always be faster because the parser returns an object already and it doesn't have serialize that object to JSON
85
112
  - `skip_json_loads` is faster only if you 100% know that the string is not a valid JSON
86
113
  - If you are having issues with escaping pass the string as **raw** string like: `r"string with escaping\""`
114
+
87
115
  ## Adding to requirements
88
116
  **Please pin this library only on the major version!**
89
117
 
@@ -3,7 +3,7 @@ requires = ["setuptools>=61.0"]
3
3
  build-backend = "setuptools.build_meta"
4
4
  [project]
5
5
  name = "json_repair"
6
- version = "0.28.2"
6
+ version = "0.28.4"
7
7
  license = {file = "LICENSE"}
8
8
  authors = [
9
9
  { name="Stefano Baccianella", email="4247706+mangiucugna@users.noreply.github.com" },
@@ -29,19 +29,52 @@ from typing import Any, Dict, List, Optional, Union, TextIO, Tuple, Literal
29
29
 
30
30
  class StringFileWrapper:
31
31
  # This is a trick to simplify the code, transform the filedescriptor handling into a string handling
32
- def __init__(self, fd: TextIO) -> None:
32
+ def __init__(self, fd: TextIO, CHUNK_LENGTH: int) -> None:
33
33
  self.fd = fd
34
34
  self.length: int = 0
35
-
36
- def __getitem__(self, index: int | slice) -> str:
35
+ # Buffers are 1MB strings that are read from the file
36
+ # and kept in memory to keep reads low
37
+ self.buffers: dict[int, str] = {}
38
+ # CHUNK_LENGTH is in bytes
39
+ if not CHUNK_LENGTH or CHUNK_LENGTH < 2:
40
+ CHUNK_LENGTH = 1_000_000
41
+ self.buffer_length = CHUNK_LENGTH
42
+
43
+ def fill_buffer(self, index: int) -> None:
44
+ if self.buffers.get(index) is None:
45
+ self.fd.seek(index * self.buffer_length)
46
+ self.buffers[index] = self.fd.read(self.buffer_length)
47
+ # Save memory by keeping max 2MB buffer chunks and min 2 chunks
48
+ if len(self.buffers) > max(2, 2_000_000 / self.buffer_length):
49
+ oldest_key = next(iter(self.buffers))
50
+ self.buffers.pop(oldest_key)
51
+
52
+ def __getitem__(self, index: Union[int, slice]) -> str:
53
+ # The buffer is an array that is seek like a RAM:
54
+ # self.buffers[index]: the row in the array of length 1MB, index is `i` modulo CHUNK_LENGTH
55
+ # self.buffures[index][j]: the column of the row that is `i` remainder CHUNK_LENGTH
37
56
  if isinstance(index, slice):
38
- self.fd.seek(index.start)
39
- value = self.fd.read(index.stop - index.start)
40
- self.fd.seek(index.start)
41
- return value
57
+ buffer_index = index.start // self.buffer_length
58
+ buffer_end = index.stop // self.buffer_length
59
+ for i in range(buffer_index, buffer_end + 1):
60
+ self.fill_buffer(i)
61
+ if buffer_index == buffer_end:
62
+ return self.buffers[buffer_index][
63
+ index.start % self.buffer_length : index.stop % self.buffer_length
64
+ ]
65
+ else:
66
+ start_slice = self.buffers[buffer_index][
67
+ index.start % self.buffer_length :
68
+ ]
69
+ end_slice = self.buffers[buffer_end][: index.stop % self.buffer_length]
70
+ middle_slices = [
71
+ self.buffers[i] for i in range(buffer_index + 1, buffer_end)
72
+ ]
73
+ return start_slice + "".join(middle_slices) + end_slice
42
74
  else:
43
- self.fd.seek(index)
44
- return self.fd.read(1)
75
+ buffer_index = index // self.buffer_length
76
+ self.fill_buffer(buffer_index)
77
+ return self.buffers[buffer_index][index % self.buffer_length]
45
78
 
46
79
  def __len__(self) -> int:
47
80
  if self.length < 1:
@@ -69,13 +102,14 @@ class JSONParser:
69
102
  json_str: Union[str, StringFileWrapper],
70
103
  json_fd: Optional[TextIO],
71
104
  logging: Optional[bool],
105
+ json_fd_chunk_length: int = 0,
72
106
  ) -> None:
73
107
  # The string to parse
74
108
  self.json_str = json_str
75
109
  # Alternatively, the file description with a json file in it
76
110
  if json_fd:
77
111
  # This is a trick we do to treat the file wrapper as an array
78
- self.json_str = StringFileWrapper(json_fd)
112
+ self.json_str = StringFileWrapper(json_fd, json_fd_chunk_length)
79
113
  # Index is our iterator that will keep track of which character we are looking at right now
80
114
  self.index: int = 0
81
115
  # This is used in the object member parsing to manage the special cases of missing quotes in key or value
@@ -639,6 +673,7 @@ def repair_json(
639
673
  logging: bool = False,
640
674
  json_fd: Optional[TextIO] = None,
641
675
  ensure_ascii: bool = True,
676
+ chunk_length: int = 0,
642
677
  ) -> Union[JSONReturnType, Tuple[JSONReturnType, List[Dict[str, str]]]]:
643
678
  """
644
679
  Given a json formatted string, it will try to decode it and, if it fails, it will try to fix it.
@@ -647,7 +682,7 @@ def repair_json(
647
682
  When `skip_json_loads=True` is passed, it will not call the built-in json.loads() function
648
683
  When `logging=True` is passed, it will return a tuple with the repaired json and a log of all repair actions
649
684
  """
650
- parser = JSONParser(json_str, json_fd, logging)
685
+ parser = JSONParser(json_str, json_fd, logging, chunk_length)
651
686
  if skip_json_loads:
652
687
  parsed_json = parser.parse()
653
688
  else:
@@ -683,7 +718,10 @@ def loads(
683
718
 
684
719
 
685
720
  def load(
686
- fd: TextIO, skip_json_loads: bool = False, logging: bool = False
721
+ fd: TextIO,
722
+ skip_json_loads: bool = False,
723
+ logging: bool = False,
724
+ chunk_length: int = 0,
687
725
  ) -> Union[JSONReturnType, Tuple[JSONReturnType, List[Dict[str, str]]]]:
688
726
  """
689
727
  This function works like `json.load()` except that it will fix your JSON in the process.
@@ -691,6 +729,7 @@ def load(
691
729
  """
692
730
  return repair_json(
693
731
  json_fd=fd,
732
+ chunk_length=chunk_length,
694
733
  return_objects=True,
695
734
  skip_json_loads=skip_json_loads,
696
735
  logging=logging,
@@ -701,12 +740,18 @@ def from_file(
701
740
  filename: str,
702
741
  skip_json_loads: bool = False,
703
742
  logging: bool = False,
743
+ chunk_length: int = 0,
704
744
  ) -> Union[JSONReturnType, Tuple[JSONReturnType, List[Dict[str, str]]]]:
705
745
  """
706
746
  This function is a wrapper around `load()` so you can pass the filename as string
707
747
  """
708
748
  fd = open(filename)
709
- jsonobj = load(fd, skip_json_loads, logging)
749
+ jsonobj = load(
750
+ fd=fd,
751
+ skip_json_loads=skip_json_loads,
752
+ logging=logging,
753
+ chunk_length=chunk_length,
754
+ )
710
755
  fd.close()
711
756
 
712
757
  return jsonobj
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: json_repair
3
- Version: 0.28.2
3
+ Version: 0.28.4
4
4
  Summary: A package to repair broken json strings
5
5
  Author-email: Stefano Baccianella <4247706+mangiucugna@users.noreply.github.com>
6
6
  License: MIT License
@@ -45,6 +45,21 @@ This simple package can be used to fix an invalid json string. To know all cases
45
45
 
46
46
  Inspired by https://github.com/josdejong/jsonrepair
47
47
 
48
+ ---
49
+ # How to cite
50
+ If you are using this library in your academic work (as I know many folks are) please find the BibTex here:
51
+
52
+ @software{Baccianella_JSON_Repair_-_2024,
53
+ author = {Baccianella, Stefano},
54
+ month = aug,
55
+ title = {{JSON Repair - A python module to repair invalid JSON, commonly used to parse the output of LLMs}},
56
+ url = {https://github.com/mangiucugna/json_repair},
57
+ version = {0.28.3},
58
+ year = {2024}
59
+ }
60
+
61
+ Thank you for citing my work and please send me a link to the paper if you can!
62
+
48
63
  ---
49
64
  # Offer me a beer
50
65
  If you find this library useful, you can help me by donating toward my monthly beer budget here: https://github.com/sponsors/mangiucugna
@@ -82,6 +97,18 @@ or just
82
97
 
83
98
  decoded_object = json_repair.repair_json(json_string, return_objects=True)
84
99
 
100
+ ### Avoid this antipattern
101
+ Some users of this library adopt the following pattern:
102
+
103
+ obj = {}
104
+ try:
105
+ obj = json.loads(string)
106
+ except json.JSONDecodeError as e:
107
+ obj = json_repair.loads(string)
108
+ ...
109
+
110
+ This is wasteful because `json_repair` will already verify for you if the JSON is valid, if you still want to do that then add `skip_json_loads=True` to the call as explained the section below.
111
+
85
112
  ### Read json from a file or file descriptor
86
113
 
87
114
  JSON repair provides also a drop-in replacement for `json.load()`:
@@ -122,6 +149,7 @@ Some rules of thumb to use:
122
149
  - Setting `return_objects=True` will always be faster because the parser returns an object already and it doesn't have serialize that object to JSON
123
150
  - `skip_json_loads` is faster only if you 100% know that the string is not a valid JSON
124
151
  - If you are having issues with escaping pass the string as **raw** string like: `r"string with escaping\""`
152
+
125
153
  ## Adding to requirements
126
154
  **Please pin this library only on the major version!**
127
155
 
@@ -0,0 +1,265 @@
1
+ from src.json_repair.json_repair import from_file, repair_json, loads
2
+
3
+ def test_basic_types_valid():
4
+ assert repair_json("True", return_objects=True) == ""
5
+ assert repair_json("False", return_objects=True) == ""
6
+ assert repair_json("Null", return_objects=True) == ""
7
+ assert repair_json("1", return_objects=True) == 1
8
+ assert repair_json("[]", return_objects=True) == []
9
+ assert repair_json("[1, 2, 3, 4]", return_objects=True) == [1, 2, 3, 4]
10
+ assert repair_json("{}", return_objects=True) == {}
11
+ assert repair_json('{ "key": "value", "key2": 1, "key3": True }', return_objects=True) == { "key": "value", "key2": 1, "key3": True }
12
+
13
+ def test_basic_types_invalid():
14
+ assert repair_json("true", return_objects=True) == True
15
+ assert repair_json("false", return_objects=True) == False
16
+ assert repair_json("null", return_objects=True) == None
17
+ assert repair_json("1.2", return_objects=True) == 1.2
18
+ assert repair_json("[", return_objects=True) == []
19
+ assert repair_json("[1, 2, 3, 4", return_objects=True) == [1, 2, 3, 4]
20
+ assert repair_json("{", return_objects=True) == {}
21
+ assert repair_json('{ "key": value, "key2": 1 "key3": null }', return_objects=True) == { "key": "value", "key2": 1, "key3": None }
22
+
23
+ def test_valid_json():
24
+ assert (
25
+ repair_json('{"name": "John", "age": 30, "city": "New York"}')
26
+ == '{"name": "John", "age": 30, "city": "New York"}'
27
+ )
28
+ assert (
29
+ repair_json('{"employees":["John", "Anna", "Peter"]} ')
30
+ == '{"employees": ["John", "Anna", "Peter"]}'
31
+ )
32
+ assert repair_json('{"key": "value:value"}') == '{"key": "value:value"}'
33
+ assert (
34
+ repair_json('{"text": "The quick brown fox,"}')
35
+ == '{"text": "The quick brown fox,"}'
36
+ )
37
+ assert (
38
+ repair_json('{"text": "The quick brown fox won\'t jump"}')
39
+ == '{"text": "The quick brown fox won\'t jump"}'
40
+ )
41
+ assert repair_json('{"key": ""') == '{"key": ""}'
42
+ assert (
43
+ repair_json('{"key1": {"key2": [1, 2, 3]}}') == '{"key1": {"key2": [1, 2, 3]}}'
44
+ )
45
+ assert (
46
+ repair_json('{"key": 12345678901234567890}') == '{"key": 12345678901234567890}'
47
+ )
48
+ assert repair_json('{"key": "value\u263A"}') == '{"key": "value\\u263a"}'
49
+ assert repair_json('{"key": "value\\nvalue"}') == '{"key": "value\\nvalue"}'
50
+
51
+ def test_brackets_edge_cases():
52
+ assert repair_json("[{]") == "[{}]"
53
+ assert repair_json(" { } ") == "{}"
54
+ assert repair_json("[") == "[]"
55
+ assert repair_json("]") == '""'
56
+ assert repair_json("{") == "{}"
57
+ assert repair_json("}") == '""'
58
+ assert repair_json('{"') == '{}'
59
+ assert repair_json('["') == '[]'
60
+ assert repair_json('{foo: [}') == '{"foo": []}'
61
+
62
+ def test_general_edge_cases():
63
+ assert repair_json("\"") == '""'
64
+ assert repair_json("\n") == '""'
65
+ assert repair_json(" ") == '""'
66
+ assert repair_json("[[1\n\n]") == "[[1]]"
67
+ assert repair_json("string") == '""'
68
+ assert repair_json("stringbeforeobject {}") == '{}'
69
+
70
+ def test_mixed_data_types():
71
+ assert repair_json(' {"key": true, "key2": false, "key3": null}') == '{"key": true, "key2": false, "key3": null}'
72
+ assert repair_json('{"key": TRUE, "key2": FALSE, "key3": Null} ') == '{"key": true, "key2": false, "key3": null}'
73
+
74
+ def test_missing_and_mixed_quotes():
75
+ assert repair_json("{'key': 'string', 'key2': false, \"key3\": null, \"key4\": unquoted}") == '{"key": "string", "key2": false, "key3": null, "key4": "unquoted"}'
76
+ assert (
77
+ repair_json('{"name": "John", "age": 30, "city": "New York')
78
+ == '{"name": "John", "age": 30, "city": "New York"}'
79
+ )
80
+ assert (
81
+ repair_json('{"name": "John", "age": 30, city: "New York"}')
82
+ == '{"name": "John", "age": 30, "city": "New York"}'
83
+ )
84
+ assert (
85
+ repair_json('{"name": "John", "age": 30, "city": New York}')
86
+ == '{"name": "John", "age": 30, "city": "New York"}'
87
+ )
88
+ assert (
89
+ repair_json('{"name": John, "age": 30, "city": "New York"}')
90
+ == '{"name": "John", "age": 30, "city": "New York"}'
91
+ )
92
+ assert repair_json('{“slanted_delimiter”: "value"}') == '{"slanted_delimiter": "value"}'
93
+ assert (
94
+ repair_json('{"name": "John", "age": 30, "city": "New')
95
+ == '{"name": "John", "age": 30, "city": "New"}'
96
+ )
97
+ assert repair_json('[{"key": "value", COMMENT "notes": "lorem "ipsum", sic." }]') == '[{"key": "value", "notes": "lorem \\"ipsum\\", sic."}]'
98
+ assert repair_json('{"key": ""value"}') == '{"key": "value"}'
99
+ assert repair_json('{"key": "value", 5: "value"}') == '{"key": "value", "5": "value"}'
100
+ assert repair_json('{"foo": "\\"bar\\""') == '{"foo": "\\"bar\\""}'
101
+ assert repair_json('{"" key":"val"') == '{" key": "val"}'
102
+ assert repair_json('{"key": value "key2" : "value2" ') == '{"key": "value", "key2": "value2"}'
103
+
104
+ def test_array_edge_cases():
105
+ assert repair_json("[1, 2, 3,") == "[1, 2, 3]"
106
+ assert repair_json("[1, 2, 3, ...]") == "[1, 2, 3]"
107
+ assert repair_json("[1, 2, ... , 3]") == "[1, 2, 3]"
108
+ assert repair_json("[1, 2, '...', 3]") == '[1, 2, "...", 3]'
109
+ assert repair_json("[true, false, null, ...]") == '[true, false, null]'
110
+ assert repair_json('["a" "b" "c" 1') == '["a", "b", "c", 1]'
111
+ assert repair_json('{"employees":["John", "Anna",') == '{"employees": ["John", "Anna"]}'
112
+ assert repair_json('{"employees":["John", "Anna", "Peter') == '{"employees": ["John", "Anna", "Peter"]}'
113
+ assert repair_json('{"key1": {"key2": [1, 2, 3') == '{"key1": {"key2": [1, 2, 3]}}'
114
+
115
+ def test_escaping():
116
+ assert repair_json("'\"'") == '""'
117
+ assert repair_json("{\"key\": 'string\"\n\t\le'") == '{"key": "string\\"\\n\\tle"}'
118
+ assert repair_json(r'{"real_content": "Some string: Some other string \t Some string <a href=\"https://domain.com\">Some link</a>"') == r'{"real_content": "Some string: Some other string \t Some string <a href=\"https://domain.com\">Some link</a>"}'
119
+ assert repair_json('{"key_1\n": "value"}') == '{"key_1": "value"}'
120
+ assert repair_json('{"key\t_": "value"}') == '{"key\\t_": "value"}'
121
+
122
+
123
+ def test_object_edge_cases():
124
+ assert repair_json('{ ') == '{}'
125
+ assert repair_json('{"": "value"') == '{"": "value"}'
126
+ assert repair_json('{"value_1": true, COMMENT "value_2": "data"}') == '{"value_1": true, "value_2": "data"}'
127
+ assert repair_json('{"value_1": true, SHOULD_NOT_EXIST "value_2": "data" AAAA }') == '{"value_1": true, "value_2": "data"}'
128
+ assert repair_json('{"" : true, "key2": "value2"}') == '{"": true, "key2": "value2"}'
129
+ assert repair_json('{""answer"":[{""traits"":''Female aged 60+'',""answer1"":""5""}]}') == '{"answer": [{"traits": "Female aged 60+", "answer1": "5"}]}'
130
+ assert repair_json('{ "words": abcdef", "numbers": 12345", "words2": ghijkl" }') == '{"words": "abcdef", "numbers": 12345, "words2": "ghijkl"}'
131
+ assert repair_json('''{"number": 1,"reason": "According...""ans": "YES"}''') == '{"number": 1, "reason": "According...", "ans": "YES"}'
132
+ assert repair_json('''{ "a" : "{ b": {} }" }''') == '{"a": "{ b"}'
133
+ assert repair_json("""{"b": "xxxxx" true}""") == '{"b": "xxxxx"}'
134
+ assert repair_json('{"key": "Lorem "ipsum" s,"}') == '{"key": "Lorem \\"ipsum\\" s,"}'
135
+ assert repair_json('{"lorem": ipsum, sic, datum.",}') == '{"lorem": "ipsum, sic, datum."}'
136
+ assert repair_json('{"lorem": sic tamet. "ipsum": sic tamet, quick brown fox. "sic": ipsum}') == '{"lorem": "sic tamet.", "ipsum": "sic tamet", "sic": "ipsum"}'
137
+ assert repair_json('{"key":value, " key2":"value2" }') == '{"key": "value", " key2": "value2"}'
138
+ assert repair_json('{"key":value "key2":"value2" }') == '{"key": "value", "key2": "value2"}'
139
+
140
+ def test_number_edge_cases():
141
+ assert repair_json(' - { "test_key": ["test_value", "test_value2"] }') == '{"test_key": ["test_value", "test_value2"]}'
142
+ assert repair_json('{"key": 1/3}') == '{"key": "1/3"}'
143
+ assert repair_json('{"key": .25}') == '{"key": 0.25}'
144
+ assert repair_json('{"here": "now", "key": 1/3, "foo": "bar"}') == '{"here": "now", "key": "1/3", "foo": "bar"}'
145
+ assert repair_json('{"key": 12345/67890}') == '{"key": "12345/67890"}'
146
+ assert repair_json('[105,12') == '[105, 12]'
147
+ assert repair_json('{"key", 105,12,') == '{"key": "105,12"}'
148
+ assert repair_json('{"key": 1/3, "foo": "bar"}') == '{"key": "1/3", "foo": "bar"}'
149
+ assert repair_json('{"key": 10-20}') == '{"key": "10-20"}'
150
+ assert repair_json('{"key": 1.1.1}') == '{"key": "1.1.1"}'
151
+ assert repair_json('[- ') == '[]'
152
+
153
+ def test_markdown():
154
+ assert repair_json('{ "content": "[LINK]("https://google.com")" }') == '{"content": "[LINK](\\"https://google.com\\")"}'
155
+ assert repair_json('{ "content": "[LINK](" }') == '{"content": "[LINK]("}'
156
+ assert repair_json('{ "content": "[LINK](", "key": true }') == '{"content": "[LINK](", "key": true}'
157
+
158
+ def test_leading_trailing_characters():
159
+ assert repair_json('````{ "key": "value" }```') == '{"key": "value"}'
160
+ assert repair_json("""{ "a": "", "b": [ { "c": 1} ] \n}```""") == '{"a": "", "b": [{"c": 1}]}'
161
+ assert repair_json("Based on the information extracted, here is the filled JSON output: ```json { 'a': 'b' } ```") == '{"a": "b"}'
162
+ assert repair_json("""
163
+ The next 64 elements are:
164
+ ```json
165
+ { "key": "value" }
166
+ ```""") == '{"key": "value"}'
167
+ def test_multiple_jsons():
168
+ assert repair_json("[]{}") == "[[], {}]"
169
+ assert repair_json("{}[]{}") == "[{}, [], {}]"
170
+ assert repair_json('{"key":"value"}[1,2,3,True]') == '[{"key": "value"}, [1, 2, 3, true]]'
171
+ assert repair_json('lorem ```json {"key":"value"} ``` ipsum ```json [1,2,3,True] ``` 42') == '[{"key": "value"}, [1, 2, 3, true]]'
172
+
173
+ def test_repair_json_with_objects():
174
+ # Test with valid JSON strings
175
+ assert repair_json("[]", return_objects=True) == []
176
+ assert repair_json("{}", return_objects=True) == {}
177
+ assert repair_json('{"key": true, "key2": false, "key3": null}', return_objects=True) == {"key": True, "key2": False, "key3": None}
178
+ assert repair_json('{"name": "John", "age": 30, "city": "New York"}', return_objects=True) == {
179
+ "name": "John",
180
+ "age": 30,
181
+ "city": "New York",
182
+ }
183
+ assert repair_json("[1, 2, 3, 4]", return_objects=True) == [1, 2, 3, 4]
184
+ assert repair_json('{"employees":["John", "Anna", "Peter"]} ', return_objects=True) == {
185
+ "employees": ["John", "Anna", "Peter"]
186
+ }
187
+ assert repair_json('''
188
+ {
189
+ "resourceType": "Bundle",
190
+ "id": "1",
191
+ "type": "collection",
192
+ "entry": [
193
+ {
194
+ "resource": {
195
+ "resourceType": "Patient",
196
+ "id": "1",
197
+ "name": [
198
+ {"use": "official", "family": "Corwin", "given": ["Keisha", "Sunny"], "prefix": ["Mrs."},
199
+ {"use": "maiden", "family": "Goodwin", "given": ["Keisha", "Sunny"], "prefix": ["Mrs."]}
200
+ ]
201
+ }
202
+ }
203
+ ]
204
+ }
205
+ ''', return_objects=True) == {"resourceType": "Bundle", "id": "1", "type": "collection", "entry": [{"resource": {"resourceType": "Patient", "id": "1", "name": [{"use": "official", "family": "Corwin", "given": ["Keisha", "Sunny"], "prefix": ["Mrs."]}, {"use": "maiden", "family": "Goodwin", "given": ["Keisha", "Sunny"], "prefix": ["Mrs."]}]}}]}
206
+ assert repair_json('{\n"html": "<h3 id="aaa">Waarom meer dan 200 Technical Experts - "Passie voor techniek"?</h3>"}', return_objects=True) == {'html': '<h3 id="aaa">Waarom meer dan 200 Technical Experts - "Passie voor techniek"?</h3>'}
207
+ assert repair_json("""
208
+ [
209
+ {
210
+ "foo": "Foo bar baz",
211
+ "tag": "#foo-bar-baz"
212
+ },
213
+ {
214
+ "foo": "foo bar "foobar" foo bar baz.",
215
+ "tag": "#foo-bar-foobar"
216
+ }
217
+ ]
218
+ """, return_objects=True) == [{"foo": "Foo bar baz", "tag": "#foo-bar-baz"},{"foo": "foo bar \"foobar\" foo bar baz.", "tag": "#foo-bar-foobar" }]
219
+
220
+ def test_repair_json_skip_json_loads():
221
+ assert repair_json('{"key": true, "key2": false, "key3": null}', skip_json_loads=True) == '{"key": true, "key2": false, "key3": null}'
222
+ assert repair_json('{"key": true, "key2": false, "key3": null}', return_objects=True, skip_json_loads=True) == {"key": True, "key2": False, "key3": None}
223
+ assert repair_json('{"key": true, "key2": false, "key3": }', skip_json_loads=True) == '{"key": true, "key2": false, "key3": ""}'
224
+ assert loads('{"key": true, "key2": false, "key3": }', skip_json_loads=True) == {"key": True, "key2": False, "key3": ""}
225
+
226
+
227
+ def test_repair_json_from_file():
228
+ import os.path
229
+ import pathlib
230
+ import tempfile
231
+
232
+ path = pathlib.Path(__file__).parent.resolve()
233
+
234
+ # Use chunk_length 2 to test the buffering feature
235
+ assert from_file(filename=os.path.join(path,"invalid.json")) == [{"_id": "655b66256574f09bdae8abe8", "index": 0, "guid": "31082ae3-b0f3-4406-90f4-cc450bd4379d", "isActive": False, "balance": "$2,562.78", "picture": "http://placehold.it/32x32", "age": 32, "eyeColor": "brown", "name": "Glover Rivas", "gender": "male", "company": "EMPIRICA", "email": "gloverrivas@empirica.com", "phone": "+1 (842) 507-3063", "address": "536 Montague Terrace, Jenkinsville, Kentucky, 2235", "about": "Mollit consectetur excepteur voluptate tempor dolore ullamco enim irure ullamco non enim officia. Voluptate occaecat proident laboris ea Lorem cupidatat reprehenderit nisi nisi aliqua. Amet nulla ipsum deserunt excepteur amet ad aute aute ex. Et enim minim sit veniam est quis dolor nisi sunt quis eiusmod in. Amet eiusmod cillum sunt occaecat dolor laboris voluptate in eiusmod irure aliqua duis.", "registered": "2023-11-18T09:32:36 -01:00", "latitude": 36.26102, "longitude": -91.304608, "tags": ["non", "tempor", "do", "ullamco", "dolore", "sunt", "ipsum"], "friends": [{"id": 0, "name": "Cara Shepherd"}, {"id": 1, "name": "Mason Farley"}, {"id": 2, "name": "Harriet Cochran"}], "greeting": "Hello, Glover Rivas! You have 7 unread messages.", "favoriteFruit": "strawberry"}, {"_id": "655b662585364bc57278bb6f", "index": 1, "guid": "0dea7a3a-f812-4dde-b78d-7a9b58e5da05", "isActive": True, "balance": "$1,359.48", "picture": "http://placehold.it/32x32", "age": 38, "eyeColor": "brown", "name": "Brandi Moreno", "gender": "female", "company": "MARQET", "email": "brandimoreno@marqet.com", "phone": "+1 (850) 434-2077", "address": "537 Doone Court, Waiohinu, Michigan, 3215", "about": "Irure proident adipisicing do Lorem do incididunt in laborum in eiusmod eiusmod ad elit proident. Eiusmod dolor ex magna magna occaecat. Nulla deserunt velit ex exercitation et irure sunt. Cupidatat ut excepteur ea quis labore sint cupidatat incididunt amet eu consectetur cillum ipsum proident. Occaecat exercitation aute laborum dolor proident reprehenderit laborum in voluptate culpa. Exercitation nulla adipisicing culpa aute est deserunt ea nisi deserunt consequat occaecat ut et non. Incididunt ex exercitation dolor dolor anim cillum dolore.", "registered": "2015-09-03T11:47:15 -02:00", "latitude": -19.768953, "longitude": 8.948458, "tags": ["laboris", "occaecat", "laborum", "laborum", "ex", "cillum", "occaecat"], "friends": [{"id": 0, "name": "Erna Kelly"}, {"id": 1, "name": "Black Mays"}, {"id": 2, "name": "Davis Buck"}], "greeting": "Hello, Brandi Moreno! You have 1 unread messages.", "favoriteFruit": "apple"}, {"_id": "655b6625870da431bcf5e0c2", "index": 2, "guid": "b17f6e3f-c898-4334-abbf-05cf222f143b", "isActive": False, "balance": "$1,493.77", "picture": "http://placehold.it/32x32", "age": 20, "eyeColor": "brown", "name": "Moody Meadows", "gender": "male", "company": "OPTIQUE", "email": "moodymeadows@optique.com", "phone": "+1 (993) 566-3041", "address": "766 Osborn Street, Bath, Maine, 7666", "about": "Non commodo excepteur nostrud qui adipisicing aliquip dolor minim nulla culpa proident. In ad cupidatat ea mollit ex est do deserunt proident nostrud. Cillum id id eiusmod amet exercitation nostrud cillum sunt deserunt dolore deserunt eiusmod mollit. Ut ex tempor ad laboris voluptate labore id officia fugiat exercitation amet.", "registered": "2015-01-16T02:48:28 -01:00", "latitude": -25.847327, "longitude": 63.95991, "tags": ["aute", "commodo", "adipisicing", "nostrud", "duis", "mollit", "ut"], "friends": [{"id": 0, "name": "Lacey Cash"}, {"id": 1, "name": "Gabrielle Harmon"}, {"id": 2, "name": "Ellis Lambert"}], "greeting": "Hello, Moody Meadows! You have 4 unread messages.", "favoriteFruit": "strawberry"}, {"_id": "655b6625f3e1bf422220854e", "index": 3, "guid": "92229883-2bfd-4974-a08c-1b506b372e46", "isActive": False, "balance": "$2,215.34", "picture": "http://placehold.it/32x32", "age": 22, "eyeColor": "brown", "name": "Heath Nguyen", "gender": "male", "company": "BLEENDOT", "email": "heathnguyen@bleendot.com", "phone": "+1 (989) 512-2797", "address": "135 Milton Street, Graniteville, Nebraska, 276", "about": "Consequat aliquip irure Lorem cupidatat nulla magna ullamco nulla voluptate adipisicing anim consectetur tempor aliquip. Magna aliqua nulla eu tempor esse proident. Proident fugiat ad ex Lorem reprehenderit dolor aliquip labore labore aliquip. Deserunt aute enim ea minim officia anim culpa sint commodo. Cillum consectetur excepteur aliqua exercitation Lorem veniam voluptate.", "registered": "2016-07-06T01:31:07 -02:00", "latitude": -60.997048, "longitude": -102.397885, "tags": ["do", "ad", "consequat", "irure", "tempor", "elit", "minim"], "friends": [{"id": 0, "name": "Walker Hernandez"}, {"id": 1, "name": "Maria Lane"}, {"id": 2, "name": "Mcknight Barron"}], "greeting": "Hello, Heath Nguyen! You have 4 unread messages.", "favoriteFruit": "apple"}, {"_id": "655b6625519a5b5e4b6742bf", "index": 4, "guid": "c5dc685f-6d0d-4173-b4cf-f5df29a1e8ef", "isActive": True, "balance": "$1,358.90", "picture": "http://placehold.it/32x32", "age": 33, "eyeColor": "brown", "name": "Deidre Duke", "gender": "female", "company": "OATFARM", "email": "deidreduke@oatfarm.com", "phone": "+1 (875) 587-3256", "address": "487 Schaefer Street, Wattsville, West Virginia, 4506", "about": "Laboris eu nulla esse magna sit eu deserunt non est aliqua exercitation commodo. Ad occaecat qui qui laborum dolore anim Lorem. Est qui occaecat irure enim deserunt enim aliqua ex deserunt incididunt esse. Quis in minim laboris proident non mollit. Magna ea do labore commodo. Et elit esse esse occaecat officia ipsum nisi.", "registered": "2021-09-12T04:17:08 -02:00", "latitude": 68.609781, "longitude": -87.509134, "tags": ["mollit", "cupidatat", "irure", "sit", "consequat", "anim", "fugiat"], "friends": [{"id": 0, "name": "Bean Paul"}, {"id": 1, "name": "Cochran Hubbard"}, {"id": 2, "name": "Rodgers Atkinson"}], "greeting": "Hello, Deidre Duke! You have 6 unread messages.", "favoriteFruit": "apple"}, {"_id": "655b6625a19b3f7e5f82f0ea", "index": 5, "guid": "75f3c264-baa1-47a0-b21c-4edac23d9935", "isActive": True, "balance": "$3,554.36", "picture": "http://placehold.it/32x32", "age": 26, "eyeColor": "blue", "name": "Lydia Holland", "gender": "female", "company": "ESCENTA", "email": "lydiaholland@escenta.com", "phone": "+1 (927) 482-3436", "address": "554 Rockaway Parkway, Kohatk, Montana, 6316", "about": "Consectetur ea est labore commodo laborum mollit pariatur non enim. Est dolore et non laboris tempor. Ea incididunt ut adipisicing cillum labore officia tempor eiusmod commodo. Cillum fugiat ex consectetur ut nostrud anim nostrud exercitation ut duis in ea. Eu et id fugiat est duis eiusmod ullamco quis officia minim sint ea nisi in.", "registered": "2018-03-13T01:48:56 -01:00", "latitude": -88.495799, "longitude": 71.840667, "tags": ["veniam", "minim", "consequat", "consequat", "incididunt", "consequat", "elit"], "friends": [{"id": 0, "name": "Debra Massey"}, {"id": 1, "name": "Weiss Savage"}, {"id": 2, "name": "Shannon Guerra"}], "greeting": "Hello, Lydia Holland! You have 5 unread messages.", "favoriteFruit": "banana"}]
236
+ assert from_file(filename=os.path.join(path,"invalid.json"), chunk_length=2) == [{"_id": "655b66256574f09bdae8abe8", "index": 0, "guid": "31082ae3-b0f3-4406-90f4-cc450bd4379d", "isActive": False, "balance": "$2,562.78", "picture": "http://placehold.it/32x32", "age": 32, "eyeColor": "brown", "name": "Glover Rivas", "gender": "male", "company": "EMPIRICA", "email": "gloverrivas@empirica.com", "phone": "+1 (842) 507-3063", "address": "536 Montague Terrace, Jenkinsville, Kentucky, 2235", "about": "Mollit consectetur excepteur voluptate tempor dolore ullamco enim irure ullamco non enim officia. Voluptate occaecat proident laboris ea Lorem cupidatat reprehenderit nisi nisi aliqua. Amet nulla ipsum deserunt excepteur amet ad aute aute ex. Et enim minim sit veniam est quis dolor nisi sunt quis eiusmod in. Amet eiusmod cillum sunt occaecat dolor laboris voluptate in eiusmod irure aliqua duis.", "registered": "2023-11-18T09:32:36 -01:00", "latitude": 36.26102, "longitude": -91.304608, "tags": ["non", "tempor", "do", "ullamco", "dolore", "sunt", "ipsum"], "friends": [{"id": 0, "name": "Cara Shepherd"}, {"id": 1, "name": "Mason Farley"}, {"id": 2, "name": "Harriet Cochran"}], "greeting": "Hello, Glover Rivas! You have 7 unread messages.", "favoriteFruit": "strawberry"}, {"_id": "655b662585364bc57278bb6f", "index": 1, "guid": "0dea7a3a-f812-4dde-b78d-7a9b58e5da05", "isActive": True, "balance": "$1,359.48", "picture": "http://placehold.it/32x32", "age": 38, "eyeColor": "brown", "name": "Brandi Moreno", "gender": "female", "company": "MARQET", "email": "brandimoreno@marqet.com", "phone": "+1 (850) 434-2077", "address": "537 Doone Court, Waiohinu, Michigan, 3215", "about": "Irure proident adipisicing do Lorem do incididunt in laborum in eiusmod eiusmod ad elit proident. Eiusmod dolor ex magna magna occaecat. Nulla deserunt velit ex exercitation et irure sunt. Cupidatat ut excepteur ea quis labore sint cupidatat incididunt amet eu consectetur cillum ipsum proident. Occaecat exercitation aute laborum dolor proident reprehenderit laborum in voluptate culpa. Exercitation nulla adipisicing culpa aute est deserunt ea nisi deserunt consequat occaecat ut et non. Incididunt ex exercitation dolor dolor anim cillum dolore.", "registered": "2015-09-03T11:47:15 -02:00", "latitude": -19.768953, "longitude": 8.948458, "tags": ["laboris", "occaecat", "laborum", "laborum", "ex", "cillum", "occaecat"], "friends": [{"id": 0, "name": "Erna Kelly"}, {"id": 1, "name": "Black Mays"}, {"id": 2, "name": "Davis Buck"}], "greeting": "Hello, Brandi Moreno! You have 1 unread messages.", "favoriteFruit": "apple"}, {"_id": "655b6625870da431bcf5e0c2", "index": 2, "guid": "b17f6e3f-c898-4334-abbf-05cf222f143b", "isActive": False, "balance": "$1,493.77", "picture": "http://placehold.it/32x32", "age": 20, "eyeColor": "brown", "name": "Moody Meadows", "gender": "male", "company": "OPTIQUE", "email": "moodymeadows@optique.com", "phone": "+1 (993) 566-3041", "address": "766 Osborn Street, Bath, Maine, 7666", "about": "Non commodo excepteur nostrud qui adipisicing aliquip dolor minim nulla culpa proident. In ad cupidatat ea mollit ex est do deserunt proident nostrud. Cillum id id eiusmod amet exercitation nostrud cillum sunt deserunt dolore deserunt eiusmod mollit. Ut ex tempor ad laboris voluptate labore id officia fugiat exercitation amet.", "registered": "2015-01-16T02:48:28 -01:00", "latitude": -25.847327, "longitude": 63.95991, "tags": ["aute", "commodo", "adipisicing", "nostrud", "duis", "mollit", "ut"], "friends": [{"id": 0, "name": "Lacey Cash"}, {"id": 1, "name": "Gabrielle Harmon"}, {"id": 2, "name": "Ellis Lambert"}], "greeting": "Hello, Moody Meadows! You have 4 unread messages.", "favoriteFruit": "strawberry"}, {"_id": "655b6625f3e1bf422220854e", "index": 3, "guid": "92229883-2bfd-4974-a08c-1b506b372e46", "isActive": False, "balance": "$2,215.34", "picture": "http://placehold.it/32x32", "age": 22, "eyeColor": "brown", "name": "Heath Nguyen", "gender": "male", "company": "BLEENDOT", "email": "heathnguyen@bleendot.com", "phone": "+1 (989) 512-2797", "address": "135 Milton Street, Graniteville, Nebraska, 276", "about": "Consequat aliquip irure Lorem cupidatat nulla magna ullamco nulla voluptate adipisicing anim consectetur tempor aliquip. Magna aliqua nulla eu tempor esse proident. Proident fugiat ad ex Lorem reprehenderit dolor aliquip labore labore aliquip. Deserunt aute enim ea minim officia anim culpa sint commodo. Cillum consectetur excepteur aliqua exercitation Lorem veniam voluptate.", "registered": "2016-07-06T01:31:07 -02:00", "latitude": -60.997048, "longitude": -102.397885, "tags": ["do", "ad", "consequat", "irure", "tempor", "elit", "minim"], "friends": [{"id": 0, "name": "Walker Hernandez"}, {"id": 1, "name": "Maria Lane"}, {"id": 2, "name": "Mcknight Barron"}], "greeting": "Hello, Heath Nguyen! You have 4 unread messages.", "favoriteFruit": "apple"}, {"_id": "655b6625519a5b5e4b6742bf", "index": 4, "guid": "c5dc685f-6d0d-4173-b4cf-f5df29a1e8ef", "isActive": True, "balance": "$1,358.90", "picture": "http://placehold.it/32x32", "age": 33, "eyeColor": "brown", "name": "Deidre Duke", "gender": "female", "company": "OATFARM", "email": "deidreduke@oatfarm.com", "phone": "+1 (875) 587-3256", "address": "487 Schaefer Street, Wattsville, West Virginia, 4506", "about": "Laboris eu nulla esse magna sit eu deserunt non est aliqua exercitation commodo. Ad occaecat qui qui laborum dolore anim Lorem. Est qui occaecat irure enim deserunt enim aliqua ex deserunt incididunt esse. Quis in minim laboris proident non mollit. Magna ea do labore commodo. Et elit esse esse occaecat officia ipsum nisi.", "registered": "2021-09-12T04:17:08 -02:00", "latitude": 68.609781, "longitude": -87.509134, "tags": ["mollit", "cupidatat", "irure", "sit", "consequat", "anim", "fugiat"], "friends": [{"id": 0, "name": "Bean Paul"}, {"id": 1, "name": "Cochran Hubbard"}, {"id": 2, "name": "Rodgers Atkinson"}], "greeting": "Hello, Deidre Duke! You have 6 unread messages.", "favoriteFruit": "apple"}, {"_id": "655b6625a19b3f7e5f82f0ea", "index": 5, "guid": "75f3c264-baa1-47a0-b21c-4edac23d9935", "isActive": True, "balance": "$3,554.36", "picture": "http://placehold.it/32x32", "age": 26, "eyeColor": "blue", "name": "Lydia Holland", "gender": "female", "company": "ESCENTA", "email": "lydiaholland@escenta.com", "phone": "+1 (927) 482-3436", "address": "554 Rockaway Parkway, Kohatk, Montana, 6316", "about": "Consectetur ea est labore commodo laborum mollit pariatur non enim. Est dolore et non laboris tempor. Ea incididunt ut adipisicing cillum labore officia tempor eiusmod commodo. Cillum fugiat ex consectetur ut nostrud anim nostrud exercitation ut duis in ea. Eu et id fugiat est duis eiusmod ullamco quis officia minim sint ea nisi in.", "registered": "2018-03-13T01:48:56 -01:00", "latitude": -88.495799, "longitude": 71.840667, "tags": ["veniam", "minim", "consequat", "consequat", "incididunt", "consequat", "elit"], "friends": [{"id": 0, "name": "Debra Massey"}, {"id": 1, "name": "Weiss Savage"}, {"id": 2, "name": "Shannon Guerra"}], "greeting": "Hello, Lydia Holland! You have 5 unread messages.", "favoriteFruit": "banana"}]
237
+
238
+
239
+ # Create a temporary file
240
+ temp_fd, temp_path = tempfile.mkstemp(suffix=".json")
241
+ try:
242
+ # Write content to the temporary file
243
+ with os.fdopen(temp_fd, 'w') as tmp:
244
+ tmp.write("{key:value}")
245
+ assert from_file(filename=temp_path, logging=True) == ({'key': 'value'}, [{'text': 'While parsing a string, we found a literal instead of a quote', 'context': '{key:value}'}, {'text': 'While parsing a string, we found no starting quote. Will add the quote back', 'context': '{key:value}'}, {'context': '{key:value}', 'text': 'While parsing a string missing the left delimiter in object key context, we found a :, stopping here',}, {'text': 'While parsing a string, we missed the closing quote, ignoring', 'context': '{key:value}'}, {'text': 'While parsing a string, we found a literal instead of a quote', 'context': '{key:value}'}, {'text': 'While parsing a string, we found no starting quote. Will add the quote back', 'context': '{key:value}'}, {'context': '{key:value}', 'text': 'While parsing a string missing the left delimiter in object value context, we found a , or } and we couldn\'t determine that a right delimiter was present. Stopping here'}, {'text': 'While parsing a string, we missed the closing quote, ignoring', 'context': '{key:value}'}])
246
+ assert from_file(filename=temp_path, logging=True, chunk_length=2) == ({'key': 'value'}, [{'text': 'While parsing a string, we found a literal instead of a quote', 'context': '{key:value}'}, {'text': 'While parsing a string, we found no starting quote. Will add the quote back', 'context': '{key:value}'}, {'context': '{key:value}', 'text': 'While parsing a string missing the left delimiter in object key context, we found a :, stopping here',}, {'text': 'While parsing a string, we missed the closing quote, ignoring', 'context': '{key:value}'}, {'text': 'While parsing a string, we found a literal instead of a quote', 'context': '{key:value}'}, {'text': 'While parsing a string, we found no starting quote. Will add the quote back', 'context': '{key:value}'}, {'context': '{key:value}', 'text': 'While parsing a string missing the left delimiter in object value context, we found a , or } and we couldn\'t determine that a right delimiter was present. Stopping here'}, {'text': 'While parsing a string, we missed the closing quote, ignoring', 'context': '{key:value}'}])
247
+ finally:
248
+ # Clean up - delete the temporary file
249
+ os.remove(temp_path)
250
+
251
+ # Create a temporary file
252
+ temp_fd, temp_path = tempfile.mkstemp(suffix=".json")
253
+ try:
254
+ # Write content to the temporary file
255
+ with os.fdopen(temp_fd, 'w') as tmp:
256
+ tmp.write('x' * 5 * 1024 * 1024) # 5 MB
257
+ assert from_file(filename=temp_path, logging=True) == ('', [])
258
+
259
+ finally:
260
+ # Clean up - delete the temporary file
261
+ os.remove(temp_path)
262
+
263
+
264
+ def test_ensure_ascii():
265
+ assert repair_json("{'test_中国人_ascii':'统一码'}", ensure_ascii=False) == '{"test_中国人_ascii": "统一码"}'
@@ -1,247 +0,0 @@
1
- from src.json_repair.json_repair import from_file, repair_json, loads
2
-
3
- def test_basic_types_valid():
4
- assert repair_json("True", return_objects=True) == ""
5
- assert repair_json("False", return_objects=True) == ""
6
- assert repair_json("Null", return_objects=True) == ""
7
- assert repair_json("1", return_objects=True) == 1
8
- assert repair_json("[]", return_objects=True) == []
9
- assert repair_json("[1, 2, 3, 4]", return_objects=True) == [1, 2, 3, 4]
10
- assert repair_json("{}", return_objects=True) == {}
11
- assert repair_json('{ "key": "value", "key2": 1, "key3": True }', return_objects=True) == { "key": "value", "key2": 1, "key3": True }
12
-
13
- def test_basic_types_invalid():
14
- assert repair_json("true", return_objects=True) == True
15
- assert repair_json("false", return_objects=True) == False
16
- assert repair_json("null", return_objects=True) == None
17
- assert repair_json("1.2", return_objects=True) == 1.2
18
- assert repair_json("[", return_objects=True) == []
19
- assert repair_json("[1, 2, 3, 4", return_objects=True) == [1, 2, 3, 4]
20
- assert repair_json("{", return_objects=True) == {}
21
- assert repair_json('{ "key": value, "key2": 1 "key3": null }', return_objects=True) == { "key": "value", "key2": 1, "key3": None }
22
-
23
- def test_valid_json():
24
- assert (
25
- repair_json('{"name": "John", "age": 30, "city": "New York"}')
26
- == '{"name": "John", "age": 30, "city": "New York"}'
27
- )
28
- assert (
29
- repair_json('{"employees":["John", "Anna", "Peter"]} ')
30
- == '{"employees": ["John", "Anna", "Peter"]}'
31
- )
32
- assert repair_json('{"key": "value:value"}') == '{"key": "value:value"}'
33
- assert (
34
- repair_json('{"text": "The quick brown fox,"}')
35
- == '{"text": "The quick brown fox,"}'
36
- )
37
- assert (
38
- repair_json('{"text": "The quick brown fox won\'t jump"}')
39
- == '{"text": "The quick brown fox won\'t jump"}'
40
- )
41
- assert repair_json('{"key": ""') == '{"key": ""}'
42
- assert (
43
- repair_json('{"key1": {"key2": [1, 2, 3]}}') == '{"key1": {"key2": [1, 2, 3]}}'
44
- )
45
- assert (
46
- repair_json('{"key": 12345678901234567890}') == '{"key": 12345678901234567890}'
47
- )
48
- assert repair_json('{"key": "value\u263A"}') == '{"key": "value\\u263a"}'
49
- assert repair_json('{"key": "value\\nvalue"}') == '{"key": "value\\nvalue"}'
50
-
51
- def test_brackets_edge_cases():
52
- assert repair_json("[{]") == "[{}]"
53
- assert repair_json(" { } ") == "{}"
54
- assert repair_json("[") == "[]"
55
- assert repair_json("]") == '""'
56
- assert repair_json("{") == "{}"
57
- assert repair_json("}") == '""'
58
- assert repair_json('{"') == '{}'
59
- assert repair_json('["') == '[]'
60
- assert repair_json('{foo: [}') == '{"foo": []}'
61
-
62
- def test_general_edge_cases():
63
- assert repair_json("\"") == '""'
64
- assert repair_json("\n") == '""'
65
- assert repair_json(" ") == '""'
66
- assert repair_json("[[1\n\n]") == "[[1]]"
67
- assert repair_json("string") == '""'
68
- assert repair_json("stringbeforeobject {}") == '{}'
69
-
70
- def test_mixed_data_types():
71
- assert repair_json(' {"key": true, "key2": false, "key3": null}') == '{"key": true, "key2": false, "key3": null}'
72
- assert repair_json('{"key": TRUE, "key2": FALSE, "key3": Null} ') == '{"key": true, "key2": false, "key3": null}'
73
-
74
- def test_missing_and_mixed_quotes():
75
- assert repair_json("{'key': 'string', 'key2': false, \"key3\": null, \"key4\": unquoted}") == '{"key": "string", "key2": false, "key3": null, "key4": "unquoted"}'
76
- assert (
77
- repair_json('{"name": "John", "age": 30, "city": "New York')
78
- == '{"name": "John", "age": 30, "city": "New York"}'
79
- )
80
- assert (
81
- repair_json('{"name": "John", "age": 30, city: "New York"}')
82
- == '{"name": "John", "age": 30, "city": "New York"}'
83
- )
84
- assert (
85
- repair_json('{"name": "John", "age": 30, "city": New York}')
86
- == '{"name": "John", "age": 30, "city": "New York"}'
87
- )
88
- assert (
89
- repair_json('{"name": John, "age": 30, "city": "New York"}')
90
- == '{"name": "John", "age": 30, "city": "New York"}'
91
- )
92
- assert repair_json('{“slanted_delimiter”: "value"}') == '{"slanted_delimiter": "value"}'
93
- assert (
94
- repair_json('{"name": "John", "age": 30, "city": "New')
95
- == '{"name": "John", "age": 30, "city": "New"}'
96
- )
97
- assert repair_json('[{"key": "value", COMMENT "notes": "lorem "ipsum", sic." }]') == '[{"key": "value", "notes": "lorem \\"ipsum\\", sic."}]'
98
- assert repair_json('{"key": ""value"}') == '{"key": "value"}'
99
- assert repair_json('{"key": "value", 5: "value"}') == '{"key": "value", "5": "value"}'
100
- assert repair_json('{"foo": "\\"bar\\""') == '{"foo": "\\"bar\\""}'
101
- assert repair_json('{"" key":"val"') == '{" key": "val"}'
102
- assert repair_json('{"key": value "key2" : "value2" ') == '{"key": "value", "key2": "value2"}'
103
-
104
- def test_array_edge_cases():
105
- assert repair_json("[1, 2, 3,") == "[1, 2, 3]"
106
- assert repair_json("[1, 2, 3, ...]") == "[1, 2, 3]"
107
- assert repair_json("[1, 2, ... , 3]") == "[1, 2, 3]"
108
- assert repair_json("[1, 2, '...', 3]") == '[1, 2, "...", 3]'
109
- assert repair_json("[true, false, null, ...]") == '[true, false, null]'
110
- assert repair_json('["a" "b" "c" 1') == '["a", "b", "c", 1]'
111
- assert repair_json('{"employees":["John", "Anna",') == '{"employees": ["John", "Anna"]}'
112
- assert repair_json('{"employees":["John", "Anna", "Peter') == '{"employees": ["John", "Anna", "Peter"]}'
113
- assert repair_json('{"key1": {"key2": [1, 2, 3') == '{"key1": {"key2": [1, 2, 3]}}'
114
-
115
- def test_escaping():
116
- assert repair_json("'\"'") == '""'
117
- assert repair_json("{\"key\": 'string\"\n\t\le'") == '{"key": "string\\"\\n\\tle"}'
118
- assert repair_json(r'{"real_content": "Some string: Some other string \t Some string <a href=\"https://domain.com\">Some link</a>"') == r'{"real_content": "Some string: Some other string \t Some string <a href=\"https://domain.com\">Some link</a>"}'
119
- assert repair_json('{"key_1\n": "value"}') == '{"key_1": "value"}'
120
- assert repair_json('{"key\t_": "value"}') == '{"key\\t_": "value"}'
121
-
122
-
123
- def test_object_edge_cases():
124
- assert repair_json('{ ') == '{}'
125
- assert repair_json('{"": "value"') == '{"": "value"}'
126
- assert repair_json('{"value_1": true, COMMENT "value_2": "data"}') == '{"value_1": true, "value_2": "data"}'
127
- assert repair_json('{"value_1": true, SHOULD_NOT_EXIST "value_2": "data" AAAA }') == '{"value_1": true, "value_2": "data"}'
128
- assert repair_json('{"" : true, "key2": "value2"}') == '{"": true, "key2": "value2"}'
129
- assert repair_json('{""answer"":[{""traits"":''Female aged 60+'',""answer1"":""5""}]}') == '{"answer": [{"traits": "Female aged 60+", "answer1": "5"}]}'
130
- assert repair_json('{ "words": abcdef", "numbers": 12345", "words2": ghijkl" }') == '{"words": "abcdef", "numbers": 12345, "words2": "ghijkl"}'
131
- assert repair_json('''{"number": 1,"reason": "According...""ans": "YES"}''') == '{"number": 1, "reason": "According...", "ans": "YES"}'
132
- assert repair_json('''{ "a" : "{ b": {} }" }''') == '{"a": "{ b"}'
133
- assert repair_json("""{"b": "xxxxx" true}""") == '{"b": "xxxxx"}'
134
- assert repair_json('{"key": "Lorem "ipsum" s,"}') == '{"key": "Lorem \\"ipsum\\" s,"}'
135
- assert repair_json('{"lorem": ipsum, sic, datum.",}') == '{"lorem": "ipsum, sic, datum."}'
136
- assert repair_json('{"lorem": sic tamet. "ipsum": sic tamet, quick brown fox. "sic": ipsum}') == '{"lorem": "sic tamet.", "ipsum": "sic tamet", "sic": "ipsum"}'
137
- assert repair_json('{"key":value, " key2":"value2" }') == '{"key": "value", " key2": "value2"}'
138
- assert repair_json('{"key":value "key2":"value2" }') == '{"key": "value", "key2": "value2"}'
139
-
140
- def test_number_edge_cases():
141
- assert repair_json(' - { "test_key": ["test_value", "test_value2"] }') == '{"test_key": ["test_value", "test_value2"]}'
142
- assert repair_json('{"key": 1/3}') == '{"key": "1/3"}'
143
- assert repair_json('{"key": .25}') == '{"key": 0.25}'
144
- assert repair_json('{"here": "now", "key": 1/3, "foo": "bar"}') == '{"here": "now", "key": "1/3", "foo": "bar"}'
145
- assert repair_json('{"key": 12345/67890}') == '{"key": "12345/67890"}'
146
- assert repair_json('[105,12') == '[105, 12]'
147
- assert repair_json('{"key", 105,12,') == '{"key": "105,12"}'
148
- assert repair_json('{"key": 1/3, "foo": "bar"}') == '{"key": "1/3", "foo": "bar"}'
149
- assert repair_json('{"key": 10-20}') == '{"key": "10-20"}'
150
- assert repair_json('{"key": 1.1.1}') == '{"key": "1.1.1"}'
151
- assert repair_json('[- ') == '[]'
152
-
153
- def test_markdown():
154
- assert repair_json('{ "content": "[LINK]("https://google.com")" }') == '{"content": "[LINK](\\"https://google.com\\")"}'
155
- assert repair_json('{ "content": "[LINK](" }') == '{"content": "[LINK]("}'
156
- assert repair_json('{ "content": "[LINK](", "key": true }') == '{"content": "[LINK](", "key": true}'
157
-
158
- def test_leading_trailing_characters():
159
- assert repair_json('````{ "key": "value" }```') == '{"key": "value"}'
160
- assert repair_json("""{ "a": "", "b": [ { "c": 1} ] \n}```""") == '{"a": "", "b": [{"c": 1}]}'
161
- assert repair_json("Based on the information extracted, here is the filled JSON output: ```json { 'a': 'b' } ```") == '{"a": "b"}'
162
- assert repair_json("""
163
- The next 64 elements are:
164
- ```json
165
- { "key": "value" }
166
- ```""") == '{"key": "value"}'
167
- def test_multiple_jsons():
168
- assert repair_json("[]{}") == "[[], {}]"
169
- assert repair_json("{}[]{}") == "[{}, [], {}]"
170
- assert repair_json('{"key":"value"}[1,2,3,True]') == '[{"key": "value"}, [1, 2, 3, true]]'
171
- assert repair_json('lorem ```json {"key":"value"} ``` ipsum ```json [1,2,3,True] ``` 42') == '[{"key": "value"}, [1, 2, 3, true]]'
172
-
173
- def test_repair_json_with_objects():
174
- # Test with valid JSON strings
175
- assert repair_json("[]", return_objects=True) == []
176
- assert repair_json("{}", return_objects=True) == {}
177
- assert repair_json('{"key": true, "key2": false, "key3": null}', return_objects=True) == {"key": True, "key2": False, "key3": None}
178
- assert repair_json('{"name": "John", "age": 30, "city": "New York"}', return_objects=True) == {
179
- "name": "John",
180
- "age": 30,
181
- "city": "New York",
182
- }
183
- assert repair_json("[1, 2, 3, 4]", return_objects=True) == [1, 2, 3, 4]
184
- assert repair_json('{"employees":["John", "Anna", "Peter"]} ', return_objects=True) == {
185
- "employees": ["John", "Anna", "Peter"]
186
- }
187
- assert repair_json('''
188
- {
189
- "resourceType": "Bundle",
190
- "id": "1",
191
- "type": "collection",
192
- "entry": [
193
- {
194
- "resource": {
195
- "resourceType": "Patient",
196
- "id": "1",
197
- "name": [
198
- {"use": "official", "family": "Corwin", "given": ["Keisha", "Sunny"], "prefix": ["Mrs."},
199
- {"use": "maiden", "family": "Goodwin", "given": ["Keisha", "Sunny"], "prefix": ["Mrs."]}
200
- ]
201
- }
202
- }
203
- ]
204
- }
205
- ''', return_objects=True) == {"resourceType": "Bundle", "id": "1", "type": "collection", "entry": [{"resource": {"resourceType": "Patient", "id": "1", "name": [{"use": "official", "family": "Corwin", "given": ["Keisha", "Sunny"], "prefix": ["Mrs."]}, {"use": "maiden", "family": "Goodwin", "given": ["Keisha", "Sunny"], "prefix": ["Mrs."]}]}}]}
206
- assert repair_json('{\n"html": "<h3 id="aaa">Waarom meer dan 200 Technical Experts - "Passie voor techniek"?</h3>"}', return_objects=True) == {'html': '<h3 id="aaa">Waarom meer dan 200 Technical Experts - "Passie voor techniek"?</h3>'}
207
- assert repair_json("""
208
- [
209
- {
210
- "foo": "Foo bar baz",
211
- "tag": "#foo-bar-baz"
212
- },
213
- {
214
- "foo": "foo bar "foobar" foo bar baz.",
215
- "tag": "#foo-bar-foobar"
216
- }
217
- ]
218
- """, return_objects=True) == [{"foo": "Foo bar baz", "tag": "#foo-bar-baz"},{"foo": "foo bar \"foobar\" foo bar baz.", "tag": "#foo-bar-foobar" }]
219
-
220
- def test_repair_json_skip_json_loads():
221
- assert repair_json('{"key": true, "key2": false, "key3": null}', skip_json_loads=True) == '{"key": true, "key2": false, "key3": null}'
222
- assert repair_json('{"key": true, "key2": false, "key3": null}', return_objects=True, skip_json_loads=True) == {"key": True, "key2": False, "key3": None}
223
- assert repair_json('{"key": true, "key2": false, "key3": }', skip_json_loads=True) == '{"key": true, "key2": false, "key3": ""}'
224
- assert loads('{"key": true, "key2": false, "key3": }', skip_json_loads=True) == {"key": True, "key2": False, "key3": ""}
225
-
226
-
227
- def test_repair_json_from_file():
228
- import os.path
229
- import pathlib
230
- path = pathlib.Path(__file__).parent.resolve()
231
-
232
- assert from_file(os.path.join(path,"invalid.json")) == [{"_id": "655b66256574f09bdae8abe8", "index": 0, "guid": "31082ae3-b0f3-4406-90f4-cc450bd4379d", "isActive": False, "balance": "$2,562.78", "picture": "http://placehold.it/32x32", "age": 32, "eyeColor": "brown", "name": "Glover Rivas", "gender": "male", "company": "EMPIRICA", "email": "gloverrivas@empirica.com", "phone": "+1 (842) 507-3063", "address": "536 Montague Terrace, Jenkinsville, Kentucky, 2235", "about": "Mollit consectetur excepteur voluptate tempor dolore ullamco enim irure ullamco non enim officia. Voluptate occaecat proident laboris ea Lorem cupidatat reprehenderit nisi nisi aliqua. Amet nulla ipsum deserunt excepteur amet ad aute aute ex. Et enim minim sit veniam est quis dolor nisi sunt quis eiusmod in. Amet eiusmod cillum sunt occaecat dolor laboris voluptate in eiusmod irure aliqua duis.", "registered": "2023-11-18T09:32:36 -01:00", "latitude": 36.26102, "longitude": -91.304608, "tags": ["non", "tempor", "do", "ullamco", "dolore", "sunt", "ipsum"], "friends": [{"id": 0, "name": "Cara Shepherd"}, {"id": 1, "name": "Mason Farley"}, {"id": 2, "name": "Harriet Cochran"}], "greeting": "Hello, Glover Rivas! You have 7 unread messages.", "favoriteFruit": "strawberry"}, {"_id": "655b662585364bc57278bb6f", "index": 1, "guid": "0dea7a3a-f812-4dde-b78d-7a9b58e5da05", "isActive": True, "balance": "$1,359.48", "picture": "http://placehold.it/32x32", "age": 38, "eyeColor": "brown", "name": "Brandi Moreno", "gender": "female", "company": "MARQET", "email": "brandimoreno@marqet.com", "phone": "+1 (850) 434-2077", "address": "537 Doone Court, Waiohinu, Michigan, 3215", "about": "Irure proident adipisicing do Lorem do incididunt in laborum in eiusmod eiusmod ad elit proident. Eiusmod dolor ex magna magna occaecat. Nulla deserunt velit ex exercitation et irure sunt. Cupidatat ut excepteur ea quis labore sint cupidatat incididunt amet eu consectetur cillum ipsum proident. Occaecat exercitation aute laborum dolor proident reprehenderit laborum in voluptate culpa. Exercitation nulla adipisicing culpa aute est deserunt ea nisi deserunt consequat occaecat ut et non. Incididunt ex exercitation dolor dolor anim cillum dolore.", "registered": "2015-09-03T11:47:15 -02:00", "latitude": -19.768953, "longitude": 8.948458, "tags": ["laboris", "occaecat", "laborum", "laborum", "ex", "cillum", "occaecat"], "friends": [{"id": 0, "name": "Erna Kelly"}, {"id": 1, "name": "Black Mays"}, {"id": 2, "name": "Davis Buck"}], "greeting": "Hello, Brandi Moreno! You have 1 unread messages.", "favoriteFruit": "apple"}, {"_id": "655b6625870da431bcf5e0c2", "index": 2, "guid": "b17f6e3f-c898-4334-abbf-05cf222f143b", "isActive": False, "balance": "$1,493.77", "picture": "http://placehold.it/32x32", "age": 20, "eyeColor": "brown", "name": "Moody Meadows", "gender": "male", "company": "OPTIQUE", "email": "moodymeadows@optique.com", "phone": "+1 (993) 566-3041", "address": "766 Osborn Street, Bath, Maine, 7666", "about": "Non commodo excepteur nostrud qui adipisicing aliquip dolor minim nulla culpa proident. In ad cupidatat ea mollit ex est do deserunt proident nostrud. Cillum id id eiusmod amet exercitation nostrud cillum sunt deserunt dolore deserunt eiusmod mollit. Ut ex tempor ad laboris voluptate labore id officia fugiat exercitation amet.", "registered": "2015-01-16T02:48:28 -01:00", "latitude": -25.847327, "longitude": 63.95991, "tags": ["aute", "commodo", "adipisicing", "nostrud", "duis", "mollit", "ut"], "friends": [{"id": 0, "name": "Lacey Cash"}, {"id": 1, "name": "Gabrielle Harmon"}, {"id": 2, "name": "Ellis Lambert"}], "greeting": "Hello, Moody Meadows! You have 4 unread messages.", "favoriteFruit": "strawberry"}, {"_id": "655b6625f3e1bf422220854e", "index": 3, "guid": "92229883-2bfd-4974-a08c-1b506b372e46", "isActive": False, "balance": "$2,215.34", "picture": "http://placehold.it/32x32", "age": 22, "eyeColor": "brown", "name": "Heath Nguyen", "gender": "male", "company": "BLEENDOT", "email": "heathnguyen@bleendot.com", "phone": "+1 (989) 512-2797", "address": "135 Milton Street, Graniteville, Nebraska, 276", "about": "Consequat aliquip irure Lorem cupidatat nulla magna ullamco nulla voluptate adipisicing anim consectetur tempor aliquip. Magna aliqua nulla eu tempor esse proident. Proident fugiat ad ex Lorem reprehenderit dolor aliquip labore labore aliquip. Deserunt aute enim ea minim officia anim culpa sint commodo. Cillum consectetur excepteur aliqua exercitation Lorem veniam voluptate.", "registered": "2016-07-06T01:31:07 -02:00", "latitude": -60.997048, "longitude": -102.397885, "tags": ["do", "ad", "consequat", "irure", "tempor", "elit", "minim"], "friends": [{"id": 0, "name": "Walker Hernandez"}, {"id": 1, "name": "Maria Lane"}, {"id": 2, "name": "Mcknight Barron"}], "greeting": "Hello, Heath Nguyen! You have 4 unread messages.", "favoriteFruit": "apple"}, {"_id": "655b6625519a5b5e4b6742bf", "index": 4, "guid": "c5dc685f-6d0d-4173-b4cf-f5df29a1e8ef", "isActive": True, "balance": "$1,358.90", "picture": "http://placehold.it/32x32", "age": 33, "eyeColor": "brown", "name": "Deidre Duke", "gender": "female", "company": "OATFARM", "email": "deidreduke@oatfarm.com", "phone": "+1 (875) 587-3256", "address": "487 Schaefer Street, Wattsville, West Virginia, 4506", "about": "Laboris eu nulla esse magna sit eu deserunt non est aliqua exercitation commodo. Ad occaecat qui qui laborum dolore anim Lorem. Est qui occaecat irure enim deserunt enim aliqua ex deserunt incididunt esse. Quis in minim laboris proident non mollit. Magna ea do labore commodo. Et elit esse esse occaecat officia ipsum nisi.", "registered": "2021-09-12T04:17:08 -02:00", "latitude": 68.609781, "longitude": -87.509134, "tags": ["mollit", "cupidatat", "irure", "sit", "consequat", "anim", "fugiat"], "friends": [{"id": 0, "name": "Bean Paul"}, {"id": 1, "name": "Cochran Hubbard"}, {"id": 2, "name": "Rodgers Atkinson"}], "greeting": "Hello, Deidre Duke! You have 6 unread messages.", "favoriteFruit": "apple"}, {"_id": "655b6625a19b3f7e5f82f0ea", "index": 5, "guid": "75f3c264-baa1-47a0-b21c-4edac23d9935", "isActive": True, "balance": "$3,554.36", "picture": "http://placehold.it/32x32", "age": 26, "eyeColor": "blue", "name": "Lydia Holland", "gender": "female", "company": "ESCENTA", "email": "lydiaholland@escenta.com", "phone": "+1 (927) 482-3436", "address": "554 Rockaway Parkway, Kohatk, Montana, 6316", "about": "Consectetur ea est labore commodo laborum mollit pariatur non enim. Est dolore et non laboris tempor. Ea incididunt ut adipisicing cillum labore officia tempor eiusmod commodo. Cillum fugiat ex consectetur ut nostrud anim nostrud exercitation ut duis in ea. Eu et id fugiat est duis eiusmod ullamco quis officia minim sint ea nisi in.", "registered": "2018-03-13T01:48:56 -01:00", "latitude": -88.495799, "longitude": 71.840667, "tags": ["veniam", "minim", "consequat", "consequat", "incididunt", "consequat", "elit"], "friends": [{"id": 0, "name": "Debra Massey"}, {"id": 1, "name": "Weiss Savage"}, {"id": 2, "name": "Shannon Guerra"}], "greeting": "Hello, Lydia Holland! You have 5 unread messages.", "favoriteFruit": "banana"}]
233
-
234
- import tempfile
235
- # Create a temporary file
236
- temp_fd, temp_path = tempfile.mkstemp(suffix=".json")
237
- try:
238
- # Write content to the temporary file
239
- with os.fdopen(temp_fd, 'w') as tmp:
240
- tmp.write("{key:value}")
241
- assert from_file(temp_path, logging=True) == ({'key': 'value'}, [{'text': 'While parsing a string, we found a literal instead of a quote', 'context': '{key:value}'}, {'text': 'While parsing a string, we found no starting quote. Will add the quote back', 'context': '{key:value}'}, {'context': '{key:value}', 'text': 'While parsing a string missing the left delimiter in object key context, we found a :, stopping here',}, {'text': 'While parsing a string, we missed the closing quote, ignoring', 'context': '{key:value}'}, {'text': 'While parsing a string, we found a literal instead of a quote', 'context': '{key:value}'}, {'text': 'While parsing a string, we found no starting quote. Will add the quote back', 'context': '{key:value}'}, {'context': '{key:value}', 'text': 'While parsing a string missing the left delimiter in object value context, we found a , or } and we couldn\'t determine that a right delimiter was present. Stopping here'}, {'text': 'While parsing a string, we missed the closing quote, ignoring', 'context': '{key:value}'}])
242
- finally:
243
- # Clean up - delete the temporary file
244
- os.remove(temp_path)
245
-
246
- def test_ensure_ascii():
247
- assert repair_json("{'test_中国人_ascii':'统一码'}", ensure_ascii=False) == '{"test_中国人_ascii": "统一码"}'
File without changes
File without changes