JsonRemedy (json_remedy v0.1.0)

View Source

A blazingly fast, Elixir-native JSON repair library.

JsonRemedy uses advanced binary pattern matching and functional composition to intelligently repair malformed JSON strings while achieving superior performance.

Examples

iex> JsonRemedy.repair(~s|{name: "Alice", age: 30, active: True}|)
{:ok, %{"name" => "Alice", "age" => 30, "active" => true}}

iex> JsonRemedy.repair_to_string(~s|[1, 2, 3,]|)
{:ok, "[1,2,3]"}

iex> JsonRemedy.repair(~s|{incomplete: "data"|, logging: true)
{:ok, %{"incomplete" => "data"}, ["quoted unquoted keys", "added missing closing brace"]}

Summary

Functions

Repairs JSON content directly from a file.

Repairs malformed JSON and returns the parsed Elixir term.

Creates a stream that repairs JSON objects from an input stream.

Repairs malformed JSON and returns the fixed JSON string.

Types

option()

@type option() ::
  {:logging, boolean()} | {:strategy, strategy()} | {:strict, boolean()}

repair_result()

@type repair_result() :: {:ok, term()} | {:error, String.t()}

repair_result_with_logs()

@type repair_result_with_logs() :: {:ok, term(), [String.t()]} | {:error, String.t()}

strategy()

@type strategy() :: :binary_patterns | :combinators | :streaming

Functions

from_file(path, opts \\ [])

@spec from_file(Path.t(), [option()]) :: repair_result() | repair_result_with_logs()

Repairs JSON content directly from a file.

Examples

iex> JsonRemedy.from_file("config.json")
{:ok, %{"setting" => "value"}}

iex> JsonRemedy.from_file("malformed.json", logging: true)
{:ok, %{"data" => "value"}, ["quoted unquoted string", "added missing colon"]}

repair(json_string, opts \\ [])

@spec repair(binary(), [option()]) :: repair_result() | repair_result_with_logs()

Repairs malformed JSON and returns the parsed Elixir term.

Options

  • logging: true - Returns repair actions taken as third element in tuple
  • strategy: :binary_patterns - Use binary pattern matching (default, fastest)
  • strategy: :combinators - Use parser combinators (most elegant)
  • strategy: :streaming - Use stream processing (for large files)
  • strict: false - Allow non-standard JSON extensions (default true)

Examples

iex> JsonRemedy.repair(~s|{"name": "John", "age": 30}|)
{:ok, %{"name" => "John", "age" => 30}}

iex> JsonRemedy.repair(~s|{name: "John", age: 30, active: True}|)
{:ok, %{"name" => "John", "age" => 30, "active" => true}}

iex> JsonRemedy.repair(~s|[1, 2, 3,]|, logging: true)
{:ok, [1, 2, 3], ["removed trailing comma"]}

repair_stream(stream)

@spec repair_stream(Enumerable.t()) :: Enumerable.t()

Creates a stream that repairs JSON objects from an input stream.

Useful for processing large files or real-time data streams.

Examples

"large_file.json"
|> File.stream!()
|> JsonRemedy.repair_stream()
|> Stream.each(&IO.inspect/1)
|> Stream.run()

repair_to_string(json_string, opts \\ [])

@spec repair_to_string(binary(), [option()]) :: {:ok, binary()} | {:error, String.t()}

Repairs malformed JSON and returns the fixed JSON string.

Examples

iex> JsonRemedy.repair_to_string(~s|{name: "Alice"}|)
{:ok, ~s|{"name":"Alice"}|}

iex> JsonRemedy.repair_to_string(~s|[1, 2, 3,]|)
{:ok, "[1,2,3]"}