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

Oj.load may fail on custom #replace method implementation #775

Open
skryukov opened this issue Jun 19, 2022 · 2 comments
Open

Oj.load may fail on custom #replace method implementation #775

skryukov opened this issue Jun 19, 2022 · 2 comments

Comments

@skryukov
Copy link
Contributor

Originally, I have encountered this problem when I tried to dump and load Faraday::Header object in Oj's default mode.

Reproduction (same applies to Array and String subclasses):

require 'oj'

class SubHash < Hash
  def initialize(...)
    super
    @cache = []
  end

  def replace(*)
    @cache.clear
    super
  end
end

h = SubHash.new
h[:a] = 1
dh = Oj.dump(h)
# => "{\"^o\":\"SubHash\",\"self\":{\":a\":1},\"cache\":[]}"
Oj.load(dh)
# => undefined method `clear' for nil:NilClass
@ohler55
Copy link
Owner

ohler55 commented Jun 19, 2022

I'll look into it but I thought several years ago this came up and there was a disclaimer about subclasses of primitive type or maybe some support was added for limited cases. Anyway I'll look.

@skryukov
Copy link
Contributor Author

I have created this issue just for visibility, since personally, I use Oj::Rails.mimic_JSON to overcome this issue 😅

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

2 participants