Class: Code::Object::Json

Inherits:
Code::Object show all
Defined in:
lib/code/object/json.rb

Instance Attribute Summary

Attributes inherited from Code::Object

#raw

Class Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, as_json, #as_json, #call, call, #code_and_operator, code_and_operator, #code_as_json, code_as_json, #code_different, code_different, #code_equal_equal, code_equal_equal, #code_equal_equal_equal, code_equal_equal_equal, code_exclamation_point, #code_exclamation_point, code_exclusive_range, #code_exclusive_range, code_inclusive_range, #code_inclusive_range, code_new, #code_or_operator, code_or_operator, code_self, #code_self, #code_to_json, code_to_json, falsy?, #falsy?, #hash, #initialize, inspect, maybe, multi_fetch, #multi_fetch, nothing?, #nothing?, repeat, sig, #sig, #succ, to_json, #to_json, to_s, truthy?, #truthy?, |

Constructor Details

This class inherits a constructor from Code::Object

Class Method Details

.to_code(json) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/code/object/json.rb', line 6

def self.to_code(json)
  if json.is_an?(Object)
    json
  elsif json.is_a?(::Hash)
    Dictionary.new(
      json
        .transform_keys { |key| Json.to_code(key) }
        .transform_values { |value| Json.to_code(value) }
    )
  elsif json.is_a?(::Array)
    List.new(json.map { |element| Json.to_code(element) })
  elsif json.is_a?(::String)
    String.new(json)
  elsif json.is_a?(::Float)
    Decimal.new(json)
  elsif json.is_an?(::Integer)
    Integer.new(json)
  elsif json.is_a?(::TrueClass) || json.is_a?(::FalseClass)
    Boolean.new(json)
  elsif json.is_a?(::NilClass)
    Nothing.new(json)
  else
    raise Error, "#{json.inspect} not supported"
  end
end