代码之家  ›  专栏  ›  技术社区  ›  t6d

如何使用Sequel和Ruby序列化数据?

  •  3
  • t6d  · 技术社区  · 16 年前

    Sequel

    4 回复  |  直到 15 年前
        1
  •  5
  •   davetron5000    11 年前

    如果您使用的是Sequel::Model,则 Serialization plugin 我们应该做到这一点。

        2
  •  5
  •   the Tin Man    13 年前

    # outside of rails you'll need this
    require 'base64'
    
    # encode
    h = { :first => "John", :age => 23 }
    encoded = Base64.encode64(Marshal.dump(h))
    
    # decode
    h = Marshal.load(Base64.decode64(encoded))
    

    我使用它来序列化Ruby对象(例如跨JSON和DB),您会发现Rails中的cookie会话以相同的方式对会话哈希进行编码。使用此工具从浏览器cookie调试会话内容通常很方便。

        3
  •  1
  •   mwp    10 年前

    如果你在找 JSON-API JSONAPI::Serializers :skip_collection_check 选项,并且为了提高性能,您应该1)传入结果集,而不是数据集,2)在序列化(如果您正在侧加载数据)之前传入渴望的加载关系。

        4
  •  0
  •   Gergely Máté    8 年前

    您可以尝试一下YAML:

    require 'yaml'
    
    # encode
    my_hash = { :first => "John", :age => 23 }
    encoded = YAML.dump(my_hash)
    
    # decode
    my_hash = YAML.load(encoded)
    

    here .

    推荐文章