Sha256: 07d46b2d7c53da5f2d318d282a7ae5e5cb6321671e261c5130fc189efda19350

Contents?: true

Size: 749 Bytes

Versions: 67

Compression:

Stored size: 749 Bytes

Contents

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

class Complex

  # Deserializes JSON string by converting Real value <tt>r</tt>, imaginary
  # value <tt>i</tt>, to a Complex object.
  def self.json_create(object)
    Complex(object['r'], object['i'])
  end

  # Returns a hash, that will be turned into a JSON object and represent this
  # object.
  def as_json(*)
    {
      JSON.create_id => self.class.name,
      'r'            => real,
      'i'            => imag,
    }
  end

  # Stores class name (Complex) along with real value <tt>r</tt> and imaginary value <tt>i</tt> as JSON string
  def to_json(*)
    as_json.to_json
  end
end

Version data entries

67 entries across 63 versions & 17 rubygems

Version Path
json-2.0.2 lib/json/add/complex.rb
json-2.0.1-java lib/json/add/complex.rb
json_pure-2.0.1 lib/json/add/complex.rb
json-2.0.1 lib/json/add/complex.rb
json-2.0.0-java lib/json/add/complex.rb
json_pure-2.0.0 lib/json/add/complex.rb
json-2.0.0 lib/json/add/complex.rb