A Java wrapper around a Rust implementation for repairing malformed or truncated JSON strings. It uses JNI to invoke the Rust backend.
This tool attempts to fix common syntax errors such as:
- Missing brackets or braces (
{,}). - Missing commas between key-value pairs.
- Unterminated strings.
- Truncated literals (
tru,fals,nu).
You will need a Rust toolchain (Cargo) and a JDK installed.
-
Clone the repository
git clone https://github.com/DedInc/jsonfixer4j cd jsonfixer4j/jsonfixer_rust -
Compile the native library
cargo build --release
The shared library will be generated in
target/release/:- Linux:
libjsonfixer_rust.so - Windows:
jsonfixer_rust.dll
- Linux:
Ensure the compiled native library (.dll or .so) is available in your Java library path, or load it explicitly in your code before using the class.
import com.github.dedinc.jsonfixer4j.JSONFixerRust;
public class Main {
public static void main(String[] args) {
// Ensure native lib is loaded if not handled statically
// System.load("/path/to/libjsonfixer_rust.so");
JSONFixerRust corrector = new JSONFixerRust();
String[] brokenCases = {
"{\"key\": 123", // Missing brace
"{\"arr\": [1, 2, 3}", // Missing bracket
"{\"key\": \"test\", \"star, ", // Truncated
"{\"flag\": tr, \"value\": nul}" // Partial literals
};
for (String broken : brokenCases) {
String fixed = corrector.autocorrect(broken);
System.out.println(fixed);
}
}
}The logic is handled in Rust to avoid overhead during heavy string manipulation, though there is a small cost associated with the JNI boundary crossing.
Binaries for Windows (x64) and Linux (x64) are available in the Releases tab. MacOS and ARM builds are currently not provided; use cargo build to generate them locally.