Skip to content

Commit 5fd4d67

Browse files
authored
Merge pull request #10 from half-pie/patch-half-parse
增加 处理 half-parse 为 object 的情况,以及做一些预测
2 parents 9aff56a + 6554dd0 commit 5fd4d67

File tree

6 files changed

+56
-11
lines changed

6 files changed

+56
-11
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ seq 1 10|xargs -I {} ./runtest.sh|grep ratio: |awk '{t += $3; h+= $6}{print h/t}
3737
7. 0.60 , 0.58 # 去掉了空行
3838
8. 0.6971, 0.6969, 0.6984 # 增加处理 StopIteration
3939
9. 0.7428, 0.7383, 0.7427 # 增加处理 half parse
40+
10. 0.7617,0.7631, 0.7558 # 细化处理 half parse
4041

4142
## 目前的缺点 && 发现
4243

@@ -46,6 +47,9 @@ seq 1 10|xargs -I {} ./runtest.sh|grep ratio: |awk '{t += $3; h+= $6}{print h/t}
4647
4. 还不支持回溯 --> [{]
4748
5. 同一个 case, 处理空白的情况
4849
6. 也许可以统计 [] {} "" 的配合情况
50+
7. 突然想到, 应该反思一下, 这个是一个fixer, 而不是一个将任何字符串都转为 json 的东西
51+
应该明确 JSONFixer 的能力范围, 对 runratio.sh 也应该比较前后两个的 json 相似程度。
52+
(听起来像无能者的辩白?)
4953

5054
## TODO
5155

@@ -54,5 +58,4 @@ seq 1 10|xargs -I {} ./runtest.sh|grep ratio: |awk '{t += $3; h+= $6}{print h/t}
5458

5559
## BadCase
5660

57-
1. {}}
58-
2.
61+
1. 1, [""], -1]

checks/debug.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# coding=utf8
2-
import sys
3-
42
from half_json.main import fixjson
53

4+
65
if __name__ == '__main__':
7-
if len(sys.argv) >= 4:
8-
import pdb
9-
pdb.set_trace()
6+
import pdb
7+
pdb.set_trace()
108
fixjson()

checks/oneline.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[]]
1+
"a":

half_json/core.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def patch_value_error(self, line, err_info):
8080
if lastchar == "," and nextchar == "}":
8181
return False, remove_line(line, pos - 1, pos)
8282
# {[ or {"a":1,[ --> "":[
83-
if nextchar == "[":
83+
if nextchar in "[{":
8484
return False, insert_line(line, "\"\":", pos)
8585
# dosomething
8686
# if lastchar == "{":
@@ -151,8 +151,16 @@ def patch_stop_iteration(self, line):
151151

152152
def patch_half_parse(self, line, err_info):
153153
obj, end = err_info
154-
nextline = line[end:]
154+
nextline = line[end:].strip()
155+
nextchar = nextline[:1]
155156
left = patch_left_object_and_array(nextline)
157+
# ??
158+
if left == "":
159+
if nextchar == ",":
160+
left = "["
161+
elif nextchar == ":" and isinstance(obj, basestring):
162+
left = "{"
163+
156164
new_line = left + line[:end] + nextline
157165
return False, new_line
158166

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
setup(
1515
name='jsonfixer',
16-
version='0.1.4a',
16+
version='0.1.4b',
1717
url='https://github.com/half-pie/half-json',
1818
description='jsonfixer: fix invalid json: broken-json / truncated-json.',
1919
long_description=readme,

tests/test_stop.py

+36
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,39 @@ def test_patch_half_array(self):
2424
ok, newline, _ = JSONFixer().fix(line)
2525
self.assertTrue(ok)
2626
self.assertEqual('[[]]', newline)
27+
28+
def test_patch_half_object(self):
29+
line = '{}}'
30+
ok, newline, _ = JSONFixer().fix(line)
31+
self.assertTrue(ok)
32+
self.assertEqual('{"":{}}', newline)
33+
34+
def test_patch_half_object_array(self):
35+
line = '{}]'
36+
ok, newline, _ = JSONFixer().fix(line)
37+
self.assertTrue(ok)
38+
self.assertEqual('[{}]', newline)
39+
40+
def test_patch_half_array_object(self):
41+
line = '[]}'
42+
ok, newline, _ = JSONFixer().fix(line)
43+
self.assertTrue(ok)
44+
self.assertEqual('{"":[]}', newline)
45+
46+
def test_patch_half_array_with_coma(self):
47+
line = '1, [""], -1]'
48+
ok, newline, _ = JSONFixer().fix(line)
49+
self.assertTrue(ok)
50+
self.assertEqual('[1, [""], -1]', newline)
51+
52+
def test_patch_half_array_with_coma_v2(self):
53+
line = '1, 2'
54+
ok, newline, _ = JSONFixer().fix(line)
55+
self.assertTrue(ok)
56+
self.assertEqual('[1, 2]', newline)
57+
58+
def test_patch_half_object_with_colon(self):
59+
line = '"a":'
60+
ok, newline, _ = JSONFixer().fix(line)
61+
self.assertTrue(ok)
62+
self.assertEqual('{"a":null}', newline)

0 commit comments

Comments
 (0)