There are some examples using a custom Json attribute:
class Json < Virtus::Attribute
def coerce(value)
value.is_a?(::Hash) ? value : JSON.parse(value)
end
end
class User
include Virtus.model
attribute :info, Json
end
As of Ruby 2.1.0 MRI I get this error because coerce does not check for nil (but it was OK some time ago)
2.1.0 (main):0 > user = User.new
TypeError: no implicit conversion of nil into String
from /opt/rubies/2.1.0/lib/ruby/2.1.0/json/common.rb:155:in `initialize'
There are some examples using a custom Json attribute:
As of Ruby 2.1.0 MRI I get this error because
coerce
does not check for nil (but it was OK some time ago)