JsonRemedy (json_remedy v0.1.0)
View SourceA 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
@type strategy() :: :binary_patterns | :combinators | :streaming
Functions
@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"]}
@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 tuplestrategy: :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"]}
@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()
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]"}