Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert recursive/nested OpenStruct Object to Hash

I have an OpenStruct object and needs to convert to JSON data.

Sample Hash (from RSPEC helper):

def test_order
 {
   "id": 505311428702,
   "email": "[email protected]",
   "closed_at": "",
   "discount_codes": {
      "id": 507328175,
      "text": "test"
   }
 }
end

I'm using, below function for recursive:

def to_recursive_ostruct(hash)
  OpenStruct.new(hash.each_with_object({}) do |(key, val), memo|
    memo[key] = val.is_a?(Hash) ? to_recursive_ostruct(val) : val
  end)
end

For ex to_recursive_ostruct(test_order), will return:

<OpenStruct id=505311428702, email="[email protected]", closed_at="", ...>

Once converted, using OpenStructObject.marshal_dump:

{
:id=>505311428702, :email=>"[email protected]", :closed_at=>"", 

discount_codes=>#<OpenStruct id=507328175, text= "test">}
}

OpenStructObject.marshal_dump gives me the right data in first level,

I want also the nested data to beconverted.

What I really need is like:

{:id=>505311428702, :email=>"[email protected]", :closed_at=>"", :discount_codes=>{:id=>507328175, :text=> "test"} }

Please help, thanks in advance.

like image 301
aldrien.h Avatar asked Aug 22 '18 06:08

aldrien.h


3 Answers

Building on work done by @Andrey-Deineko, @owyongsk, @aldrien.h here is a converter that handles arrays. While the OP wasn't looking this in particular, others may find it helpful.


def openstruct_to_h(object)
   object.to_h.transform_values do |value|
      value.is_a?(OpenStruct) ? openstruct_to_h(value) : value.is_a?(Array) ? value.map{|v| v.is_a?(String) ? v : openstruct_to_h(v) } : value
   end
end

# Example usage

open_struct_object = OpenStruct.new(paramtest: "ok", array_test: [OpenStruct.new(array_one: true), OpenStruct.new(array_two: false, nested_os: OpenStruct.new(works: 'maybe'))], os_test: OpenStruct.new(testy: "yup")) 

openstruct_to_h(open_struct_object)

=> {:paramtest=>"ok", :array_test=>[{:array_one=>true}, {:array_two=>false, :nested_os=>{:works=>"maybe"}}], :os_test=>{:testy=>"yup"}}
like image 121
Tayden Avatar answered Nov 13 '22 05:11

Tayden


To convert your deep openstruct to hash you could go with something along these lines:

def deep_openstruct_to_hash(object)
  object.each_pair.with_object({}) do |(key, value), hash|
    hash[key] = value.is_a?(OpenStruct) ? deep_openstruct_to_hash(value) : value
  end
end

Then:

openstruct_object = to_recursive_ostruct(test_order)
#=> #<OpenStruct id=505311428702, email="[email protected]", closed_at="", discount_codes=#<OpenStruct id=507328175, text="test">>
deep_openstruct_to_hash(openstruct_object)
# {
#   :id=>505311428702,
#   :email=>"[email protected]",
#   :closed_at=>"",
#   :discount_codes=>{
#     :id=>507328175,
#     :text=>"test"
#   }
# }
like image 2
Andrey Deineko Avatar answered Nov 13 '22 07:11

Andrey Deineko


On Ruby 2.4+, you can use transform_values together with a recursive function in a monkey patch.

class OpenStruct
  def deep_to_h
    to_h.transform_values do |v|
      v.is_a?(OpenStruct) ? v.deep_to_h : v
    end
  end
end

Or if you don't want to monkey patch

def deep_to_h(obj)
  obj.to_h.transform_values do |v|
    v.is_a?(OpenStruct) ? deep_to_h(v) : v
  end
end
like image 1
owyongsk Avatar answered Nov 13 '22 07:11

owyongsk