Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storing and reading JSON is asymmetrical unless load_raw_value is true #8

Open
qwertme opened this issue Feb 14, 2017 · 1 comment
Open

Comments

@qwertme
Copy link

qwertme commented Feb 14, 2017

When not using load_raw_value for a property you still need to serialize the value when setting the property even though you can read it as a Hash.

This is confusing since it is asymmetrical.

When load_raw_value is false I would expect that both reading and writing to the property to behave like accessing a Hash.

@qwertme
Copy link
Author

qwertme commented Feb 14, 2017

Here is a code example:

For this given class

class Content
  include DataMapper::Resource

  property :id, Serial
  property :hash_tags, String
  property :text, String
  property :template, String
  property :parameters, PgJSON, default: {}

The following behaviour is observed (take a look at command #13 to see the asymmetry)

irb(main):001:0> c = Content.new
=> #<Content @id=nil @hash_tags=nil @text=nil @template=nil @parameters=nil>
irb(main):002:0> c.parameters = {}
=> {}
irb(main):003:0> c.valid?
=> false
irb(main):004:0> c.errors.inspect
=> "#<DataMapper::Validations::ValidationErrors:0x007fd0d0b1faf0 @resource=#<Content @id=nil @hash_tags=nil @text=nil @template=nil @parameters={}>, @errors={:parameters=>[\"Parameters must be of type Object\"]}>"
irb(main):005:0> c.parameters = {}.to_json
=> "{}"
irb(main):006:0> c.valid?
=> true
irb(main):007:0> c.save
=> true
irb(main):008:0> c.reload
=> #<Content @id=1 @hash_tags=<not loaded> @text=<not loaded> @template=<not loaded> @parameters=<not loaded>>
irb(main):009:0> c.parameters
=> {}
irb(main):010:0> c.parameters['a'] = 'A'
=> "A"
irb(main):011:0> c.valid?
=> false
irb(main):012:0> c.errors.inspect
=> "#<DataMapper::Validations::ValidationErrors:0x007fd0d0b1faf0 @resource=#<Content @id=1 @hash_tags=nil @text=nil @template=nil @parameters={\"a\"=>\"A\"}>, @errors={:parameters=>[\"Parameters must be of type Object\"]}>"
irb(main):013:0> c.parameters = c.parameters.to_json
=> "{\"a\":\"A\"}"
irb(main):014:0> c.valid?
=> true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant