Skip to content

Wish: Remove "forced" dependency from ostruct #988

@phorsuedzie

Description

@phorsuedzie

Ruby's core makes some effort to squeeze ostruct / OpenStruct out of the code. The gem 'json', promoted to a default gem now, is no exception to this. Any require "ostruct" there has been transformed into conditional code (see MRI 3.3 => 3.4).

  1. OpenStruct itself is monkey-patched only with an active require 'json/add/ostruct' (see e.g. https://rubyapi.org/3.4/o/openstruct)
  2. JSON::GenericObject is only an autoload. Which works even if require 'ostruct' would raise a LoadError.

Best if Oj would be able to drop s.add_runtime_dependency 'ostruct', '>= 0.2'. And run any require 'ostruct' only if "actively demanded" ...

  • ... by Oj.add_to_json() (if this continues to promise that OpenStruct is patched as a default). Note: if OpenStruct is passed as an argument, there is no need to require it.
  • ... because JSON::GenericObject is (auto-)loaded. Could even be, that Oj doesn't need to define JSON::GenericObject at all anymore (for a modern Ruby). AFAICS, the default json gem's JSON::GenericObject is
    • unconditionally available (at least for the lowest MRI version supported (RBX might be different): https://github.com/ruby/ruby/blob/ruby_2_4/ext/json/lib/json/generic_object.rb)
    • "demands" only an autoload declaration (which is set up by json gem's require "json", i.e. Oj would have to declare something similar; to require "json/generic_object" directly works immediately), and
    • the code of generic_object.rb calls nothing but methods from JSON, which are mimicked by Oj (AFAICS).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions