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

Does not play well with DelegateClass #477

Closed
msakrejda opened this issue May 22, 2018 · 3 comments
Closed

Does not play well with DelegateClass #477

msakrejda opened this issue May 22, 2018 · 3 comments

Comments

@msakrejda
Copy link

I ran into an issue where Oj is not serializing a DelegateClass as I expected. I was able to minimize the test case to something pretty simple:

require 'oj'
require 'json'

a = [1, 2, 3]

class MyArray < DelegateClass(Array); end

m = MyArray.new(a)

puts <<-EOF
JSON.generate:
       raw: #{JSON.generate(a)}
  delegate: #{JSON.generate(m)}

Oj.dump:
       raw: #{Oj.dump(a)}
  delegate: #{Oj.dump(m)}
EOF

the output is:

JSON.generate:
       raw: [1,2,3]
  delegate: [1,2,3]

Oj.dump:
       raw: [1,2,3]
  delegate: {"^o":"MyArray","delegate_dc_obj":[1,2,3]}

Is this expected? I expected Oj to behave like JSON.generate here. If this is intended, is there an option to mimic the JSON behavior? I looked through the docs but nothing stood out as relevant to me.

I ran into this working with Sequel's pg_array extension which wraps Array via DelegateClass.

@ohler55
Copy link
Owner

ohler55 commented May 22, 2018

Oj is not replacement for the JSON gem by default. You have to use the :compat mode to make it act like the JSON gem. The default mode is object serialization mode. Take a look at the docs here http://ohler.com/oj/doc/file.Modes.html.

@msakrejda
Copy link
Author

Thanks! After reading that doc (and the linked compat doc), I'm still not clear on the difference between mode: :compat and mimic_JSON, but mimic_JSON appears to resolve my issue.

@ohler55
Copy link
Owner

ohler55 commented May 22, 2018

Compat mode is the same as the json gem output. With mimic you can make calls like JSON.dump and it will us Oj instead.

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