Class: LLM::JSONAdapter::JSON

Inherits:
JSONAdapter
  • Object
show all
Defined in:
lib/llm/json_adapter.rb

Overview

The LLM::JSONAdapter::JSON class provides a JSON adapter backed by the standard library JSON module.

Class Method Summary collapse

Class Method Details

.dump(obj, state = nil, **options) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/llm/json_adapter.rb', line 75

def self.dump(obj, state = nil, **options)
  require "json" unless defined?(::JSON)
  pretty = options.delete(:pretty) || false
  if ::JSON::State === state
    if pretty
      ::JSON.pretty_generate normalize(obj), state
    else
      ::JSON.generate normalize(obj), state
    end
  elsif state
    if pretty
      ::JSON.pretty_generate normalize(obj), state, **options
    else
      ::JSON.dump normalize(obj), state, **options
    end
  else
    if pretty
      ::JSON.pretty_generate normalize(obj), **options
    else
      ::JSON.dump normalize(obj), **options
    end
  end
end

.load(string) ⇒ Object



101
102
103
104
# File 'lib/llm/json_adapter.rb', line 101

def self.load(string, ...)
  require "json" unless defined?(::JSON)
  ::JSON.parse(string, ...)
end

.parser_error ⇒ Object



109
110
111
112
# File 'lib/llm/json_adapter.rb', line 109

def self.parser_error
  require "json" unless defined?(::JSON)
  [::JSON::ParserError]
end