Skip to main content

jsonfixer: fix invalid json: broken-json / truncated-json.

Project description

Install

pip install jsonfixer

Usage

In [1]: from half_json.core import JSONFixer

In [2]: f = JSONFixer()

In [3]: f.fix('[{')
Out[3]: FixResult(success=True, line='[{}]', origin=False)

In [4]: f.fix('{"a')
Out[4]: FixResult(success=True, line='{"a":null}', origin=False)

In [5]: f.fix('{"a":}')
Out[5]: FixResult(success=True, line='{"a":null}', origin=False)

目标

fix invalid/broken/truncated json

修复原理

  1. JSONDecoderError
  2. line context

HitRatio

根据 checks 里面的工具来衡量修改效果

ABC : autogen --> broken --> check TSR : run test.sh show.sh ratio.sh

FixRatio

仅判断 result.success == True

./runratio.sh fix
1.  0.4269, 0.4287, 0.4303   # 实现完 12 条规则
2.  0.5037, 0.5084, 0.5077   # string 的 " 补充在末尾
3.  0.5259, 0.5224, 0.5187   # Array 需要 pos - 2
4.  0.5433, 0.5311, 0.5381   # Array 细化一下 [, 的情况
5.  0.7192, 0.7216, 0.7265   # 大改进, FIX 之前的 Bug( parser 被冲掉了)
6.  0.7732, 0.7686, 0.7701   # case: {"a":1 --> 补充 }
7.  0.60  , 0.58             # 去掉了空行
8.  0.6971, 0.6969, 0.6984   # 增加处理 StopIteration
9.  0.7428, 0.7383, 0.7427   # 增加处理 half parse
10. 0.7617, 0.7631, 0.7558   # 细化处理 half parse
11. 0.7608, 0.7612, 0.7650   # 添加从左处理
12. 0.9817, 0.9827, 0.9819   # 增加对字符串的处理
13. 0.8314, 0.8302, 0.8312   # 去掉对字符串的额外处理
14. 0.95X                    # 已不可参考

Real HitRatio

判断 result.success == True

并且解析后的 json 大体和原来一致(equal && dictdiffer)

./runratio.sh hit
1. 0.5610, 0.5563, 0.5529   # origin
2. 0.5593, 0.5532, 0.5587   # fix :} --> :null}

TODO

目前的缺点 && 发现

  1. 数字的支持比较弱 --> -02 / 0. / .0

  2. 还不支持分支回溯 --> [{]

  3. 突然想到, 应该反思一下, 这个是一个fixer, 而不是一个将任何字符串都转为 json 的东西 应该明确 JSONFixer 的能力范围, 对 runratio.sh 也应该比较前后两个的 json 相似程度。 (听起来像无能者的辩白?)

  4. 也需要吧 parser 也做成 stack 这样可以解决 ["a] --> ["a"] 这样的 case

  5. 考虑分支回溯的方式来试探

  6. 解析缺失的 JSON 常量

BadCase

  1. 1- --> {"1-": null}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonfixer-0.2.2.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

jsonfixer-0.2.2-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file jsonfixer-0.2.2.tar.gz.

File metadata

  • Download URL: jsonfixer-0.2.2.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.9.3 CPython/3.10.6

File hashes

Hashes for jsonfixer-0.2.2.tar.gz
Algorithm Hash digest
SHA256 ee684c7b78acd24c64ed9e5e747ff934a90564c9a61b3f9f9608f8c7ec7ec29d
MD5 ca3064dd327132413b5af34c8f0d6bae
BLAKE2b-256 9dd99aa9fc3dd995340f8acf86e98c30cf9c44879bbd23e0bd016c75410c7119

See more details on using hashes here.

File details

Details for the file jsonfixer-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: jsonfixer-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.9.3 CPython/3.10.6

File hashes

Hashes for jsonfixer-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cb875c551a63d308a71d25ab55c98c28afe9010545b9e4d5db6c17f56d1aeadf
MD5 075592f41354c2f6cdd15fcfae97fbaf
BLAKE2b-256 e18ca93a83b54009085a02e43a3033f95613f7f1e0b95b3a88096a9b068662a6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page