MCPcopy Index your code
hub / github.com/HAibiiin/json-repair

github.com/HAibiiin/json-repair @0.4.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.4.0 ↗ · + Follow
122 symbols 252 edges 23 files 0 documented · 0% 30 cross-repo links updated 4mo ago0.4.0 · 2026-02-14★ 1057 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

json-repair : 大语言模型生成异常 JSON 应用层解决方案

Release Maven Central Apache 2.0 license EN doc

Logo

json-repair 是什么?

json-repair 是一个在应用层为 LLMs(大语言模型)生成异常 JSON 提供修复的 Java 库。基于 json-repair 可以简单高效并准确的实现异常 JSON 的修复。

如何使用

要想使用 json-repair,只需将其作为依赖添加到你的 Java 项目中即可。 Maven 配置如下:

<dependency>
    <groupId>io.github.haibiiin</groupId>
    <artifactId>json-repair</artifactId>
    <version>0.4.0</version>
</dependency>

Gradle 配置如下:

implementation 'io.github.haibiiin:json-repair:0.4.0'

接下来你只需要实例化 JSONRepair 并调用 handle() 方法便可以完成异常 JSON 的修复。

JSONRepair repair = new JSONRepair();
String correctJSON = repair.handle(mistakeJSON);

如果需要从文本字符串中提取出符合 JSON 格式的内容,需要通过 JSONRepairConfig 启用提取功能。

JSONRepairConfig config = new JSONRepairConfig();
config.enableExtractJSON();
JSONRepair repair = new JSONRepair(config);
String correctJSON = repair.handle(mistakeJSON);

功能特性

你可以通过查看 测试用例数据集测试报告 了解当前 0.4.0 版本 json-repair 支持修补的所有 JSON 异常。

基于当前 0.4.0 版本的功能如下:

  • 实现对 JSON 字符串基本修补功能:
  • 修补缺少的右括号;
  • 修补缺少的右中括号;
  • 值为数组情况下多余逗号清理;
  • 值缺失以 null 填补;
  • 修补缺少的左括号;
  • 修补缺少的最外层括号;
  • 修补个别场景下字符串缺少引号;
  • 提供自定义修补尝试次数。
  • 实现对基于文本字符串的 JSON 提取功能:
  • 提取符合 JSON 格式的字符串;
  • 并支持对提取字符串的有限修复;

性能指标

你可以通过运行 BenchmarkTests 进行更多场景的性能测试

基于当前 0.4.0 版本的性能测试如下:

--AverageTime --NANOSECONDS --Warmup-5-1-SECONDS
Benchmark                                                          (anomalyJSON)    Mode     Cnt          Score         Error   Units
BenchmarkTests.testSimpleRepairStrategy                      {"f":"v", "f2":"v2"    avgt       5     114767.597 ±  246035.480   ns/op
BenchmarkTests.testSimpleRepairStrategy                         {"f":"v", "a":[1    avgt       5      42658.921 ±   66863.970   ns/op
BenchmarkTests.testSimpleRepairStrategy  {"f":"v", "a":[1,2], "o1":{"f1":"v1"},     avgt       5     410883.872 ±  703643.753   ns/op
BenchmarkTests.testSimpleRepairStrategy     "f":"v", "a":[1,2], "o1":{"f1":"v1"}    avgt       5     180096.819 ±  535679.217   ns/op
BenchmarkTests.testSimpleRepairStrategy                                      f:v    avgt       5     252954.401 ±  908076.158   ns/op

--AverageTime --MILLISECONDS --Warmup-5-1-SECONDS
Benchmark                                                          (anomalyJSON)    Mode     Cnt          Score         Error   Units
BenchmarkTests.testSimpleRepairStrategy                      {"f":"v", "f2":"v2"    avgt       5          0.179 ±       0.256   ms/op
BenchmarkTests.testSimpleRepairStrategy                         {"f":"v", "a":[1    avgt       5          0.215 ±       0.373   ms/op
BenchmarkTests.testSimpleRepairStrategy  {"f":"v", "a":[1,2], "o1":{"f1":"v1"},     avgt       5          0.070 ±       0.152   ms/op
BenchmarkTests.testSimpleRepairStrategy     "f":"v", "a":[1,2], "o1":{"f1":"v1"}    avgt       5          0.306 ±       0.121   ms/op
BenchmarkTests.testSimpleRepairStrategy                                      f:v    avgt       5          0.125 ±       0.243   ms/op

测试覆盖

你可以通过查看 报告 了解项目测试覆盖率详情。

未来规划

  • [ ] 通过提供参考 JSON 样式,对异常 JSON 进行更准确的修正;

开发指南

将代码克隆到本地后,在项目根目录下,编译并安装所有模块到 Maven 本地仓库缓存,同时会生成 ANTLR .g4 语法文件对应的解析器 Java 类,这样在 IDE 就不会有相关的编译错误了。

mvnw install

开源许可

Apache-2.0 license.

Extension points exported contracts — how you extend this code

RepairStrategy (Interface)
(no doc) [4 implementers]
src/main/java/io/github/haibiiin/json/repair/RepairStrategy.java
ExtractStrategy (Interface)
(no doc) [2 implementers]
src/main/java/io/github/haibiiin/json/repair/ExtractStrategy.java

Core symbols most depended-on inside this repo

val
called by 57
src/main/java/io/github/haibiiin/json/repair/antlr/KeySymbol.java
expectingList
called by 44
src/main/java/io/github/haibiiin/json/repair/Expecting.java
key
called by 21
src/main/java/io/github/haibiiin/json/repair/strategy/SimpleRepairStrategy.java
get
called by 10
src/main/java/io/github/haibiiin/json/repair/strategy/SimpleRepairStrategy.java
add
called by 6
src/main/java/io/github/haibiiin/json/repair/Expecting.java
handle
called by 4
src/main/java/io/github/haibiiin/json/repair/JSONRepair.java
build
called by 4
src/main/java/io/github/haibiiin/json/repair/ParserListBuilder.java
isEOF
called by 4
src/main/java/io/github/haibiiin/json/repair/strategy/SimpleRepairStrategy.java

Shape

Method 92
Class 21
Enum 5
Interface 4

Languages

Java100%

Modules by API surface

src/main/java/io/github/haibiiin/json/repair/strategy/SimpleRepairStrategy.java20 symbols
src/main/java/io/github/haibiiin/json/repair/strategy/CorrectRepairStrategy.java20 symbols
src/main/java/io/github/haibiiin/json/repair/Expecting.java12 symbols
src/main/java/io/github/haibiiin/json/repair/JSONRepairConfig.java8 symbols
src/main/java/io/github/haibiiin/json/repair/antlr/SyntaxErrorListener.java7 symbols
src/main/java/io/github/haibiiin/json/repair/antlr/VocabularyWrapper.java6 symbols
src/main/java/io/github/haibiiin/json/repair/JSONRepair.java6 symbols
src/test/java/io/github/haibiiin/json/repair/test/kits/TestCase.java5 symbols
src/main/java/io/github/haibiiin/json/repair/antlr/KeySymbol.java5 symbols
src/test/java/io/github/haibiiin/json/repair/FixerTests.java4 symbols
src/test/java/io/github/haibiiin/json/repair/test/kits/TestCaseArgumentsProvider.java3 symbols
src/test/java/io/github/haibiiin/json/repair/BenchmarkTests.java3 symbols

For agents

$ claude mcp add json-repair \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact