In Crystal language, what is the difference between JSON::Any and JSON::Type? What are the use cases of this types?
JSON::Any is a struct, which is returned as a result of parsing. It has convenient methods to access underlying data as_s
, as_bool
, as_f
etc. :
obj = JSON.parse %({"access": true})
p obj.class # => JSON::Any
p obj["access"] # => true
p obj["access"].class # => JSON::Any
JSON::Type
is an union type of all possible json types. It is used internally by JSON::Any
struct to represent the data:
p obj.raw # => {"access" => true}
p obj.raw.class # => Hash(String, JSON::Type)