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

Behavior with Oj.default_options of create_additions: true #918

Open
mkdynamic opened this issue Apr 20, 2024 · 1 comment
Open

Behavior with Oj.default_options of create_additions: true #918

mkdynamic opened this issue Apr 20, 2024 · 1 comment

Comments

@mkdynamic
Copy link

When passing create_additions: true to the Oj.default_options, I expected that option to be applied to the JSON parsing APIs, but it seems to be ignored.

Oj.default_options = {create_additions: true}
Oj.optimize_rails

class Foo
  def initialize(data)
    @data = data
  end

  def as_json(*pargs, **kwargs, &blk)
    {
      "json_class" => self.class.name,
      "data" => @data
    }.as_json(*pargs, **kwargs, &blk)
  end

  def self.json_create(object, *)
    self.new(object["data"])
  end
end

Actual behavior:

foo_json = Foo.new("bar").to_json
puts(foo_json)
# => {"json_class":"Foo","data":"bar"}

foo = JSON.parse(foo_json)
puts(foo)
# => {"json_class"=>"Foo", "data"=>"bar"}

foo = ActiveSupport::JSON.decode(foo_json)
puts(foo)
# => {"json_class"=>"Foo", "data"=>"bar"}

Expected behavior:

foo_json = Foo.new("bar").to_json
puts(foo_json)
# => {"json_class":"Foo","data":"bar"}

foo = JSON.parse(foo_json)
puts(foo)
# => <Foo:0x... @data="bar">

foo = ActiveSupport::JSON.decode(foo_json)
puts(foo)
# => <Foo:0x... @data="bar">

Is this a bug or am I doing something wrong?

@ohler55
Copy link
Owner

ohler55 commented Apr 27, 2024

Have you looked at the :create_id option. The :create_id and :create_additions are options in the JSON that Oj also supports.

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