class Rational

def self.json_create(object)

See #as_json.
def self.json_create(object)
  Rational(object['n'], object['d'])
end

def as_json(*)


# => (2/3)
Rational.json_create(x)

\Method +JSON.create+ deserializes such a hash, returning a \Rational object:

# => {"json_class"=>"Rational", "n"=>2, "d"=>3}
x = Rational(2, 3).as_json
require 'json/add/rational'

returning a 2-element hash representing +self+:
\Method Rational#as_json serializes +self+,

see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
to serialize and deserialize a \Rational object;
Methods Rational#as_json and +Rational.json_create+ may be used
def as_json(*)
  {
    JSON.create_id => self.class.name,
    'n'            => numerator,
    'd'            => denominator,
  }
end

def to_json(*args)


{"json_class":"Rational","n":2,"d":3}

Output:

puts Rational(2, 3).to_json
require 'json/add/rational'

Returns a JSON string representing +self+:
def to_json(*args)
  as_json.to_json(*args)
end