Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix strict argument added #16

Merged
merged 4 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions half_json/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ def __init__(self, max_try=20, max_stack=3, js_style=False):
self._max_stack = max_stack
self._js_style = js_style

def fix(self, line):
def fix(self, line, strict=True):
try:
json.loads(line)
json.loads(line, strict=strict)
return FixResult(success=True, line=line, origin=True)
except Exception as e:
except Exception:
pass

ok, new_line = self.fixwithtry(line)
ok, new_line = self.fixwithtry(line, strict=strict)
return FixResult(success=ok, line=new_line, origin=False)

def fixwithtry(self, line):
def fixwithtry(self, line, strict=True):
if self._max_try <= 0:
return False, line

Expand All @@ -35,7 +35,7 @@ def fixwithtry(self, line):

for i in range(self._max_try):

ok, new_line = self.patch_line(line)
ok, new_line = self.patch_line(line, strict=strict)
if ok:
return ok, new_line

Expand All @@ -47,8 +47,8 @@ def fixwithtry(self, line):
line = new_line
return ok, line

def patch_line(self, line):
result = decode_line(line)
def patch_line(self, line, strict=True):
result = decode_line(line, strict=strict)
if result.success:
return True, line

Expand Down
9 changes: 5 additions & 4 deletions half_json/json_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ def new_parser(*args, **kwargs):
return new_parser


def make_decoder():
def make_decoder(strict=True):
json.decoder.scanstring = record_parser_name(py_scanstring)

decoder = JSONDecoder()
decoder = JSONDecoder(strict=strict)
decoder.parse_object = record_parser_name(decoder.parse_object)
decoder.parse_array = record_parser_name(decoder.parse_array)
decoder.parse_string = record_parser_name(py_scanstring)
Expand All @@ -110,14 +110,15 @@ def make_decoder():


decoder = make_decoder()
decoder_unstrict = make_decoder(strict=False)


DecodeResult = namedtuple('DecodeResult', ['success', 'exception', 'err_info'])


def decode_line(line):
def decode_line(line, strict=True):
try:
obj, end = decoder.scan_once(line, 0)
obj, end = (decoder if strict else decoder_unstrict).scan_once(line, 0)
ok = end == len(line)
return DecodeResult(success=ok, exception=None, err_info=(obj, end))
except StopIteration as e:
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

[project]
name = "jsonfixer"
version = "0.2.0"
version = "0.2.1"
description = "jsonfixer: fix invalid json: broken-json / truncated-json."
authors = [
{name = "alingse", email = "[email protected]"},
]
dependencies = []
requires-python = ">=3.10"
requires-python = ">=3.8"
readme = "README.md"
license = {text = "MIT"}
classifiers = [
Expand Down
16 changes: 16 additions & 0 deletions tests/test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,19 @@ def test_array_miss_value_2(self):
ok, newline, _ = JSONFixer().fix(line)
self.assertTrue(ok)
self.assertEqual('[null]', newline)

def test_unstrict_ok(self):
line = '{"hello": "wor\nld"}'
ok, newline, _ = JSONFixer().fix(line)
self.assertFalse(ok)
ok, newline, _ = JSONFixer().fix(line, strict=False)
self.assertTrue(ok)
self.assertEqual(line, newline)

def test_unstrict_fix(self):
line = '{"hello": "wor\nld"'
ok, newline, _ = JSONFixer().fix(line)
self.assertFalse(ok)
ok, newline, _ = JSONFixer().fix(line, strict=False)
self.assertTrue(ok)
self.assertEqual('{"hello": "wor\nld"}', newline)