def fixjson(): infile = sys.argv[1] outfile = sys.argv[2] inf = open(infile, 'r') outf = open(outfile, 'w') total = 0 hit = 0 fixer = JSONFixer() for line in inf: try: line = line.strip() if not line: continue total += 1 result = fixer.fix(line) if result.success: outf.write(result.line + "\n") if not result.origin: hit += 1 else: print(result) except Exception as e: print(e, line) print("total is {} and hit {} --> ratio:{} \n".format( total, hit, hit * 1.0 / total)) inf.close() outf.close()
def test_range_head(self): fixer = JSONFixer(200) for i in range(1000): idx = random.randint(1, len(self.line)) result = fixer.fix(self.line[idx:]) if not result.success: print(result.line) self.assertTrue(result.success)
def test_litte_key(self): line = "{'a':1, 'b':{'c':[]}}" ok, newline, _ = JSONFixer(js_style=True).fix(line) self.assertTrue(ok) self.assertEqual('{"a":1, "b":{"c":[]}}', newline) for i in range(1, len(line)): ok, newline, _ = JSONFixer(js_style=True).fix(line[:i]) self.assertTrue(ok)
def test_case_from_stackoverflow(self): line = '{"title": "Center "ADVANCE"", "text": "Business.English."}' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual( '{"title": "Center ","ADVANCE":", ","text": "Business.English."}', newline)
def test_patch_half_array_object(self): line = '[]}' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('{"":[]}', newline)
def test_half_string(self): line = '"a' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('"a"', newline)
def test_litte_key_half(self): line = "{'a':}" ok, newline, _ = JSONFixer(js_style=True).fix(line) self.assertTrue(ok) self.assertEqual('{"a":null}', newline)
def test_patch_left_object(self): line = '}' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('{}', newline)
def test_patch_many_half_object(self): line = '{}[]{}}]' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('[{"":{},"":[],"":{}}]', newline)
def test_many_from_adhocore(self): line = '{"a":1,' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('{"a":1}', newline)
def test_miss_colon(self): line = '{"a":1,"b"' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('{"a":1,"b":null}', newline)
def test_miss_quote(self): line = '{"a' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('{"a":null}', newline)
def test_simple_mix_A(self): line = '[{,' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('[{}]', newline)
def test_half_array_with_element(self): line = '[1' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('[1]', newline)
def test_object_miss_key(self): line = '{:1}' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('{"":1}', newline)
def test_patch_half_array_with_coma_v2(self): line = '1, 2' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('[1, 2]', newline)
def test_patch_half_object_with_colon(self): line = '"a":' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('{"a":null}', newline)
def test_half_object(self): line = '{' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('{}', newline)
def test_patch_string(self): line = 'E"' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('"E"', newline)
def test_case_miss_key(self): line = '{[' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('{"":[]}', newline)
# coding=utf8 import sys import json from half_json.core import JSONFixer f = JSONFixer(100) def json_equal(line, origin): return json.loads(line) == json.loads(origin) def main(inflie, outfile): inf = open(inflie, 'r') outf = open(outfile, 'w') total = 0 hit = 0 fix = 0 for line in inf: info = json.loads(line) result = f.fix(info['broken']) info['fixed'] = result.success info['fix'] = result.line info['hited'] = False if info['fixed']: info['hited'] = json_equal(result.line, info['origin']) outf.write(json.dumps(info))
def test_object_miss_value(self): line = '{"V":}' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('{"V":null}', newline)
def test_range_tail(self): fixer = JSONFixer() for i in range(1000): idx = random.randint(1, len(self.line)) result = fixer.fix(self.line[:idx]) self.assertTrue(result.success)
def test_array_miss_value(self): line = '[,]' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('[]', newline)
def test_patch_left_array(self): line = ']' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('[]', newline)
def test_patch_half_object_array(self): line = '{}]' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('[{}]', newline)
def test_bare_key(self): line = '{a:1, b:{c:3}}' ok, newline, _ = JSONFixer(js_style=True).fix(line) self.assertTrue(ok) self.assertEqual('{"a":1, "b":{"c":3}}', newline)
def test_half_array(self): line = '[' ok, newline, _ = JSONFixer().fix(line) self.assertTrue(ok) self.assertEqual('[]', newline)