JsonRemedy.Layer3.SyntaxNormalization (json_remedy v0.1.3)

View Source

Layer 3: Syntax Normalization - Fixes JSON syntax issues using character-by-character parsing.

This layer handles:

  • Quote normalization (single → double quotes)
  • Unquoted keys (add missing quotes)
  • Boolean/null normalization (True/False/None → true/false/null)
  • Comma and colon fixes (trailing commas, missing commas/colons)

Uses character-by-character parsing to be context-aware and preserve string content.

Summary

Functions

Apply a single syntax rule with context awareness.

Get default syntax normalization rules.

Add missing colons in object key-value pairs.

Public API function to fix commas only.

Get position information for error reporting.

Check if a position in the input is inside a string literal. Used to avoid applying repairs to string content.

Return a human-readable name for this layer.

Public API function to normalize booleans only.

Normalize boolean and null literals.

Public API function to normalize quotes only.

Return the priority order for this layer. Layer 3 (Syntax Normalization) should run after structural repair.

Process input string and apply Layer 3 syntax normalization repairs.

Add quotes around unquoted keys.

Check if this layer can handle the given input.

Validate layer configuration and options.

Validate that a syntax rule is well-formed.

Types

layer_result()

@type layer_result() :: JsonRemedy.LayerBehaviour.layer_result()

parse_state()

@type parse_state() :: %{
  result: String.t(),
  position: non_neg_integer(),
  in_string: boolean(),
  escape_next: boolean(),
  string_quote: String.t() | nil,
  repairs: [repair_action()],
  context_stack: [:object | :array],
  expecting: :key | :value | :colon | :comma_or_end
}

repair_action()

@type repair_action() :: JsonRemedy.LayerBehaviour.repair_action()

repair_context()

@type repair_context() :: JsonRemedy.LayerBehaviour.repair_context()

syntax_rule()

@type syntax_rule() :: %{
  name: String.t(),
  processor: (String.t() -> {String.t(), [repair_action()]}),
  condition: (String.t() -> boolean()) | nil
}

Functions

apply_rule(input, rule)

@spec apply_rule(input :: String.t(), rule :: syntax_rule()) ::
  {String.t(), [repair_action()]}

Apply a single syntax rule with context awareness.

default_rules()

@spec default_rules() :: [syntax_rule()]

Get default syntax normalization rules.

fix_colons(input)

@spec fix_colons(input :: String.t()) :: {String.t(), [repair_action()]}

Add missing colons in object key-value pairs.

fix_commas(input)

@spec fix_commas(input :: String.t()) :: {String.t(), [repair_action()]}

Public API function to fix commas only.

get_position_info(input, position)

@spec get_position_info(input :: String.t(), position :: non_neg_integer()) :: %{
  line: pos_integer(),
  column: pos_integer(),
  context: String.t()
}

Get position information for error reporting.

inside_string?(input, position)

@spec inside_string?(input :: String.t(), position :: non_neg_integer()) :: boolean()

Check if a position in the input is inside a string literal. Used to avoid applying repairs to string content.

name()

@spec name() :: String.t()

Return a human-readable name for this layer.

normalize_booleans(input)

@spec normalize_booleans(input :: String.t()) :: {String.t(), [repair_action()]}

Public API function to normalize booleans only.

normalize_literals(input)

@spec normalize_literals(input :: String.t()) :: {String.t(), [repair_action()]}

Normalize boolean and null literals.

normalize_quotes(input)

@spec normalize_quotes(input :: String.t()) :: {String.t(), [repair_action()]}

Public API function to normalize quotes only.

priority()

@spec priority() :: 3

Return the priority order for this layer. Layer 3 (Syntax Normalization) should run after structural repair.

process(input, context)

@spec process(input :: String.t(), context :: repair_context()) :: layer_result()

Process input string and apply Layer 3 syntax normalization repairs.

quote_unquoted_keys(input)

@spec quote_unquoted_keys(input :: String.t()) :: {String.t(), [repair_action()]}

Add quotes around unquoted keys.

supports?(input)

@spec supports?(input :: String.t()) :: boolean()

Check if this layer can handle the given input.

validate_options(options)

@spec validate_options(options :: keyword()) :: :ok | {:error, String.t()}

Validate layer configuration and options.

validate_rule(rule)

@spec validate_rule(rule :: syntax_rule()) :: :ok | {:error, String.t()}

Validate that a syntax rule is well-formed.