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

Don't define to_json and self.from_json on Object #6275

Conversation

asterite
Copy link
Member

Fixes #5695

As the title says, this removes to_json and self.from_json from Object and moves it to JSON::Serializable. For the class method (self.from_json) I had to define a module that gets extened on inclusion.

Then, JSON::Serializable is included on primitive types, Hash, Array, Set, etc. They of course override the to_json and self.from_json methods.

In that way, you can't invoke to_json on a method and get a strange compile error saying "undefined method to_json(io)", now you'll get a better error saying undefined method to_json`".

You could also check if a type is_a?(JSON::Serializable) or responds_to?(:to_json), and so on.

If this direction is accepted, I can later add another commit with the same change for YAML.

@asterite
Copy link
Member Author

Hm, it seems it doesn't work for a reason (I don't know why), and just compiling the specs for JSON triggers a compiler bug. Maybe the current way is good enough 😅

@asterite asterite closed this Jun 26, 2018
@asterite asterite deleted the feature/5695-remove-object-to_json branch June 26, 2018 14:09
@straight-shoota
Copy link
Member

There is already #5697 for this.

The failure might come from reusing JSON::Serializable with types that don't use the new annotation based JSON mapping.

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

2 participants