2010-01-25 20 views

回答

6

如果您使用Sequel :: Model,則Serialization plugin應該有效。

+0

它的工作原理!非常感謝。我可能應該花更多時間閱讀文檔:http://sequel.rubyforge.org/rdoc-plugins/classes/Sequel/Plugins/Serialization.html – t6d 2010-01-25 18:47:22

+0

至於保存序列化數據的實際列,一個Blob看起來像要走的路 - http://stackoverflow.com/questions/5544749/what-c​​olumn-type-should-be-used-to-store-serialized-data-in-a-mysql-db – Gokul 2016-09-30 06:51:11

6

您似乎已經找到了足夠的答案,但我認爲我會用通用的Ruby方式進行操作。

# 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和數據庫),你會發現在Rails的那個cookie會話編碼會話哈希以同樣的方式。使用此功能從瀏覽器cookie調試會話內容通常很方便。

+0

The續集序列化插件以同樣的方式工作。您可以決定是否要使用Marshal,Yaml或Json來序列化和反序列化數據。但謝謝你的提示。 ;) – t6d 2010-01-28 13:46:27

1

如果您正在尋找JSON-API合規性,我已與JSONAPI::Serializers祝你好運。您需要傳入:skip_collection_check選項,並且爲了提高性能,您應該1)傳遞結果集,而不是數據集,以及2)在序列化之前(如果您在裝載數據)傳遞負載關係。

0

你可以給YAML一試:

require 'yaml' 

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

# decode 
my_hash = YAML.load(encoded) 

您可以將其存儲在一個字符串的續集,它的工作原理與複雜的對象,而且它是在DB可讀。我讀了關於它here

相關問題