Class: ClaudeSwarm::JsonHandler
- Inherits:
-
Object
- Object
- ClaudeSwarm::JsonHandler
- Defined in:
- lib/claude_swarm/json_handler.rb
Overview
Centralized JSON handling for the Claude Swarm codebase
Class Method Summary collapse
-
.parse(json_string, raise_on_error: false) ⇒ Object
Parse JSON string into Ruby object.
-
.parse!(json_string) ⇒ Object
Parse JSON string with exception raising.
-
.parse_file(file_path) ⇒ Object?
Parse JSON from a file, returning nil on error.
-
.parse_file!(file_path) ⇒ Object
Parse JSON from a file with exception raising.
-
.pretty_generate(object, raise_on_error: false) ⇒ String?
Generate pretty-formatted JSON string.
-
.pretty_generate!(object) ⇒ String
Generate pretty-formatted JSON string with exception raising.
-
.write_file(file_path, object) ⇒ Boolean
Write Ruby object to a JSON file with pretty formatting.
-
.write_file!(file_path, object) ⇒ Object
Write Ruby object to a JSON file with exception raising.
Class Method Details
.parse(json_string, raise_on_error: false) ⇒ Object
Parse JSON string into Ruby object
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
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
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
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
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
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
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
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 |