Skip to content

A Java wrapper around a Rust implementation for repairing malformed or truncated JSON strings.

Notifications You must be signed in to change notification settings

DedInc/jsonfixer4j

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

JSONFixer4J

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).

Build from Source

You will need a Rust toolchain (Cargo) and a JDK installed.

  1. Clone the repository

    git clone https://github.com/DedInc/jsonfixer4j
    cd jsonfixer4j/jsonfixer_rust
  2. Compile the native library

    cargo build --release

    The shared library will be generated in target/release/:

    • Linux: libjsonfixer_rust.so
    • Windows: jsonfixer_rust.dll

Usage

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);
        }
    }
}

Performance

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.

Pre-built Binaries

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.

About

A Java wrapper around a Rust implementation for repairing malformed or truncated JSON strings.

Topics

Resources

Stars

Watchers

Forks