Class: ClaudeSwarm::JsonHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/claude_swarm/json_handler.rb

Overview

Centralized JSON handling for the Claude Swarm codebase

Class Method Summary collapse

Class Method Details

.parse(json_string, raise_on_error: false) ⇒ Object

Parse JSON string into Ruby object

Parameters:

  • json_string (String)

    The JSON string to parse

  • raise_on_error (Boolean) (defaults to: false)

    Whether to raise exception on error (default: false)

Returns:

  • (Object)

    The parsed Ruby object, or original string if parsing fails and raise_on_error is false

Raises:

  • (JSON::ParserError)

    If the JSON is invalid and raise_on_error is true



12
13
14
15
16
17
18
# File 'lib/claude_swarm/json_handler.rb', line 12

def parse(json_string, raise_on_error: false)
  JSON.parse(json_string)
rescue JSON::ParserError => e
  raise e if raise_on_error

  json_string
end

.parse!(json_string) ⇒ Object

Parse JSON string with exception raising

Parameters:

  • json_string (String)

    The JSON string to parse

Returns:

  • (Object)

    The parsed Ruby object

Raises:

  • (JSON::ParserError)

    If the JSON is invalid



24
25
26
# File 'lib/claude_swarm/json_handler.rb', line 24

def parse!(json_string)
  parse(json_string, raise_on_error: true)
end

.parse_file(file_path) ⇒ Object?

Parse JSON from a file, returning nil on error

Parameters:

  • file_path (String)

    Path to the JSON file

Returns:

  • (Object, nil)

    The parsed Ruby object or nil if file doesn't exist or contains invalid JSON



41
42
43
44
45
# File 'lib/claude_swarm/json_handler.rb', line 41

def parse_file(file_path)
  parse_file!(file_path)
rescue Errno::ENOENT, JSON::ParserError
  nil
end

.parse_file!(file_path) ⇒ Object

Parse JSON from a file with exception raising

Parameters:

  • file_path (String)

    Path to the JSON file

Returns:

  • (Object)

    The parsed Ruby object

Raises:

  • (Errno::ENOENT)

    If the file does not exist

  • (JSON::ParserError)

    If the file contains invalid JSON



33
34
35
36
# File 'lib/claude_swarm/json_handler.rb', line 33

def parse_file!(file_path)
  content = File.read(file_path)
  parse!(content)
end

.pretty_generate(object, raise_on_error: false) ⇒ String?

Generate pretty-formatted JSON string

Parameters:

  • object (Object)

    The Ruby object to convert to JSON

  • raise_on_error (Boolean) (defaults to: false)

    Whether to raise exception on error (default: false)

Returns:

  • (String, nil)

    The pretty-formatted JSON string, or nil if generation fails and raise_on_error is false

Raises:

  • (JSON::GeneratorError)

    If the object cannot be converted to JSON and raise_on_error is true



52
53
54
55
56
57
58
# File 'lib/claude_swarm/json_handler.rb', line 52

def pretty_generate(object, raise_on_error: false)
  JSON.pretty_generate(object)
rescue JSON::GeneratorError, JSON::NestingError => e
  raise e if raise_on_error

  nil
end

.pretty_generate!(object) ⇒ String

Generate pretty-formatted JSON string with exception raising

Parameters:

  • object (Object)

    The Ruby object to convert to JSON

Returns:

  • (String)

    The pretty-formatted JSON string

Raises:

  • (JSON::GeneratorError)

    If the object cannot be converted to JSON



64
65
66
# File 'lib/claude_swarm/json_handler.rb', line 64

def pretty_generate!(object)
  pretty_generate(object, raise_on_error: true)
end

.write_file(file_path, object) ⇒ Boolean

Write Ruby object to a JSON file with pretty formatting

Parameters:

  • file_path (String)

    Path to the JSON file

  • object (Object)

    The Ruby object to write

Returns:

  • (Boolean)

    True if successful, false if generation or write fails



72
73
74
75
76
77
78
# File 'lib/claude_swarm/json_handler.rb', line 72

def write_file(file_path, object)
  json_string = pretty_generate!(object)
  File.write(file_path, json_string)
  true
rescue JSON::GeneratorError, JSON::NestingError, SystemCallError
  false
end

.write_file!(file_path, object) ⇒ Object

Write Ruby object to a JSON file with exception raising

Parameters:

  • file_path (String)

    Path to the JSON file

  • object (Object)

    The Ruby object to write

Raises:

  • (JSON::GeneratorError)

    If the object cannot be converted to JSON

  • (SystemCallError)

    If the file cannot be written



85
86
87
88
# File 'lib/claude_swarm/json_handler.rb', line 85

def write_file!(file_path, object)
  json_string = pretty_generate!(object)
  File.write(file_path, json_string)
end