Set JSON.parse returned object and array classes
By default, the Ruby JSON.parse
method returns a ruby Hash for any json object, and a ruby Array for any json array.
However, you can customize the returned object classes using the object_class
and array_class
options:
source = JSON.dump({ wibble: "wubble", data: [1,2,3] })
result = JSON.parse(
source,
object_class: OpenStruct,
array_class: Set
)
# => #<OpenStruct wibble="wubble", data=#<Set: {1, 2, 3}>>
result.data # => #<Set: {1, 2, 3}>
result.wibble # => "wubble"
Tweet