JsonRemedy.Context.JsonContext (json_remedy v0.1.3)
View SourceCentralized context tracking for JSON repair operations.
This module provides state management for tracking the current parsing context, which enables context-aware repair decisions across all layers. It tracks whether we're in an object key, object value, array, or string context.
Summary
Functions
Determines if a repair can be applied in the current context.
Returns the depth of the context stack.
Enters string parsing context with the given delimiter.
Exits string parsing context.
Checks if the context is currently inside a string.
Creates a new empty context with default values.
Pops the most recent context from the stack.
Pushes a new context onto the context stack.
Transitions from the current context to a new context.
Updates the current position in the input string.
Types
@type context_value() :: :root | :object_key | :object_value | :array
@type t() :: %JsonRemedy.Context.JsonContext{ current: context_value(), in_string: boolean(), position: non_neg_integer(), stack: [context_value()], string_delimiter: String.t() | nil }
Functions
Determines if a repair can be applied in the current context.
String delimiter repairs are allowed when in strings, but most other repairs should be avoided to prevent corrupting string content.
Examples
iex> context = JsonRemedy.Context.JsonContext.new()
iex> JsonRemedy.Context.JsonContext.can_apply_repair?(context, :quote_normalization)
true
iex> string_context = JsonRemedy.Context.JsonContext.enter_string(context, "\"")
iex> JsonRemedy.Context.JsonContext.can_apply_repair?(string_context, :quote_normalization)
false
iex> JsonRemedy.Context.JsonContext.can_apply_repair?(string_context, :string_delimiter)
true
@spec context_stack_depth(t()) :: non_neg_integer()
Returns the depth of the context stack.
Examples
iex> context = JsonRemedy.Context.JsonContext.new()
iex> JsonRemedy.Context.JsonContext.context_stack_depth(context)
0
iex> nested = %JsonRemedy.Context.JsonContext{stack: [:root, :array]}
iex> JsonRemedy.Context.JsonContext.context_stack_depth(nested)
2
Enters string parsing context with the given delimiter.
Examples
iex> context = JsonRemedy.Context.JsonContext.new()
iex> string_context = JsonRemedy.Context.JsonContext.enter_string(context, "\"")
iex> string_context.in_string
true
iex> string_context.string_delimiter
"\""
Exits string parsing context.
Examples
iex> context = %JsonRemedy.Context.JsonContext{in_string: true, string_delimiter: "\""}
iex> exited = JsonRemedy.Context.JsonContext.exit_string(context)
iex> exited.in_string
false
iex> exited.string_delimiter
nil
Checks if the context is currently inside a string.
Examples
iex> context = JsonRemedy.Context.JsonContext.new()
iex> JsonRemedy.Context.JsonContext.is_in_string?(context)
false
iex> string_context = JsonRemedy.Context.JsonContext.enter_string(context, "\"")
iex> JsonRemedy.Context.JsonContext.is_in_string?(string_context)
true
@spec new() :: %JsonRemedy.Context.JsonContext{
current: :root,
in_string: false,
position: 0,
stack: [],
string_delimiter: nil
}
Creates a new empty context with default values.
Examples
iex> context = JsonRemedy.Context.JsonContext.new()
iex> context.current
:root
iex> context.stack
[]
iex> context.position
0
iex> context.in_string
false
Pops the most recent context from the stack.
If the stack is empty, the context remains unchanged.
Examples
iex> context = %JsonRemedy.Context.JsonContext{current: :object_key, stack: [:root]}
iex> popped = JsonRemedy.Context.JsonContext.pop_context(context)
iex> popped.current
:root
iex> popped.stack
[]
@spec push_context(t(), context_value()) :: t()
Pushes a new context onto the context stack.
The current context becomes the new context and the previous current context is pushed onto the stack.
Examples
iex> context = JsonRemedy.Context.JsonContext.new()
iex> new_context = JsonRemedy.Context.JsonContext.push_context(context, :object_key)
iex> new_context.current
:object_key
iex> new_context.stack
[:root]
@spec transition_context(t(), context_value()) :: t()
Transitions from the current context to a new context.
Unlike push_context/2, this doesn't modify the stack.
Examples
iex> context = %JsonRemedy.Context.JsonContext{current: :object_key}
iex> transitioned = JsonRemedy.Context.JsonContext.transition_context(context, :object_value)
iex> transitioned.current
:object_value
@spec update_position(t(), non_neg_integer()) :: t()
Updates the current position in the input string.
Examples
iex> context = JsonRemedy.Context.JsonContext.new()
iex> updated = JsonRemedy.Context.JsonContext.update_position(context, 15)
iex> updated.position
15