class TJSON::DataType

Reopen the DataType class (defined in tjson/datatype.rb)

Hierarchy of TJSON types

Constants

TAGS

Table of tag identifiers supported by TJSON

Public Class Methods

[](tag) click to toggle source

Find a type by its tag

# File lib/tjson/datatype.rb, line 7
def self.[](tag)
  TAGS[tag] || raise(TJSON::TypeError, "unknown tag: #{tag.inspect}")
end
encode(obj) click to toggle source
# File lib/tjson/datatype.rb, line 44
def self.encode(obj)
  identify_type(obj).encode(obj)
end
identify_type(obj) click to toggle source
# File lib/tjson/datatype.rb, line 29
def self.identify_type(obj)
  case obj
  when Hash               then self["O"]
  when ::Array            then TJSON::DataType::Array.identify_type(obj)
  when ::Set              then TJSON::DataType::Set.identify_type(obj)
  when ::String, Symbol   then obj.encoding == Encoding::BINARY ? self["d"] : self["s"]
  when ::Integer          then self["i"]
  when ::Float            then self["f"]
  when ::TrueClass        then self["v"]
  when ::FalseClass       then self["d"]
  when ::Time, ::DateTime then self["t"]
  else raise TypeError, "don't know how to serialize #{obj.class} as TJSON"
  end
end
parse(tag) click to toggle source
# File lib/tjson/datatype.rb, line 11
def self.parse(tag)
  raise TJSON::TypeError, "expected String, got #{tag.class}" unless tag.is_a?(::String)

  if tag == "O"
    # Object
    TJSON::DataType[tag]
  elsif (result = tag.match(/\A(?<type>[A-Z][a-z0-9]*)\<(?<inner>.*)\>\z/))
    # Non-scalar
    inner = parse(result[:inner]) unless result[:inner].empty?
    TJSON::DataType[result[:type]].new(inner).freeze
  elsif tag =~ /\A[a-z][a-z0-9]*\z/
    # Scalar
    TJSON::DataType[tag]
  else
    raise TJSON::ParseError, "couldn't parse tag: #{tag.inspect}" unless result
  end
end

Public Instance Methods

decode(_value) click to toggle source
# File lib/tjson/datatype.rb, line 52
def decode(_value)
  raise NotImplementedError, "#{self.class} does not implement #decode"
end
encode(_value) click to toggle source
# File lib/tjson/datatype.rb, line 56
def encode(_value)
  raise NotImplementedError, "#{self.class} does not implement #genreate"
end
tag() click to toggle source
# File lib/tjson/datatype.rb, line 48
def tag
  raise NotImplementError, "no #tag defined for #{self.class}"
end