简体   繁体   English

如何将递归/嵌套的 OpenStruct 对象转换为哈希

[英]How to convert recursive/nested OpenStruct Object to Hash

I have an OpenStruct object and needs to convert to JSON data.我有一个 OpenStruct 对象,需要转换为 JSON 数据。

Sample Hash (from RSPEC helper):样本哈希(来自 RSPEC 助手):

def test_order
 {
   "id": 505311428702,
   "email": "test@gmail.com",
   "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:对于前 to_recursive_ostruct(test_order),将返回:

<OpenStruct id=505311428702, email="test@gmail.com", closed_at="", ...>

Once converted, using OpenStructObject.marshal_dump :转换后,使用OpenStructObject.marshal_dump

{
:id=>505311428702, :email=>"test@gmail.com", :closed_at=>"", 

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

OpenStructObject.marshal_dump gives me the right data in first level, OpenStructObject.marshal_dump在第一级给了我正确的数据,

I want also the nested data to beconverted.我还希望转换嵌套数据。

What I really need is like:我真正需要的是:

{:id=>505311428702, :email=>"test@gmail.com", :closed_at=>"", :discount_codes=>{:id=>507328175, :text=> "test"} }

Please help, thanks in advance.请帮忙,提前致谢。

Building on work done by @Andrey-Deineko, @owyongsk, @aldrien.h here is a converter that handles arrays.在@Andrey-Deineko、@owyongsk、@aldrien.h 完成的工作的基础上,这里是一个处理数组的转换器。 While the OP wasn't looking this in particular, others may find it helpful.虽然 OP 并没有特别关注这一点,但其他人可能会发现它有帮助。


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"}}

Check out docs .查看文档

You can use OpenStruct#marshal_dump :您可以使用OpenStruct#marshal_dump

openstruct_object.marshal_dump

OpenStruct#to_h will work, too: OpenStruct#to_h也可以工作:

openstruct_object.to_h

You can convert your object to hash and then hash to JSON:您可以将对象转换为哈希,然后将哈希转换为 JSON:

openstruct_object.to_h.to_json

But it looks like what you want is a Hash object, not JSON object.但看起来您想要的是 Hash 对象,而不是 JSON 对象。

To convert your deep openstruct to hash you could go with something along these lines:要将您的深度 openstruct 转换为哈希,您可以使用以下方法:

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="test@gmail.com", closed_at="", discount_codes=#<OpenStruct id=507328175, text="test">>
deep_openstruct_to_hash(openstruct_object)
# {
#   :id=>505311428702,
#   :email=>"test@gmail.com",
#   :closed_at=>"",
#   :discount_codes=>{
#     :id=>507328175,
#     :text=>"test"
#   }
# }

On Ruby 2.4+, you can use transform_values together with a recursive function in a monkey patch.在 Ruby 2.4+ 上,您可以在猴子补丁中将transform_values与递归函数一起使用。

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

Thanks also to this gist : Can converted array of hash as well.还要感谢这个要点:也可以转换哈希数组。

def recursive_ostruct(object)
  case object
  when Hash
    hash = {}; object.each{|k,v| hash[k] = recursive_ostruct(v)}
    OpenStruct.new(hash)
  when Array
    object.map {|e| recursive_ostruct(e) }
  else
    object
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM