Skip to content

Latest commit

 

History

History
40 lines (27 loc) · 948 Bytes

model_serialization.md

File metadata and controls

40 lines (27 loc) · 948 Bytes

Serialization

serializable_hash(options = nil) 序列化操作,提供了 :only, :except 选项。常用的 as_json 封装并扩展了它。

使用 Serialization 需要 record.respond_to? :attributes # => true 因为转换的过程使用到相应 model 的 attributes 实例方法。

此外,还有相关 module 及方法:

JSON

# 封装了上面的 serializable_hash,并提供 :root 选项 
as_json

# 调用了 attributes= 方法,给对象赋值
from_json

Xml

# 将对象转化成 xml 格式
to_xml

# 先将 xml 格式成 Hash,再调用了 attributes= 方法,给对象赋值
from_xml

该模块在 Rails 5 已被移除。

注意几个转化成 json 方法之间的区别:

person = Person.new
person.name = "Bob"

person.serializable_hash # => {"name"=>"Bob"}
person.as_json           # => {"name"=>"Bob"}
person.to_json           # => "{\"name\":\"Bob\"}"