Sha256: 542b2a8edf2951fc2d1a65220cd7ad0eef05f4df4c9984fbe4f5de1c288b383d

Contents?: true

Size: 1.39 KB

Versions: 20

Compression:

Stored size: 1.39 KB

Contents

#frozen_string_literal: false
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
  require 'json'
end
require 'ostruct'

class OpenStruct

  # See #as_json.
  def self.json_create(object)
    new(object['t'] || object[:t])
  end

  # Methods <tt>OpenStruct#as_json</tt> and +OpenStruct.json_create+ may be used
  # to serialize and deserialize a \OpenStruct object;
  # see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
  #
  # \Method <tt>OpenStruct#as_json</tt> serializes +self+,
  # returning a 2-element hash representing +self+:
  #
  #   require 'json/add/ostruct'
  #   x = OpenStruct.new('name' => 'Rowdy', :age => nil).as_json
  #   # => {"json_class"=>"OpenStruct", "t"=>{:name=>'Rowdy', :age=>nil}}
  #
  # \Method +JSON.create+ deserializes such a hash, returning a \OpenStruct object:
  #
  #   OpenStruct.json_create(x)
  #   # => #<OpenStruct name='Rowdy', age=nil>
  #
  def as_json(*)
    klass = self.class.name
    klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
    {
      JSON.create_id => klass,
      't'            => table,
    }
  end

  # Returns a JSON string representing +self+:
  #
  #   require 'json/add/ostruct'
  #   puts OpenStruct.new('name' => 'Rowdy', :age => nil).to_json
  #
  # Output:
  #
  #   {"json_class":"OpenStruct","t":{'name':'Rowdy',"age":null}}
  #
  def to_json(*args)
    as_json.to_json(*args)
  end
end

Version data entries

20 entries across 20 versions & 7 rubygems

Version Path
cloudsmith-api-2.0.11 vendor/bundle/ruby/2.6.0/gems/json-2.7.1/lib/json/add/ostruct.rb
cloudsmith-api-2.0.10 vendor/bundle/ruby/2.6.0/gems/json-2.7.1/lib/json/add/ostruct.rb
cloudsmith-api-2.0.9 vendor/bundle/ruby/2.6.0/gems/json-2.7.1/lib/json/add/ostruct.rb
cloudsmith-api-2.0.8 vendor/bundle/ruby/2.6.0/gems/json-2.7.1/lib/json/add/ostruct.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/json-2.7.1/lib/json/add/ostruct.rb
wallee-ruby-sdk-4.4.0 vendor/bundle/ruby/3.1.0/gems/json-2.7.1/lib/json/add/ostruct.rb
postfinancecheckout-ruby-sdk-4.4.0 vendor/bundle/ruby/3.1.0/gems/json-2.7.1/lib/json/add/ostruct.rb
postfinancecheckout-ruby-sdk-4.2.0 vendor/bundle/ruby/3.0.0/gems/json-2.7.1/lib/json/add/ostruct.rb
wallee-ruby-sdk-4.2.0 vendor/bundle/ruby/3.0.0/gems/json-2.7.1/lib/json/add/ostruct.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/json-2.7.1/lib/json/add/ostruct.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/json-2.7.1/lib/json/add/ostruct.rb
study_line-0.2.5 vendor/bundle/ruby/3.2.0/gems/json-2.7.1/lib/json/add/ostruct.rb
study_line-0.2.4 vendor/bundle/ruby/3.2.0/gems/json-2.7.1/lib/json/add/ostruct.rb
study_line-0.2.3 vendor/bundle/ruby/3.2.0/gems/json-2.7.1/lib/json/add/ostruct.rb
study_line-0.2.2 vendor/bundle/ruby/3.2.0/gems/json-2.7.1/lib/json/add/ostruct.rb
study_line-0.2.1 vendor/bundle/ruby/3.2.0/gems/json-2.7.1/lib/json/add/ostruct.rb
study_line-0.2.0 vendor/bundle/ruby/3.2.0/gems/json-2.7.1/lib/json/add/ostruct.rb
json-2.7.1-java lib/json/add/ostruct.rb
json_pure-2.7.1 lib/json/add/ostruct.rb
json-2.7.1 lib/json/add/ostruct.rb