module Rison

Constants

FALSE
NIL
TRUE

Public Class Methods

Number(int, frac = nil, exp = nil) click to toggle source
# File lib/rison/parslet_transform.rb, line 39
def self.Number(int, frac = nil, exp = nil)
  base = frac ? int.to_i + Rational(frac.to_i, 10 ** frac.to_s.length) : int.to_i

  exp ? base * 10 ** exp.to_i : base
end
dump(object) click to toggle source
# File lib/rison/dump.rb, line 12
def self.dump(object)
  case object
    when NilClass then NIL

    when TrueClass then TRUE

    when FalseClass then FALSE

    when Symbol then object.to_s

    when Rational then object.to_f.to_s

    when Numeric then object.to_s

    when String then "'#{escape(object)}'"

    when Hash then '(%s)' % object.map { |(k, v)| dump_pair(k, v) }.join(?,)

    when Array then '!(%s)' % object.map { |member| dump(member) }.join(?,)

    else
      raise DumpError, "Cannot encode #{object.class} objects"
  end
end
dump_pair(key, value) click to toggle source
# File lib/rison/dump.rb, line 37
def self.dump_pair(key, value)
  key.is_a?(Symbol) ? "#{key}:#{dump(value)}" : "#{dump(key.to_s)}:#{dump(value)}"
end
escape(string) click to toggle source
# File lib/rison/dump.rb, line 41
def self.escape(string)
  string.gsub('!', '!!').gsub("'", "!'")
end
load(string) click to toggle source
# File lib/rison.rb, line 8
def self.load(string)
  ParsletTransform.new.apply(ParsletParser.new.parse(string))
rescue Parslet::ParseFailed => exception
  raise ParseError, "Invalid Rison input. #{exception.message}"
end