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

Serializable ActiveRecord columns with JSON #196

Closed
wants to merge 1 commit into from
Closed

Serializable ActiveRecord columns with JSON #196

wants to merge 1 commit into from

Conversation

txus
Copy link

@txus txus commented Feb 24, 2011

Hi! I'd like to include a JSON Column encoder to use with ActiveRecord::Base serialize. Now it only supports YAML.

What I don't quite like is that, since serialize already has a last argument with a default, the options hash isn't so comfortable to use. In my pull request to serialize a column with JSON you must do this:

class Post < ActiveRecord::Base
  serialize :data, Hash, :format => :json
end

You can't omit the Hash part if you want to specify a :format, and I don't like that. How would you improve this?

@oriolgual
Copy link
Contributor

obj ||= object_class.new if object_class != Object

obj
rescue *RESCUE_ERRORS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to this instead just doing: rescue ActiveSupport::JSON.backend::ParseError ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just mimics the implementation of the YAML serializer, but I think you are right, there's no need for this abstraction.

@NZKoz
Copy link
Member

NZKoz commented May 3, 2011

You can either change the signature to use *args and args.extract_options! to support an optional hash argument. Or you'll have to do something where you check something like if class_name.is_a? Hash.

For now I'd prefer we split the pull request in two, apply a JSONColumn (with @miloops' suggested improvements) and then think about some syntactic sugar for the serialize method if needs be

@NZKoz NZKoz closed this May 3, 2011
@benatkin
Copy link

benatkin commented Oct 3, 2011

@oriolgual I tried to find an open issue or a pull request that covers the idea of adding Coders::JSONColumn to go along with Coders::YAMLColumn but didn't find one.

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

Successfully merging this pull request may close these issues.

None yet

5 participants