示例#1
0
    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)
示例#2
0
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()
示例#3
0
 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)
示例#4
0
 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)
示例#5
0
 def test_patch_half_array_object(self):
     line = '[]}'
     ok, newline, _ = JSONFixer().fix(line)
     self.assertTrue(ok)
     self.assertEqual('{"":[]}', newline)
示例#6
0
 def test_half_string(self):
     line = '"a'
     ok, newline, _ = JSONFixer().fix(line)
     self.assertTrue(ok)
     self.assertEqual('"a"', newline)
示例#7
0
 def test_litte_key_half(self):
     line = "{'a':}"
     ok, newline, _ = JSONFixer(js_style=True).fix(line)
     self.assertTrue(ok)
     self.assertEqual('{"a":null}', newline)
示例#8
0
 def test_patch_left_object(self):
     line = '}'
     ok, newline, _ = JSONFixer().fix(line)
     self.assertTrue(ok)
     self.assertEqual('{}', newline)
示例#9
0
 def test_patch_many_half_object(self):
     line = '{}[]{}}]'
     ok, newline, _ = JSONFixer().fix(line)
     self.assertTrue(ok)
     self.assertEqual('[{"":{},"":[],"":{}}]', newline)
示例#10
0
 def test_many_from_adhocore(self):
     line = '{"a":1,'
     ok, newline, _ = JSONFixer().fix(line)
     self.assertTrue(ok)
     self.assertEqual('{"a":1}', newline)
示例#11
0
 def test_miss_colon(self):
     line = '{"a":1,"b"'
     ok, newline, _ = JSONFixer().fix(line)
     self.assertTrue(ok)
     self.assertEqual('{"a":1,"b":null}', newline)
示例#12
0
 def test_miss_quote(self):
     line = '{"a'
     ok, newline, _ = JSONFixer().fix(line)
     self.assertTrue(ok)
     self.assertEqual('{"a":null}', newline)
示例#13
0
 def test_simple_mix_A(self):
     line = '[{,'
     ok, newline, _ = JSONFixer().fix(line)
     self.assertTrue(ok)
     self.assertEqual('[{}]', newline)
示例#14
0
 def test_half_array_with_element(self):
     line = '[1'
     ok, newline, _ = JSONFixer().fix(line)
     self.assertTrue(ok)
     self.assertEqual('[1]', newline)
示例#15
0
 def test_object_miss_key(self):
     line = '{:1}'
     ok, newline, _ = JSONFixer().fix(line)
     self.assertTrue(ok)
     self.assertEqual('{"":1}', newline)
示例#16
0
 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)
示例#17
0
 def test_patch_half_object_with_colon(self):
     line = '"a":'
     ok, newline, _ = JSONFixer().fix(line)
     self.assertTrue(ok)
     self.assertEqual('{"a":null}', newline)
示例#18
0
 def test_half_object(self):
     line = '{'
     ok, newline, _ = JSONFixer().fix(line)
     self.assertTrue(ok)
     self.assertEqual('{}', newline)
示例#19
0
 def test_patch_string(self):
     line = 'E"'
     ok, newline, _ = JSONFixer().fix(line)
     self.assertTrue(ok)
     self.assertEqual('"E"', newline)
示例#20
0
 def test_case_miss_key(self):
     line = '{['
     ok, newline, _ = JSONFixer().fix(line)
     self.assertTrue(ok)
     self.assertEqual('{"":[]}', newline)
示例#21
0
# 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))
示例#22
0
 def test_object_miss_value(self):
     line = '{"V":}'
     ok, newline, _ = JSONFixer().fix(line)
     self.assertTrue(ok)
     self.assertEqual('{"V":null}', newline)
示例#23
0
 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)
示例#24
0
 def test_array_miss_value(self):
     line = '[,]'
     ok, newline, _ = JSONFixer().fix(line)
     self.assertTrue(ok)
     self.assertEqual('[]', newline)
示例#25
0
 def test_patch_left_array(self):
     line = ']'
     ok, newline, _ = JSONFixer().fix(line)
     self.assertTrue(ok)
     self.assertEqual('[]', newline)
示例#26
0
 def test_patch_half_object_array(self):
     line = '{}]'
     ok, newline, _ = JSONFixer().fix(line)
     self.assertTrue(ok)
     self.assertEqual('[{}]', newline)
示例#27
0
 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)
示例#28
0
 def test_half_array(self):
     line = '['
     ok, newline, _ = JSONFixer().fix(line)
     self.assertTrue(ok)
     self.assertEqual('[]', newline)